summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/api/e621/index.ts18
-rw-r--r--src/config.ts2
-rw-r--r--src/index.ts64
3 files changed, 64 insertions, 20 deletions
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({
26 }, 26 },
27}); 27});
28 28
29export async function randomPost() { 29export async function getPost(id: number) {
30 const response = await client
31 .get("https://e926.net/posts.json", {
32 searchParams: {
33 tags: `id:${id}`,
34 },
35 })
36 .json<{ posts: readonly Post[] }>();
37
38 if (!response.posts.length) {
39 throw new Error("No posts received");
40 }
41
42 return response.posts[0];
43}
44
45export async function getRandomPost() {
30 const queryIndex = Math.floor(Math.random() * config.e621.queries.length); 46 const queryIndex = Math.floor(Math.random() * config.e621.queries.length);
31 const query = config.e621.queries[queryIndex]; 47 const query = config.e621.queries[queryIndex];
32 const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1; 48 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 {
37 instance: "https://botsin.space/", 37 instance: "https://botsin.space/",
38 token: process.env.MASTODON_TOKEN, 38 token: process.env.MASTODON_TOKEN,
39 }, 39 },
40 cw: ["gun"], 40 cw: ["gun", "blood"],
41}; 41};
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 @@
1import * as e621 from "./api/e621"; 1import * as e621 from "./api/e621";
2import * as mastodon from "./api/mastodon"; 2import * as mastodon from "./api/mastodon";
3import config from "./config"; 3import config from "./config";
4import * as cliArgs from 'ts-command-line-args';
4 5
5(async () => { 6const args = cliArgs.parse<{
6 if (!config.mastodon.token) { 7 id?: number;
7 console.error("MASTODON_TOKEN not set"); 8 help?: boolean;
8 return; 9}>(
9 } 10 {
11 id: { type: Number, optional: true },
12 help: { type: Boolean, optional: true, alias: 'h' },
13 },
14 {
15 helpArg: 'help',
16 },
17);
18
19async function postRandomPicture() {
20 console.log("Fetching random post...");
21
22 const { queryIndex, post } = await e621.getRandomPost();
23
24 console.log(`Got ${post.id} via query ${queryIndex}`);
25
26 await handlePost(post);
27}
28
29async function postSpecificPicture(id: number) {
30 console.log("Fetching post ${id}...");
31
32 const post = await e621.getPost(id);
10 33
11 console.log("Fetching post..."); 34 console.log(`Got ${post.id}`);
35
36 await handlePost(post);
37}
12 38
13 const { queryIndex, post } = await e621.randomPost(); 39async function handlePost(post: e621.Post) {
14 const source = post.sources.length ? post.sources[0] : undefined; 40 const source = post.sources.length ? post.sources[0] : undefined;
15 const cws = config.cw.filter((w) => post.tags.general.includes(w)); 41 const cws = config.cw.filter((w) => post.tags.general.includes(w));
16 42
17 console.log(`Got ${post.id} via query ${queryIndex}`);
18 console.log(`Downloading image...`); 43 console.log(`Downloading image...`);
19 44
20 const file = await e621.client.get(post.file.url).buffer(); 45 const file = await e621.client.get(post.file.url).buffer();
21 46
22 /*console.log(`Compressing...`);
23
24 const compressedFile = await sharp(file)
25 .resize(1000, 1000, {
26 fit: "inside",
27 withoutEnlargement: true,
28 })
29 .jpeg({ quality: 85, mozjpeg: true })
30 .toBuffer();*/
31
32 console.log(`Uploading...`); 47 console.log(`Uploading...`);
33 48
34 const attachment = await mastodon.upload(file, post.id.toString(10)); 49 const attachment = await mastodon.upload(file, post.id.toString(10));
@@ -38,4 +53,17 @@ import config from "./config";
38 const status = await mastodon.createStatus(`https://e926.net/posts/${post.id}`, source, cws, attachment.id); 53 const status = await mastodon.createStatus(`https://e926.net/posts/${post.id}`, source, cws, attachment.id);
39 54
40 console.log(`Done! ${status.url}`); 55 console.log(`Done! ${status.url}`);
56}
57
58(async () => {
59 if (!config.mastodon.token) {
60 console.error("MASTODON_TOKEN not set");
61 return;
62 }
63
64 if (args.id) {
65 await postSpecificPicture(args.id);
66 } else {
67 await postRandomPicture();
68 }
41})(); 69})();