diff options
author | metamuffin <metamuffin@disroot.org> | 2025-05-17 23:05:56 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-05-17 23:05:56 +0200 |
commit | 15065f924214e00594bbc77ea9b4c7adf53c9b2a (patch) | |
tree | f4436e6200cb5beae66884695ef1c261f8aa1e32 /scripts/ytdlp_flatten.ts | |
parent | f2dbd1c247d23628768ec7bc6ed9e32fb23fee1e (diff) | |
download | isda-15065f924214e00594bbc77ea9b4c7adf53c9b2a.tar isda-15065f924214e00594bbc77ea9b4c7adf53c9b2a.tar.bz2 isda-15065f924214e00594bbc77ea9b4c7adf53c9b2a.tar.zst |
flat playlist works
Diffstat (limited to 'scripts/ytdlp_flatten.ts')
-rw-r--r-- | scripts/ytdlp_flatten.ts | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/scripts/ytdlp_flatten.ts b/scripts/ytdlp_flatten.ts index 6a3eb5e..7cd9da1 100644 --- a/scripts/ytdlp_flatten.ts +++ b/scripts/ytdlp_flatten.ts @@ -1,14 +1,61 @@ - const ws = new WebSocket(Deno.args[0]) + +function key_to_url(key: string): [string, string] { + const [kind, id] = key.split(":", 2) + if (kind == "youtube-channel") return ["youtube", `https://youtube.com/channel/${id}`] + throw new Error("unknown kind"); +} + +async function flat_playlist(url: string, kind: string, output: string) { + console.log(output, url); + const o = await new Deno.Command("yt-dlp", { + args: [ + "--flat-playlist", + "--print-json", + "--match-filter", "availability=public & live_status=not_live", + url + ] + }).output() + const otext = new TextDecoder().decode(o.stdout) + for (const line of otext.split("\n")) { + if (!line.length) continue + const ob = JSON.parse(line) + const key = `${kind}:${ob.id}`; + ws.send(JSON.stringify({ + t: "metadata", + key, + data: { + title: ob.title, + subtitle: `by ${ob.playlist_uploader}; duration ${ob.duration_string}`, + description: ob.description, + thumbnail: ob.thumbnails[0]?.url, + output, + } + })) + ws.send(JSON.stringify({ t: "enqueue", key })) + } + console.log("done"); +} + ws.onerror = () => console.error("ws error") ws.onclose = () => console.error("ws closed") ws.onopen = () => { console.log("ws open"); - ws.send(JSON.stringify({ t: "register", name: "yt-dlp playlist flattener", sources: ["ytdlp-flatten"] })) + ws.send(JSON.stringify({ t: "register", name: "yt-dlp playlist flattener", task_kinds: ["youtube-channel"] })) ws.send(JSON.stringify({ t: "accept" })) } -ws.onmessage = ev => { - console.log(ev.data); +ws.onmessage = async ev => { + if (typeof ev.data != "string") return + const p = JSON.parse(ev.data) + if (p.t == "error") console.error(`error: ${p.message}`); + if (p.t == "work") { + const [outkind, url] = key_to_url(p.key) + if (!p.data.output) throw new Error("no output"); + await flat_playlist(url, outkind, p.data.output) + ws.send(JSON.stringify({ t: "complete", key: p.key })) + ws.send(JSON.stringify({ t: "save" })) + ws.send(JSON.stringify({ t: "accept" })) + } } |