aboutsummaryrefslogtreecommitdiff
path: root/scripts/youtube_xml_feed.ts
blob: 5d21542bb40495cea388b06b27b60a3e270d5f98 (plain)
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
import { Config } from "./config.ts";

const ws = new WebSocket(Deno.args[0])
// deno-lint-ignore no-unused-vars
let config: Config = {} as unknown as Config

async function flat_feed(cid: string, data: { [key: string]: unknown }) {
    const feedurl = `https://www.youtube.com/feeds/videos.xml?channel_id=${cid}`
    console.log(`load feed ${feedurl}`);
    const res = await fetch(feedurl)
    if (!res.ok) throw new Error("feed download error:" + await res.text());
    const feed = await res.text()

    for (const entry of feed.split("<entry>")) {
        const id_match = entry.match(/\<yt:videoId\>([0-9A-Za-z-_]{11})\<\/yt:videoId\>/)
        const title_match = entry.match(/\<title\>([^\<]*)\<\/title\>/)
        const uploader_match = entry.match(/\<name\>([^\<]*)\<\/name\>/)
        if (!id_match || !title_match || !uploader_match) continue
        const id = id_match[1]
        const title = title_match[1]
        const uploader = uploader_match[1]
        const key = `youtube:${id}`;
        ws.send(JSON.stringify({
            t: "metadata",
            key,
            data: {
                ...data,
                title: title,
                subtitle: `by ${uploader}`,
            }
        }))
        ws.send(JSON.stringify({ t: "enqueue", key }))
    }
    console.log("done");
}

ws.onerror = () => console.error("ws error")
ws.onclose = () => {
    console.error("ws closed")
    Deno.exit(1)
}
ws.onopen = () => {
    console.log("ws open");
    ws.send(JSON.stringify({ t: "register", name: "YouTube XML feed flattener", task_kinds: ["youtube-channel-xmlfeed"] }))
    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 == "config") config = p.config
    if (p.t == "error") console.error(`error: ${p.message}`);
    if (p.t == "work") {
        await flat_feed(p.key.replace("youtube-channel-xmlfeed:", ""), p.data)
        ws.send(JSON.stringify({ t: "complete", key: p.key }))
        ws.send(JSON.stringify({ t: "save" }))
        ws.send(JSON.stringify({ t: "accept" }))
    }
}