-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (49 loc) · 1.53 KB
/
index.js
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
import * as dotenv from "dotenv";
import fs from "node:fs";
import mastodonApi from "mastodon";
import random from "random";
dotenv.config();
const datalist = JSON.parse(fs.readFileSync("./data.json"));
const imageMetadata = random.choice(datalist);
const imageAltText =
imageMetadata.image_description?.trim() || imageMetadata.common_name?.trim();
const imageFile = fs.createReadStream("./images/" + imageMetadata.file);
console.log({ imageMetadata });
const M = new mastodonApi({
access_token: process.env.MASTO_ACCESS_TOKEN,
api_url: process.env.MASTO_API_ENDPOINT,
});
let status = `${imageMetadata.common_name.trim()} `;
if (
imageMetadata.scientific_name &&
imageMetadata.common_name !== imageMetadata.scientific_name
) {
status += `(${imageMetadata.scientific_name.trim()}) `;
}
status += `\n\n📸 ${imageMetadata.attribution.trim()}`;
if (!imageMetadata.image_description) {
status += `\n\n💬 Suggest alt text: https://github.com/querkmachine/bat-bot/issues/new?labels=image+description&template=image-description.yml&title=Image+description%3A+${encodeURI(
imageMetadata.file
)}&filename=${encodeURI(imageMetadata.file)}`;
}
M.post("media", {
file: imageFile,
description: imageAltText,
})
.then((res) => {
const id = res.data.id;
M.post("statuses", {
status: status,
media_ids: [id],
visibility: "unlisted",
})
.then((res) => {
console.log(res.data);
})
.catch((err) => {
throw new Error(err);
});
})
.catch((err) => {
throw new Error(err);
});