aboutsummaryrefslogtreecommitdiff
path: root/scripts/complete_from_files.ts
blob: 8093c20a3a56709f13c6087c1a33ea474a43e06b (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
const ws = new WebSocket(Deno.args[0])
const root = Deno.args[1]

async function traverse(path: string) {
    console.log(`> ${path}`);
    for await (const e of Deno.readDir(path)) {
        if (e.isDirectory) await traverse(path + "/" + e.name)
        else if (e.isFile) mark_complete(e.name)
    }
}

let counter = 0;
function mark_complete(name: string) {
    // TODO other platforms
    const r = /([A-Za-z0-9-_]{11})\.mkv/g.exec(name)
    if (!r) return
    const id = r[1]
    counter += 1
    ws.send(JSON.stringify({ t: "complete", key: `youtube:${id}`, force: true }))
}

ws.onerror = () => console.error("ws error")
ws.onclose = () => console.error("ws closed")
ws.onopen = async () => {
    console.log("ws open");
    ws.send(JSON.stringify({ t: "register", name: "complete from files", task_kinds: [] }))
    await traverse(root)
    ws.send(JSON.stringify({ t: "save" }))
    console.log(`done, ${counter} tasks marked as complete`);
    setTimeout(() => Deno.exit(0), 200) // not sure if websockets are flushed since they're instant
}
ws.onmessage = ev => {
    if (typeof ev.data != "string") return
    const p = JSON.parse(ev.data)
    if (p.t == "config") return
    if (p.t == "error") console.error(`error: ${p.message}`);
}