Filter news articles to show only recent posts
Add a `maxAgeDays` parameter to `parseRssItems` function to filter out older articles, applying a 14-day limit to music news and a 1-day limit to breaking news. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 517dfa7b-26ac-463d-a6e1-a58c6df97188 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: ace20c2a-f332-4725-b83e-8ebf770dc2f6 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/f209e72a-0939-48fa-84fc-57854de71967/517dfa7b-26ac-463d-a6e1-a58c6df97188/nFw7xof Replit-Helium-Checkpoint-Created: true
This commit is contained in:
parent
9bccf2883e
commit
8bc1d063fb
@ -364,7 +364,7 @@ export async function registerRoutes(
|
||||
}
|
||||
});
|
||||
|
||||
function parseRssItems(xml: string): { title: string; link: string; source: string; pubDate: string }[] {
|
||||
function parseRssItems(xml: string, maxAgeDays?: number): { title: string; link: string; source: string; pubDate: string }[] {
|
||||
const items: { title: string; link: string; source: string; pubDate: string }[] = [];
|
||||
const itemRegex = /<item>([\s\S]*?)<\/item>/g;
|
||||
let match;
|
||||
@ -377,6 +377,10 @@ export async function registerRoutes(
|
||||
let pubDate = "";
|
||||
try {
|
||||
const d = new Date(pubDateRaw);
|
||||
if (maxAgeDays !== undefined) {
|
||||
const ageMs = Date.now() - d.getTime();
|
||||
if (ageMs > maxAgeDays * 24 * 3600000) continue;
|
||||
}
|
||||
const diffH = Math.floor((Date.now() - d.getTime()) / 3600000);
|
||||
if (diffH < 1) pubDate = "Gerade eben";
|
||||
else if (diffH < 24) pubDate = `vor ${diffH} Std.`;
|
||||
@ -411,7 +415,7 @@ export async function registerRoutes(
|
||||
const topic = topics[Math.floor(Date.now() / 3600000) % topics.length];
|
||||
const rssUrl = `https://news.google.com/rss/search?q=${topic}&hl=de&gl=DE&ceid=DE:de`;
|
||||
const xml = await fetchRssFeed(rssUrl);
|
||||
const items = parseRssItems(xml);
|
||||
const items = parseRssItems(xml, 14);
|
||||
if (items.length > 0) setCache(cacheKey, items);
|
||||
res.json(items);
|
||||
} catch (err: any) {
|
||||
@ -431,7 +435,7 @@ export async function registerRoutes(
|
||||
const topic = topics[Math.floor(Date.now() / 3600000) % topics.length];
|
||||
const rssUrl = `https://news.google.com/rss/search?q=${topic}&hl=de&gl=DE&ceid=DE:de`;
|
||||
const xml = await fetchRssFeed(rssUrl);
|
||||
const items = parseRssItems(xml);
|
||||
const items = parseRssItems(xml, 1);
|
||||
if (items.length > 0) setCache(cacheKey, items);
|
||||
res.json(items);
|
||||
} catch (err: any) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user