blok Aktuelle Nachrichten: rubrike se izmenjujejo v krogu, da ne pride trikrat zapored Sport
This commit is contained in:
+29
-8
@@ -1073,16 +1073,37 @@ export async function registerRoutes(
|
|||||||
signal: AbortSignal.timeout(10000),
|
signal: AbortSignal.timeout(10000),
|
||||||
});
|
});
|
||||||
const arts: any[] = await r.json();
|
const arts: any[] = await r.json();
|
||||||
const items = (Array.isArray(arts) ? arts : [])
|
const sveze = (Array.isArray(arts) ? arts : [])
|
||||||
.filter((a) => a && a.slug && a.title)
|
.filter((a) => a && a.slug && a.title)
|
||||||
.sort((a, b) => String(b.publishedAt || "").localeCompare(String(a.publishedAt || "")))
|
.sort((a, b) => String(b.publishedAt || "").localeCompare(String(a.publishedAt || "")))
|
||||||
.slice(0, 12)
|
.slice(0, 60);
|
||||||
.map((a) => ({
|
|
||||||
title: String(a.title),
|
// Rubrike se morajo izmenjevati: sicer pridejo trije Sporti zapored,
|
||||||
link: `https://news.folx.tv/${a.slug}`,
|
// ker jih bot objavi v svezju. Vzamemo po eno iz vsake rubrike v krogu,
|
||||||
source: String(a.category || "FOLX NEWS"),
|
// znotraj rubrike pa ostane vrstni red po datumu.
|
||||||
pubDate: String(a.publishedAt || ""),
|
const poRubriki = new Map<string, any[]>();
|
||||||
}));
|
for (const a of sveze) {
|
||||||
|
const k = String(a.category || "FOLX NEWS");
|
||||||
|
if (!poRubriki.has(k)) poRubriki.set(k, []);
|
||||||
|
poRubriki.get(k)!.push(a);
|
||||||
|
}
|
||||||
|
const kroznik: any[] = [];
|
||||||
|
let se = true;
|
||||||
|
while (se && kroznik.length < 12) {
|
||||||
|
se = false;
|
||||||
|
for (const vrsta of poRubriki.values()) {
|
||||||
|
const a = vrsta.shift();
|
||||||
|
if (a) { kroznik.push(a); se = true; }
|
||||||
|
if (kroznik.length >= 12) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const items = kroznik.map((a) => ({
|
||||||
|
title: String(a.title),
|
||||||
|
link: `https://news.folx.tv/${a.slug}`,
|
||||||
|
source: String(a.category || "FOLX NEWS"),
|
||||||
|
pubDate: String(a.publishedAt || ""),
|
||||||
|
}));
|
||||||
if (items.length > 0) setCache(cacheKey, items);
|
if (items.length > 0) setCache(cacheKey, items);
|
||||||
res.json(items);
|
res.json(items);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
|||||||
Reference in New Issue
Block a user