From 500e0ed6eb408dd5566cdce47e0ad0f4945b2376 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 19 Oct 2021 17:32:48 +0200 Subject: Code improvements --- src/index.ts | 58 +++------------------------------------------------------- src/jobs.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 8 +++++--- 3 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 src/jobs.ts 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 @@ -import * as e621 from "./api/e621"; -import * as mastodon from "./api/mastodon"; import config from "./config"; -import Sharp from "sharp"; +import * as jobs from "./jobs"; import * as cliArgs from "ts-command-line-args"; const args = cliArgs.parse<{ @@ -17,56 +15,6 @@ const args = cliArgs.parse<{ } ); -async function postRandomPicture() { - console.log("Fetching random post..."); - - const queryIndex = Math.floor(Math.random() * config.e621.queries.length); - const query = config.e621.queries[queryIndex]; - const post = await e621.getRandomPost(query); - - console.log(`Got ${post.id} via query ${queryIndex}`); - - await handlePost(post); -} - -async function postSpecificPicture(id: number) { - console.log(`Fetching post ${id}...`); - - const post = await e621.getPostById(id); - - console.log(`Got ${post.id}`); - - await handlePost(post); -} - -async function handlePost(post: e621.Post) { - const source = post.sources.length ? post.sources[0] : undefined; - const cws = config.cw.filter((w) => post.tags.general.includes(w)); - - console.log(`Downloading image...`); - - let file = await e621.client.get(post.file.url).buffer(); - - if (Buffer.byteLength(file) > 1024 * 1024 * 9) { - console.log(`Compressing...`); - - file = await Sharp(file) - .resize(1800, 1800, { fit: "inside", withoutEnlargement: true }) - .jpeg({ quality: 80, 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}`); -} - (async () => { if (!config.mastodon.token) { console.error("MASTODON_TOKEN not set"); @@ -74,8 +22,8 @@ async function handlePost(post: e621.Post) { } if (args.id) { - await postSpecificPicture(args.id); + await jobs.postSpecificPicture(args.id); } else { - await postRandomPicture(); + await jobs.postRandomPicture(); } })(); 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 @@ +import * as e621 from "./api/e621"; +import * as mastodon from "./api/mastodon"; +import config from "./config"; +import Sharp from "sharp"; + +export async function postRandomPicture() { + console.log("Fetching random post..."); + + const queryIndex = Math.floor(Math.random() * config.e621.queries.length); + const query = config.e621.queries[queryIndex]; + const post = await e621.getRandomPost(query); + + console.log(`Got ${post.id} via query ${queryIndex}`); + + await handlePost(post); +} + +export async function postSpecificPicture(id: number) { + console.log(`Fetching post ${id}...`); + + const post = await e621.getPostById(id); + + console.log(`Got ${post.id}`); + + await handlePost(post); +} + +async function handlePost(post: e621.Post) { + const source = post.sources.length ? post.sources[0] : undefined; + const cws = config.cw.filter((w) => post.tags.general.includes(w)); + + console.log(`Downloading image...`); + + let file = await e621.client.get(post.file.url).buffer(); + + if (Buffer.byteLength(file) > 1024 * 1024 * 9) { + console.log(`Compressing...`); + + file = await Sharp(file) + .resize(1800, 1800, { fit: "inside", withoutEnlargement: true }) + .jpeg({ quality: 80, 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}`); +} diff --git a/tsconfig.json b/tsconfig.json index 75337a5..ad89216 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,10 @@ { "compilerOptions": { - "rootDirs": ["src"], - "target": "ES2020", + "lib": ["es2020"], + "module": "ESNext", "moduleResolution": "node", - "esModuleInterop": true + "target": "ES2020", + "esModuleInterop": true, + "rootDirs": ["src"] } } -- cgit v1.2.3-70-g09d2