homepage/scripts/generate-rss.ts
2025-02-03 16:31:04 +08:00

103 lines
2.6 KiB
TypeScript

import { Feed } from 'feed';
import fs from 'fs-extra';
import path from 'path';
import { Update } from '../src/utils/updates';
const SITE_URL = 'https://starset.wiki';
interface LanguageConfig {
code: string;
dataDir: string;
title: string;
description: string;
}
const LANGUAGES: LanguageConfig[] = [
{
code: 'en-US',
dataDir: 'en-US',
title: 'STARSET Mirror Site Updates',
description: 'Latest updates from STARSET Mirror Site'
},
{
code: 'zh-CN',
dataDir: 'zh-CN',
title: 'STARSET 镜像站更新',
description: 'STARSET 镜像站的最新更新'
},
{
code: 'zh-Hant',
dataDir: 'zh-TW',
title: 'STARSET 鏡像站更新',
description: 'STARSET 鏡像站的最新更新'
}
];
async function generateRSSFeed(lang: LanguageConfig) {
// Read all updates
const updatesIndex = await fs.readJson(path.join('data', lang.dataDir, 'updates.json'));
const years = updatesIndex.years as string[];
let allUpdates: Update[] = [];
for (const year of years) {
const yearData = await fs.readJson(path.join('data', lang.dataDir, 'updates', year));
allUpdates = [...allUpdates, ...yearData.updates];
}
// Sort updates by date in descending order
allUpdates.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
// Take the latest 10 updates
const latestUpdates = allUpdates.slice(0, 10);
// Create feed
const feed = new Feed({
title: lang.title,
description: lang.description,
id: `${SITE_URL}/${lang.code}/updates`,
link: `${SITE_URL}/${lang.code}/updates`,
language: lang.code,
favicon: `${SITE_URL}/favicon.ico`,
copyright: "All rights reserved",
updated: latestUpdates[0] ? new Date(latestUpdates[0].date) : new Date(),
feedLinks: {
rss2: `${SITE_URL}/${lang.code}/rss.xml`
}
});
// Add items to feed
for (const update of latestUpdates) {
feed.addItem({
title: update.title,
id: update.id,
link: update.link || `${SITE_URL}/${lang.code}/updates#${update.id}`,
description: update.summary,
date: new Date(update.date),
category: update.tags.map(tag => ({ name: tag }))
});
}
// Create dist directory if it doesn't exist
const distPath = path.join('dist', lang.code);
await fs.ensureDir(distPath);
// Write feed to file
await fs.writeFile(
path.join(distPath, 'rss.xml'),
feed.rss2()
);
}
async function main() {
try {
for (const lang of LANGUAGES) {
await generateRSSFeed(lang);
console.log(`Generated RSS feed for ${lang.code}`);
}
} catch (error) {
console.error('Error generating RSS feeds:', error);
process.exit(1);
}
}
main();