summaryrefslogtreecommitdiffstats
path: root/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.ts')
-rw-r--r--src/index.ts24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/index.ts b/src/index.ts
index 3b8b0a8..e7bb328 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,7 +1,8 @@
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'; 4import Sharp from "sharp";
5import * as cliArgs from "ts-command-line-args";
5 6
6const args = cliArgs.parse<{ 7const args = cliArgs.parse<{
7 id?: number; 8 id?: number;
@@ -9,11 +10,11 @@ const args = cliArgs.parse<{
9}>( 10}>(
10 { 11 {
11 id: { type: Number, optional: true }, 12 id: { type: Number, optional: true },
12 help: { type: Boolean, optional: true, alias: 'h' }, 13 help: { type: Boolean, optional: true, alias: "h" },
13 }, 14 },
14 { 15 {
15 helpArg: 'help', 16 helpArg: "help",
16 }, 17 }
17); 18);
18 19
19async function postRandomPicture() { 20async function postRandomPicture() {
@@ -24,7 +25,7 @@ async function postRandomPicture() {
24 const post = await e621.getRandomPost(query); 25 const post = await e621.getRandomPost(query);
25 26
26 console.log(`Got ${post.id} via query ${queryIndex}`); 27 console.log(`Got ${post.id} via query ${queryIndex}`);
27 28
28 await handlePost(post); 29 await handlePost(post);
29} 30}
30 31
@@ -34,7 +35,7 @@ async function postSpecificPicture(id: number) {
34 const post = await e621.getPostById(id); 35 const post = await e621.getPostById(id);
35 36
36 console.log(`Got ${post.id}`); 37 console.log(`Got ${post.id}`);
37 38
38 await handlePost(post); 39 await handlePost(post);
39} 40}
40 41
@@ -44,7 +45,16 @@ async function handlePost(post: e621.Post) {
44 45
45 console.log(`Downloading image...`); 46 console.log(`Downloading image...`);
46 47
47 const file = await e621.client.get(post.file.url).buffer(); 48 let file = await e621.client.get(post.file.url).buffer();
49
50 if (Buffer.byteLength(file) > 1024 * 1024 * 9) {
51 console.log(`Compressing...`);
52
53 file = await Sharp(file)
54 .resize(1000, 1000, { fit: "inside", withoutEnlargement: true })
55 .jpeg({ quality: 80, mozjpeg: true })
56 .toBuffer();
57 }
48 58
49 console.log(`Uploading...`); 59 console.log(`Uploading...`);
50 60