summaryrefslogtreecommitdiffstats
path: root/src/index.ts
blob: 42081e6eb8f3fa01f9813cb0d1129ae71a82243b (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
import * as e621 from "./api/e621";
import * as mastodon from "./api/mastodon";
import config from "./config";

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

    console.log("Fetching post...");

    const post = await e621.randomPost();
    const source = post.sources.length ? post.sources[0] : undefined;
    const cws = config.cw.filter((w) => post.tags.general.includes(w));

    console.log(`Got ${post.id}`);
    console.log(`Downloading image...`);

    const file = await e621.client.get(post.file.url).buffer();

    /*console.log(`Compressing...`);

    const compressedFile = await sharp(file)
        .resize(1000, 1000, {
            fit: "inside",
            withoutEnlargement: true,
        })
        .jpeg({ quality: 85, mozjpeg: true })
        .toBuffer();*/

    console.log(`Uploading...`);

    const attachment = await mastodon.upload(file, post.id.toString(10));

    console.log(`Posting status...`);

    const status = await mastodon.createStatus(`https://e926.net/posts/${post.id}`, source, cws, attachment.id);

    console.log(`Done! ${status.url}`);
})();