diff options
author | Volpeon <git@volpeon.ink> | 2021-10-19 17:32:48 +0200 |
---|---|---|
committer | Volpeon <git@volpeon.ink> | 2021-10-19 17:32:48 +0200 |
commit | 500e0ed6eb408dd5566cdce47e0ad0f4945b2376 (patch) | |
tree | d1c69cbaacc00887a09939e63dd8fadd8a605a91 /src/index.ts | |
parent | Code improvements (diff) | |
download | feralbot-500e0ed6eb408dd5566cdce47e0ad0f4945b2376.tar.gz feralbot-500e0ed6eb408dd5566cdce47e0ad0f4945b2376.tar.bz2 feralbot-500e0ed6eb408dd5566cdce47e0ad0f4945b2376.zip |
Code improvements
Diffstat (limited to 'src/index.ts')
-rw-r--r-- | src/index.ts | 58 |
1 files changed, 3 insertions, 55 deletions
diff --git a/src/index.ts b/src/index.ts index b083604..6e9ad2e 100644 --- a/src/index.ts +++ b/src/index.ts | |||
@@ -1,7 +1,5 @@ | |||
1 | import * as e621 from "./api/e621"; | ||
2 | import * as mastodon from "./api/mastodon"; | ||
3 | import config from "./config"; | 1 | import config from "./config"; |
4 | import Sharp from "sharp"; | 2 | import * as jobs from "./jobs"; |
5 | import * as cliArgs from "ts-command-line-args"; | 3 | import * as cliArgs from "ts-command-line-args"; |
6 | 4 | ||
7 | const args = cliArgs.parse<{ | 5 | const args = cliArgs.parse<{ |
@@ -17,56 +15,6 @@ const args = cliArgs.parse<{ | |||
17 | } | 15 | } |
18 | ); | 16 | ); |
19 | 17 | ||
20 | async function postRandomPicture() { | ||
21 | console.log("Fetching random post..."); | ||
22 | |||
23 | const queryIndex = Math.floor(Math.random() * config.e621.queries.length); | ||
24 | const query = config.e621.queries[queryIndex]; | ||
25 | const post = await e621.getRandomPost(query); | ||
26 | |||
27 | console.log(`Got ${post.id} via query ${queryIndex}`); | ||
28 | |||
29 | await handlePost(post); | ||
30 | } | ||
31 | |||
32 | async function postSpecificPicture(id: number) { | ||
33 | console.log(`Fetching post ${id}...`); | ||
34 | |||
35 | const post = await e621.getPostById(id); | ||
36 | |||
37 | console.log(`Got ${post.id}`); | ||
38 | |||
39 | await handlePost(post); | ||
40 | } | ||
41 | |||
42 | async function handlePost(post: e621.Post) { | ||
43 | const source = post.sources.length ? post.sources[0] : undefined; | ||
44 | const cws = config.cw.filter((w) => post.tags.general.includes(w)); | ||
45 | |||
46 | console.log(`Downloading image...`); | ||
47 | |||
48 | let file = await e621.client.get(post.file.url).buffer(); | ||
49 | |||
50 | if (Buffer.byteLength(file) > 1024 * 1024 * 9) { | ||
51 | console.log(`Compressing...`); | ||
52 | |||
53 | file = await Sharp(file) | ||
54 | .resize(1800, 1800, { fit: "inside", withoutEnlargement: true }) | ||
55 | .jpeg({ quality: 80, mozjpeg: true }) | ||
56 | .toBuffer(); | ||
57 | } | ||
58 | |||
59 | console.log(`Uploading...`); | ||
60 | |||
61 | const attachment = await mastodon.upload(file, post.id.toString(10)); | ||
62 | |||
63 | console.log(`Posting status...`); | ||
64 | |||
65 | const status = await mastodon.createStatus(`https://e926.net/posts/${post.id}`, source, cws, attachment.id); | ||
66 | |||
67 | console.log(`Done! ${status.url}`); | ||
68 | } | ||
69 | |||
70 | (async () => { | 18 | (async () => { |
71 | if (!config.mastodon.token) { | 19 | if (!config.mastodon.token) { |
72 | console.error("MASTODON_TOKEN not set"); | 20 | console.error("MASTODON_TOKEN not set"); |
@@ -74,8 +22,8 @@ async function handlePost(post: e621.Post) { | |||
74 | } | 22 | } |
75 | 23 | ||
76 | if (args.id) { | 24 | if (args.id) { |
77 | await postSpecificPicture(args.id); | 25 | await jobs.postSpecificPicture(args.id); |
78 | } else { | 26 | } else { |
79 | await postRandomPicture(); | 27 | await jobs.postRandomPicture(); |
80 | } | 28 | } |
81 | })(); | 29 | })(); |