diff --git a/Kapitel 10/Free Rhohtext.md b/Kapitel 10/Free Rhohtext.md index 67096ce..0b08871 100644 --- a/Kapitel 10/Free Rhohtext.md +++ b/Kapitel 10/Free Rhohtext.md @@ -872,34 +872,66 @@ Kopiere den folgenden Code vollständig in das Code-Feld: ``` /** - * INPUT: Array von Items aus "Parse ICS" - * OUTPUT: nur das nächste bevorstehende Event + * 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 } */ -const now = Date.now(); +const input = items[0]?.json ?? {}; +let payload; -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' } }]; +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 next = upcoming[0]; +const segments = Array.isArray(payload.data?.segments) ? payload.data.segments : []; -return [{ - json: { - uid: next.uid, - title: next.title, - description: next.description, - startIso: next.startIso, - endIso: next.endIso, - tz: next.tz, - hash: next.hash - } -}]; +// 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; ``` [!TIP]