aboutsummaryrefslogtreecommitdiff
path: root/scripts/enqueue.ts
blob: e6e3958559ed6c27c1a0d80a27aae4e6c4985c05 (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
const file = await Deno.readTextFile(Deno.args[1])
const note_filter = Deno.args.length >= 3 ? Deno.args[2] : ""

const ws = new WebSocket(Deno.args[0])

function run_enqueue() {
    let kind = "http"
    for (const line of file.split("\n")) {
        if (!line.trim().length) continue
        else if (line.startsWith("[") && line.endsWith("]"))
            kind = line.substring(1, line.length - 1)
        else {
            const [name, rest] = line.split("=", 2)
            const [id, note] = rest.split(";", 2)
            if (note_filter.length && note != note_filter) continue
            const key = `${kind}:${id}`;
            ws.send(JSON.stringify({ t: "metadata", key, data: { output: name, title: name } }))
            ws.send(JSON.stringify({ t: "enqueue", key }))
        }
    }
    ws.send(JSON.stringify({ t: "save" }))
    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: "enqueuer", task_kinds: [] }))
    run_enqueue()
}
ws.onmessage = ev => {
    if (typeof ev.data != "string") return
    const p = JSON.parse(ev.data)
    if (p.t == "error") console.error(`error: ${p.message}`);
}