From 2237856fa676085d59c1f7575c9c9c071e217453 Mon Sep 17 00:00:00 2001 From: Thomas Dannenberg Date: Sat, 11 Oct 2025 20:10:21 +0000 Subject: [PATCH] Kapitel 10/Free Rhohtext.md aktualisiert --- Kapitel 10/Free Rhohtext.md | 77 +++++++++++-------------------------- 1 file changed, 23 insertions(+), 54 deletions(-) diff --git a/Kapitel 10/Free Rhohtext.md b/Kapitel 10/Free Rhohtext.md index 0b08871..e39b2a1 100644 --- a/Kapitel 10/Free Rhohtext.md +++ b/Kapitel 10/Free Rhohtext.md @@ -872,66 +872,35 @@ Kopiere den folgenden Code vollständig in das Code-Feld: ``` /** - * INPUT: items[0].json ODER items[0].json.body (String) vom HTTP Request /helix/schedule - * OUTPUT: pro Segment ein Item in deinem Schema: - * { uid, cancelled, title, description, startIso, endIso, tz, hash } + * INPUT: Array von Items aus "Parse ICS" + * OUTPUT: nur das nächste bevorstehende Event */ -const input = items[0]?.json ?? {}; -let payload; +const now = Date.now(); -if (typeof input.body === 'string') { - // HTTP-Node hat "Response Format: Text" -> JSON-String in body - try { payload = JSON.parse(input.body); } - catch (e) { throw new Error('HTTP body ist kein gültiges JSON'); } -} else if (input.data) { - // HTTP-Node hat "Response Format: JSON" - payload = input; -} else if (typeof input === 'string') { - payload = JSON.parse(input); -} else { - throw new Error('Unerwartete Eingabeform. Erwartet JSON oder body-String.'); +const upcoming = items + .map(i => i.json) + .filter(e => !e.cancelled && new Date(e.startIso).getTime() > now) + .sort((a, b) => new Date(a.startIso) - new Date(b.startIso)); + +if (upcoming.length === 0) { + return [{ json: { message: 'Kein bevorstehender Stream gefunden' } }]; } -const segments = Array.isArray(payload.data?.segments) ? payload.data.segments : []; +const next = upcoming[0]; -// Hash wie in deinem ICS-Code -function hashHex(s) { - let h = 5381; - for (let i = 0; i < s.length; i++) h = ((h << 5) + h) ^ s.charCodeAt(i); - return (h >>> 0).toString(16).padStart(8, '0'); -} - -function buildTitle() { return 'Streamtime'; } - -function buildDesc(seg) { - const lines = []; - if (seg.title) lines.push(`Originaltitel: ${seg.title}`); - if (seg.category?.name) lines.push(`Kategorie/Game: ${seg.category.name}`); - return lines.join('\n'); -} - -const tz = 'Europe/Berlin'; - -const out = segments.map(seg => { - const startIso = seg.start_time; // ISO-UTC laut Twitch - const endIso = seg.end_time; - const title = buildTitle(); - const description = buildDesc(seg); - const hash = hashHex(`${title}|${startIso}|${endIso}|${description}`); - const kategorie = seg.category?.name; - return { - json: { - uid: seg.id, - cancelled: Boolean(seg.canceled_until), - title, description, - startIso, endIso, tz, - hash, kategorie - } - }; -}); - -return out; +return [{ + json: { + uid: next.uid, + title: next.title, + kategroie: next.kategorie, + description: next.description, + startIso: next.startIso, + endIso: next.endIso, + tz: next.tz, + hash: next.hash + } +}]; ``` [!TIP]