summaryrefslogtreecommitdiffstats
path: root/src/index.ts
blob: 7fe2128ca7b8a282fe0825d966cfd0f10566fb83 (plain) (blame)
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
import config from "./config";
import * as jobs from "./services/jobs";
import * as cliArgs from "ts-command-line-args";
import PostDatabase from "./services/postDatabase";

export const queueDb = new PostDatabase("queue.json");

const args = cliArgs.parse<{
    id?: number;
    enqueue?: number[];
    dequeue?: number[];
    help?: boolean;
}>(
    {
        id: { type: Number, optional: true },
        enqueue: { type: Number, optional: true, multiple: true },
        dequeue: { type: Number, optional: true, multiple: true },
        help: { type: Boolean, optional: true, alias: "h" },
    },
    {
        helpArg: "help",
    }
);

(async () => {
    if (!config.mastodon.token) {
        console.error("MASTODON_TOKEN not set");
        return;
    }

    if (args.id) {
        await jobs.postSpecificPicture(args.id);
    } else if (args.enqueue) {
        for (const id of args.enqueue) {
            console.log(`Enqueueing post ${id}...`);

            await queueDb.insertIfNotExists({ provider: "e926", id });
        }
    } else if (args.dequeue) {
        for (const id of args.dequeue) {
            console.log(`Dequeueing post ${id}...`);

            await queueDb.remove({ provider: "e926", id });
        }
    } else {
        console.log("Reading queue...");

        const queued = await queueDb.takeFirst();

        if (queued) {
            await jobs.postSpecificPicture(queued.id);
        } else {
            await jobs.postRandomPicture();
        }
    }
})();