diff options
Diffstat (limited to 'scripts/ytdlp_flatten.ts')
-rw-r--r-- | scripts/ytdlp_flatten.ts | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/scripts/ytdlp_flatten.ts b/scripts/ytdlp_flatten.ts index 6d246da..a433665 100644 --- a/scripts/ytdlp_flatten.ts +++ b/scripts/ytdlp_flatten.ts @@ -3,11 +3,20 @@ import { Config } from "./config.ts"; const ws = new WebSocket(Deno.args[0]) let config: Config = {} as unknown as Config +const supported = [ + "youtube-channel", + "bilibili-channel", +] 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}`] + if (kind == "bilibili-channel") return ["bilibili", `https://space.bilibili.com/${id}/upload/video`] throw new Error("unknown kind"); } +function key_to_infokey(key: string): string { + const [kind, id] = key.split(":", 2) + return `${kind}-info:${id}` +} async function flat_playlist(url: string, kind: string, data: { [key: string]: unknown }) { const flags = (data.flags as string[]) ?? [] @@ -24,10 +33,12 @@ async function flat_playlist(url: string, kind: string, data: { [key: string]: u stdout: "piped", stderr: "inherit", }).output() + if (!o.success) throw new Error("yt-dlp failure code"); const otext = new TextDecoder().decode(o.stdout) for (const line of otext.split("\n")) { if (!line.length) continue const ob = JSON.parse(line) + console.log(ob); const key = `${kind}:${ob.id}`; ws.send(JSON.stringify({ t: "metadata", @@ -35,8 +46,8 @@ async function flat_playlist(url: string, kind: string, data: { [key: string]: u data: { ...data, title: ob.title, - subtitle: `by ${ob.playlist_uploader}; duration ${ob.duration_string}`, - thumbnail: ob.thumbnails[0]?.url, + subtitle: `by ${ob.playlist_uploader ?? data.output ?? "unknown"}; duration ${ob.duration_string}`, + thumbnail: ob.thumbnails ? ob.thumbnails[0]?.url : null, // description: ob.description, } })) @@ -52,7 +63,7 @@ ws.onclose = () => { } ws.onopen = () => { console.log("ws open"); - ws.send(JSON.stringify({ t: "register", name: "yt-dlp playlist flattener", task_kinds: ["youtube-channel"] })) + ws.send(JSON.stringify({ t: "register", name: "yt-dlp playlist flattener", task_kinds: supported })) ws.send(JSON.stringify({ t: "accept" })) } ws.onmessage = async ev => { @@ -64,6 +75,7 @@ ws.onmessage = async ev => { const [outkind, url] = key_to_url(p.key) if (!p.data.output) throw new Error("no output"); await flat_playlist(url, outkind, p.data) + ws.send(JSON.stringify({ t: "enqueue", key: key_to_infokey(p.key) })) ws.send(JSON.stringify({ t: "complete", key: p.key })) ws.send(JSON.stringify({ t: "save" })) ws.send(JSON.stringify({ t: "accept" })) |