Kapitel 10/Free Rhohtext.md aktualisiert
This commit is contained in:
@@ -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 {
|
||||
return [{
|
||||
json: {
|
||||
uid: seg.id,
|
||||
cancelled: Boolean(seg.canceled_until),
|
||||
title, description,
|
||||
startIso, endIso, tz,
|
||||
hash, kategorie
|
||||
uid: next.uid,
|
||||
title: next.title,
|
||||
kategroie: next.kategorie,
|
||||
description: next.description,
|
||||
startIso: next.startIso,
|
||||
endIso: next.endIso,
|
||||
tz: next.tz,
|
||||
hash: next.hash
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return out;
|
||||
}];
|
||||
```
|
||||
|
||||
[!TIP]
|
||||
|
||||
Reference in New Issue
Block a user