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/api/e621/index.ts | 18 ++++++++++++++- src/config.ts | 2 +- src/index.ts | 64 ++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 64 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/api/e621/index.ts b/src/api/e621/index.ts index c8e0f44..411edae 100644 --- a/src/api/e621/index.ts +++ b/src/api/e621/index.ts @@ -26,7 +26,23 @@ export const client = got.extend({ }, }); -export async function randomPost() { +export async function getPost(id: number) { + const response = await client + .get("https://e926.net/posts.json", { + searchParams: { + tags: `id:${id}`, + }, + }) + .json<{ posts: readonly Post[] }>(); + + if (!response.posts.length) { + throw new Error("No posts received"); + } + + return response.posts[0]; +} + +export async function getRandomPost() { const queryIndex = Math.floor(Math.random() * config.e621.queries.length); const query = config.e621.queries[queryIndex]; const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1; diff --git a/src/config.ts b/src/config.ts index f6e7447..d854725 100644 --- a/src/config.ts +++ b/src/config.ts @@ -37,5 +37,5 @@ export default { instance: "https://botsin.space/", token: process.env.MASTODON_TOKEN, }, - cw: ["gun"], + cw: ["gun", "blood"], }; 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