summaryrefslogtreecommitdiffstats
path: root/src/jobs.ts
diff options
context:
space:
mode:
authorVolpeon <git@volpeon.ink>2021-10-19 17:32:48 +0200
committerVolpeon <git@volpeon.ink>2021-10-19 17:32:48 +0200
commit500e0ed6eb408dd5566cdce47e0ad0f4945b2376 (patch)
treed1c69cbaacc00887a09939e63dd8fadd8a605a91 /src/jobs.ts
parentCode improvements (diff)
downloadferalbot-500e0ed6eb408dd5566cdce47e0ad0f4945b2376.tar.gz
feralbot-500e0ed6eb408dd5566cdce47e0ad0f4945b2376.tar.bz2
feralbot-500e0ed6eb408dd5566cdce47e0ad0f4945b2376.zip
Code improvements
Diffstat (limited to 'src/jobs.ts')
-rw-r--r--src/jobs.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/jobs.ts b/src/jobs.ts
new file mode 100644
index 0000000..d77104f
--- /dev/null
+++ b/src/jobs.ts
@@ -0,0 +1,54 @@
1import * as e621 from "./api/e621";
2import * as mastodon from "./api/mastodon";
3import config from "./config";
4import Sharp from "sharp";
5
6export async function postRandomPicture() {
7 console.log("Fetching random post...");
8
9 const queryIndex = Math.floor(Math.random() * config.e621.queries.length);
10 const query = config.e621.queries[queryIndex];
11 const post = await e621.getRandomPost(query);
12
13 console.log(`Got ${post.id} via query ${queryIndex}`);
14
15 await handlePost(post);
16}
17
18export async function postSpecificPicture(id: number) {
19 console.log(`Fetching post ${id}...`);
20
21 const post = await e621.getPostById(id);
22
23 console.log(`Got ${post.id}`);
24
25 await handlePost(post);
26}
27
28async function handlePost(post: e621.Post) {
29 const source = post.sources.length ? post.sources[0] : undefined;
30 const cws = config.cw.filter((w) => post.tags.general.includes(w));
31
32 console.log(`Downloading image...`);
33
34 let file = await e621.client.get(post.file.url).buffer();
35
36 if (Buffer.byteLength(file) > 1024 * 1024 * 9) {
37 console.log(`Compressing...`);
38
39 file = await Sharp(file)
40 .resize(1800, 1800, { fit: "inside", withoutEnlargement: true })
41 .jpeg({ quality: 80, mozjpeg: true })
42 .toBuffer();
43 }
44
45 console.log(`Uploading...`);
46
47 const attachment = await mastodon.upload(file, post.id.toString(10));
48
49 console.log(`Posting status...`);
50
51 const status = await mastodon.createStatus(`https://e926.net/posts/${post.id}`, source, cws, attachment.id);
52
53 console.log(`Done! ${status.url}`);
54}