1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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", task_kinds: ["youtube-channel"] }))
ws.send(JSON.stringify({ t: "accept" }))
}
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" }))
}
}
|