aboutsummaryrefslogtreecommitdiff
path: root/scripts/ytdlp_flatten.ts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/ytdlp_flatten.ts')
-rw-r--r--scripts/ytdlp_flatten.ts55
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" }))
+ }
}