This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/server/calendarBot.js

105 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-12-16 02:06:16 -08:00
require('../src/dotenv')();
const Twitter = require('twitter');
const Mastodon = require('mastodon');
const fetch = require('node-fetch');
2022-01-03 10:22:10 -08:00
const fs = require('fs');
const buildLocaleList = require('../src/buildLocaleList');
2021-12-16 02:06:16 -08:00
2022-01-03 10:22:10 -08:00
const locales = buildLocaleList('_');
2021-12-16 02:06:16 -08:00
const publishers = {
2022-01-03 10:22:10 -08:00
async twitter(tweet, image, previousId) {
2021-12-16 02:06:16 -08:00
const client = new Twitter({
consumer_key: process.env.TWITTER_CALENDAR_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CALENDAR_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_CALENDAR_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_CALENDAR_ACCESS_TOKEN_SECRET,
});
try {
2022-01-03 10:22:10 -08:00
const tweetResponse = await client.post('statuses/update', {
status: tweet,
...(previousId ? {in_reply_to_status_id: previousId} : {}),
});
2021-12-16 02:06:16 -08:00
console.log(tweetResponse);
2022-01-03 10:22:10 -08:00
return tweetResponse.id_str;
2021-12-16 02:06:16 -08:00
} catch (error) {
console.error(error);
}
},
2022-01-03 10:22:10 -08:00
async mastodon(tweet, image, previousId) {
2021-12-16 02:06:16 -08:00
const client = new Mastodon({
access_token: process.env.MASTODON_ACCESS_TOKEN,
api_url: `https://${process.env.MASTODON_INSTANCE}/api/v1/`
});
const mediaIds = [];
if (image) {
try {
2022-01-03 10:22:10 -08:00
const mediaResponse = await client.post('media', {
file: image,
description: 'Screenshot of the link above'
});
2021-12-16 02:06:16 -08:00
console.log(mediaResponse);
mediaIds.push(mediaResponse.data.id);
} catch (error) {
console.error(error);
}
}
try {
2022-01-03 10:22:10 -08:00
const tweetResponse = await client.post('statuses', {
status: tweet,
media_ids: mediaIds,
visibility: 'unlisted',
...(previousId ? {in_reply_to_id: previousId} : {}),
});
console.log(tweetResponse.data);
return tweetResponse.data.id;
2021-12-16 02:06:16 -08:00
} catch (error) {
console.error(error);
}
},
};
2022-01-03 10:22:10 -08:00
const tmpDir = `${__dirname}/../cache/tmp`;
fs.mkdirSync(tmpDir, {recursive: true});
const imageTmpPath = `${tmpDir}/calendar-tmp.png`;
2021-12-16 02:06:16 -08:00
2022-01-03 10:22:10 -08:00
const lastPostId = {};
const timer = ms => new Promise( res => setTimeout(res, ms));
2021-12-16 02:06:16 -08:00
2022-01-03 10:22:10 -08:00
(async () => {
if (process.argv.length !== 4) {
console.error('Missing parameters. Usage: node server/calendarBot.js <locales> <publishers>');
return;
2021-12-16 02:06:16 -08:00
}
2022-01-03 10:22:10 -08:00
for (let locale of process.argv[2].split(',')) {
console.log('------------');
console.log(locales[locale].name);
2021-12-16 02:06:16 -08:00
2022-01-03 10:22:10 -08:00
const { day, message, image } = await (await fetch(locales[locale].url + '/api/calendar/today')).json();
console.log('<<<', message, '>>>');
if (!message) { continue; }
2021-12-16 02:06:16 -08:00
2022-01-03 10:22:10 -08:00
fs.writeFileSync(imageTmpPath, Buffer.from(await (await fetch(image)).arrayBuffer()), {encoding: 'binary'});
let imageStream = null;
try {
imageStream = fs.createReadStream(imageTmpPath);
} catch {}
2021-12-16 02:06:16 -08:00
2022-01-03 10:22:10 -08:00
for (let publisher of process.argv[3].split(',')) {
console.log('Publishing: ' + publisher);
const postId = await publishers[publisher](
message,
imageStream,
lastPostId[publisher]
);
console.log(postId);
lastPostId[publisher] = postId;
}
2021-12-16 02:06:16 -08:00
}
})();