From 8a6ad69c1e859db373a67c75f893853986750603 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Mon, 18 Oct 2021 13:50:57 +0200 Subject: Support posting a specific picture --- src/index.ts | 64 +++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 18 deletions(-) (limited to 'src/index.ts') 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 @@ import * as e621 from "./api/e621"; import * as mastodon from "./api/mastodon"; import config from "./config"; +import * as cliArgs from 'ts-command-line-args'; -(async () => { - if (!config.mastodon.token) { - console.error("MASTODON_TOKEN not set"); - return; - } +const args = cliArgs.parse<{ + id?: number; + help?: boolean; +}>( + { + id: { type: Number, optional: true }, + help: { type: Boolean, optional: true, alias: 'h' }, + }, + { + helpArg: 'help', + }, +); + +async function postRandomPicture() { + console.log("Fetching random post..."); + + const { queryIndex, post } = await e621.getRandomPost(); + + 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.getPost(id); - console.log("Fetching post..."); + console.log(`Got ${post.id}`); + + await handlePost(post); +} - const { queryIndex, post } = await e621.randomPost(); +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(`Got ${post.id} via query ${queryIndex}`); 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)); @@ -38,4 +53,17 @@ import config from "./config"; 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"); + return; + } + + if (args.id) { + await postSpecificPicture(args.id); + } else { + await postRandomPicture(); + } })(); -- cgit v1.2.3-70-g09d2