blob: bb0c3e16b5cbf42da07b25dbec207d3ae8bc85f9 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import * as e621 from "../api/e926";
import * as mastodon from "../api/mastodon";
import config from "../config";
import { imageSize } from "image-size";
import Sharp from "sharp";
export async function postSpecificPicture(id: number) {
console.log(`Fetching post ${id}...`);
const post = await e621.getPostById(id);
console.log(`Got ${post.id}`);
await handlePost(post);
}
export async function postRandomPicture() {
console.log("Fetching random post...");
const queryIndex = Math.floor(Math.random() * config.e621.queries.length);
const query = config.e621.queries[queryIndex];
const post = await e621.getRandomPost(query);
console.log(`Got ${post.id} via query ${queryIndex}`);
await handlePost(post);
}
async function handlePost(post: e621.Post) {
const cws = config.cw.filter((w) => post.tags.general.includes(w));
console.log(`Downloading image...`);
let file = await e621.client.get(post.file.url).buffer();
const dims = imageSize(file);
const width = dims.width ?? 0;
const height = dims.height ?? 0;
if (Buffer.byteLength(file) > 1024 * 1024 * 9 || width > 2000 || height > 2000) {
console.log(`Compressing...`);
file = await Sharp(file)
.resize(2000, 2000, { fit: "inside", withoutEnlargement: true })
.jpeg({ quality: 80, mozjpeg: true })
.toBuffer();
}
console.log(`Uploading...`);
const attachment = await mastodon.upload(file, post.id.toString(10));
console.log(`Posting status...`);
const status = await mastodon.createStatus(`https://e926.net/posts/${post.id}`, post.sources, cws, attachment.id);
console.log(`Done! ${status.url}`);
}
|