summaryrefslogtreecommitdiffstats
path: root/src/index.ts
diff options
context:
space:
mode:
authorVolpeon <git@volpeon.ink>2021-10-18 13:50:57 +0200
committerVolpeon <git@volpeon.ink>2021-10-18 13:50:57 +0200
commit8a6ad69c1e859db373a67c75f893853986750603 (patch)
tree92ddcffc840dc444c1a28a883a62fc8eea01958c /src/index.ts
parentAdd presenting to excluded tags (diff)
downloadferalbot-8a6ad69c1e859db373a67c75f893853986750603.tar.gz
feralbot-8a6ad69c1e859db373a67c75f893853986750603.tar.bz2
feralbot-8a6ad69c1e859db373a67c75f893853986750603.zip
Support posting a specific picture
Diffstat (limited to 'src/index.ts')
-rw-r--r--src/index.ts64
1 files changed, 46 insertions, 18 deletions
diff --git a/src/index.ts b/src/index.ts
index 34cd5f1..e2fee01 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,34 +1,49 @@
1import * as e621 from "./api/e621"; 1import * as e621 from "./api/e621";
2import * as mastodon from "./api/mastodon"; 2import * as mastodon from "./api/mastodon";
3import config from "./config"; 3import config from "./config";
4import * as cliArgs from 'ts-command-line-args';
4 5
5(async () => { 6const args = cliArgs.parse<{
6 if (!config.mastodon.token) { 7 id?: number;
7 console.error("MASTODON_TOKEN not set"); 8 help?: boolean;
8 return; 9}>(
9 } 10 {
11 id: { type: Number, optional: true },
12 help: { type: Boolean, optional: true, alias: 'h' },
13 },
14 {
15 helpArg: 'help',
16 },
17);
18
19async function postRandomPicture() {
20 console.log("Fetching random post...");
21
22 const { queryIndex, post } = await e621.getRandomPost();
23
24 console.log(`Got ${post.id} via query ${queryIndex}`);
25
26 await handlePost(post);
27}
28
29async function postSpecificPicture(id: number) {
30 console.log("Fetching post ${id}...");
31
32 const post = await e621.getPost(id);
10 33
11 console.log("Fetching post..."); 34 console.log(`Got ${post.id}`);
35
36 await handlePost(post);
37}
12 38
13 const { queryIndex, post } = await e621.randomPost(); 39async function handlePost(post: e621.Post) {
14 const source = post.sources.length ? post.sources[0] : undefined; 40 const source = post.sources.length ? post.sources[0] : undefined;
15 const cws = config.cw.filter((w) => post.tags.general.includes(w)); 41 const cws = config.cw.filter((w) => post.tags.general.includes(w));
16 42
17 console.log(`Got ${post.id} via query ${queryIndex}`);
18 console.log(`Downloading image...`); 43 console.log(`Downloading image...`);
19 44
20 const file = await e621.client.get(post.file.url).buffer(); 45 const file = await e621.client.get(post.file.url).buffer();
21 46
22 /*console.log(`Compressing...`);
23
24 const compressedFile = await sharp(file)
25 .resize(1000, 1000, {
26 fit: "inside",
27 withoutEnlargement: true,
28 })
29 .jpeg({ quality: 85, mozjpeg: true })
30 .toBuffer();*/
31
32 console.log(`Uploading...`); 47 console.log(`Uploading...`);
33 48
34 const attachment = await mastodon.upload(file, post.id.toString(10)); 49 const attachment = await mastodon.upload(file, post.id.toString(10));
@@ -38,4 +53,17 @@ import config from "./config";
38 const status = await mastodon.createStatus(`https://e926.net/posts/${post.id}`, source, cws, attachment.id); 53 const status = await mastodon.createStatus(`https://e926.net/posts/${post.id}`, source, cws, attachment.id);
39 54
40 console.log(`Done! ${status.url}`); 55 console.log(`Done! ${status.url}`);
56}
57
58(async () => {
59 if (!config.mastodon.token) {
60 console.error("MASTODON_TOKEN not set");
61 return;
62 }
63
64 if (args.id) {
65 await postSpecificPicture(args.id);
66 } else {
67 await postRandomPicture();
68 }
41})(); 69})();