diff options
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 64 |
1 files changed, 46 insertions, 18 deletions
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 @@ | |||
| 1 | import * as e621 from "./api/e621"; | 1 | import * as e621 from "./api/e621"; |
| 2 | import * as mastodon from "./api/mastodon"; | 2 | import * as mastodon from "./api/mastodon"; |
| 3 | import config from "./config"; | 3 | import config from "./config"; |
| 4 | import * as cliArgs from 'ts-command-line-args'; | ||
| 4 | 5 | ||
| 5 | (async () => { | 6 | const 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 | |||
| 19 | async 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 | |||
| 29 | async 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(); | 39 | async 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 | })(); |
