[calendar][bot] mastodon support
This commit is contained in:
parent
532bcbd697
commit
37204f059d
|
@ -37,3 +37,5 @@ TWITTER_CALENDAR_CONSUMER_KEY=
|
|||
TWITTER_CALENDAR_CONSUMER_SECRET=
|
||||
TWITTER_CALENDAR_ACCESS_TOKEN_KEY=
|
||||
TWITTER_CALENDAR_ACCESS_TOKEN_SECRET=
|
||||
|
||||
MASTODON_ACCESS_TOKEN=
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
"luxon": "^1.25.0",
|
||||
"mailer": "^0.6.7",
|
||||
"markdown-loader": "^6.0.0",
|
||||
"mastodon": "^1.2.2",
|
||||
"multer": "^1.4.2",
|
||||
"node-fetch": "^2.6.1",
|
||||
"nuxt": "^2.15.2",
|
||||
|
|
|
@ -0,0 +1,101 @@
|
|||
require('../src/dotenv')();
|
||||
const Twitter = require('twitter');
|
||||
const Mastodon = require('mastodon');
|
||||
const Suml = require('suml');
|
||||
const fs = require('fs');
|
||||
const { calendar } = require('../src/calendar/calendar');
|
||||
const { Day } = require('../src/calendar/helpers');
|
||||
const locales = require('../src/locales');
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
const loadSuml = name => new Suml().parse(fs.readFileSync(`${__dirname}/../data/${name}.suml`).toString());
|
||||
const translations = loadSuml('translations');
|
||||
const config = loadSuml('config');
|
||||
|
||||
let domain = null;
|
||||
let language = null;
|
||||
for (let [code, name, url, ] of locales) {
|
||||
if (code === config.locale) {
|
||||
domain = url;
|
||||
language = name;
|
||||
}
|
||||
}
|
||||
|
||||
const getEventName = (name) => {
|
||||
name = translations.calendar.events[name] || name;
|
||||
name = name.replace(/{.*?=(.*?)}/g, '$1')
|
||||
return name;
|
||||
}
|
||||
|
||||
const publishers = {
|
||||
async twitter(tweet, image) {
|
||||
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 {
|
||||
const tweetResponse = await client.post('statuses/update', {status: tweet});
|
||||
console.log(tweetResponse);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
async mastodon(tweet, image) {
|
||||
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 {
|
||||
const mediaResponse = await client.post('media', { file: image, description: 'Screenshot of the link above' });
|
||||
console.log(mediaResponse);
|
||||
mediaIds.push(mediaResponse.data.id);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const tweetResponse = await client.post('statuses', { status: tweet, media_ids: mediaIds });
|
||||
console.log(tweetResponse);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
(async () => {
|
||||
const day = Day.today();
|
||||
const events = calendar.getCurrentYear().eventsByDate[day.toString()];
|
||||
console.log(events);
|
||||
|
||||
if (events === undefined || events.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let tweet = `[${language}] ${day.toString()}\n\n${translations.calendar.banner}:\n`;
|
||||
for (let event of events) {
|
||||
tweet += ` - ${getEventName(event.name)}\n`;
|
||||
}
|
||||
tweet += `\n${domain}/${encodeURIComponent(config.calendar.route)}/${day}`;
|
||||
|
||||
let image = null;
|
||||
try {
|
||||
image = fs.createReadStream(`${__dirname}/../static/calendar/${day}.png`);
|
||||
} catch {}
|
||||
|
||||
console.log('------------');
|
||||
console.log(tweet);
|
||||
console.log('------------');
|
||||
|
||||
for (let publisher of process.argv.slice(2)) {
|
||||
console.log('Publishing: ' + publisher);
|
||||
publishers[publisher](tweet, image);
|
||||
console.log('------------');
|
||||
}
|
||||
})();
|
|
@ -1,60 +0,0 @@
|
|||
require('../src/dotenv')();
|
||||
const Twitter = require('twitter');
|
||||
const Suml = require('suml');
|
||||
const fs = require('fs');
|
||||
const { calendar } = require('../src/calendar/calendar');
|
||||
const { Day } = require('../src/calendar/helpers');
|
||||
const locales = require('../src/locales');
|
||||
|
||||
const loadSuml = name => new Suml().parse(fs.readFileSync(`${__dirname}/../data/${name}.suml`).toString());
|
||||
const translations = loadSuml('translations');
|
||||
const config = loadSuml('config');
|
||||
|
||||
let domain = null;
|
||||
let language = null;
|
||||
for (let [code, name, url, ] of locales) {
|
||||
if (code === config.locale) {
|
||||
domain = url;
|
||||
language = name;
|
||||
}
|
||||
}
|
||||
|
||||
const getEventName = (name) => {
|
||||
name = translations.calendar.events[name] || name;
|
||||
name = name.replace(/{.*?=(.*?)}/g, '$1')
|
||||
return name;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const day = Day.today();
|
||||
const events = calendar.getCurrentYear().eventsByDate[day.toString()];
|
||||
console.log(events);
|
||||
|
||||
if (events === undefined || events.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
let tweet = `[${language}] ${day.toString()}\n\n${translations.calendar.banner}:\n`;
|
||||
for (let event of events) {
|
||||
tweet += ` - ${getEventName(event.name)}\n`;
|
||||
}
|
||||
tweet += `\n${domain}/${encodeURIComponent(config.calendar.route)}/${day}`;
|
||||
|
||||
console.log('------------');
|
||||
console.log(tweet);
|
||||
console.log('------------');
|
||||
|
||||
try {
|
||||
const tweetResponse = await client.post('statuses/update', {status: tweet});
|
||||
console.log(tweetResponse);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
})();
|
15
yarn.lock
15
yarn.lock
|
@ -2221,7 +2221,7 @@ block-stream@*:
|
|||
dependencies:
|
||||
inherits "~2.0.0"
|
||||
|
||||
bluebird@^3.1.1, bluebird@^3.5.5:
|
||||
bluebird@^3.1.1, bluebird@^3.1.5, bluebird@^3.5.5:
|
||||
version "3.7.2"
|
||||
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
|
||||
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==
|
||||
|
@ -6163,6 +6163,15 @@ marked@^0.7.0:
|
|||
resolved "https://registry.yarnpkg.com/marked/-/marked-0.7.0.tgz#b64201f051d271b1edc10a04d1ae9b74bb8e5c0e"
|
||||
integrity sha512-c+yYdCZJQrsRjTPhUx7VKkApw9bwDkNbHUKo1ovgcfDjb2kc8rLuRbIFyXL5WOEUwzSSKo3IXpph2K6DqB/KZg==
|
||||
|
||||
mastodon@^1.2.2:
|
||||
version "1.2.2"
|
||||
resolved "https://registry.yarnpkg.com/mastodon/-/mastodon-1.2.2.tgz#e3a99b21af7ad37b20a5ca6583c9d76ac7716fe5"
|
||||
integrity sha512-ixcYkzn6SorH8U2jNc1vwiX89EiVMjzd2aDYFtr191YY9rdoVo+owI6cQo2EjUnzg2RN9WxyBJ9KDuw+R4lt+w==
|
||||
dependencies:
|
||||
bluebird "^3.1.5"
|
||||
mime "^1.3.4"
|
||||
request "^2.68.0"
|
||||
|
||||
md5.js@^1.3.4:
|
||||
version "1.3.5"
|
||||
resolved "https://registry.yarnpkg.com/md5.js/-/md5.js-1.3.5.tgz#b5d07b8e3216e3e27cd728d72f70d1e6a342005f"
|
||||
|
@ -6325,7 +6334,7 @@ mime-types@^2.1.12, mime-types@^2.1.19, mime-types@^2.1.27, mime-types@^2.1.28,
|
|||
dependencies:
|
||||
mime-db "1.46.0"
|
||||
|
||||
mime@1.6.0:
|
||||
mime@1.6.0, mime@^1.3.4:
|
||||
version "1.6.0"
|
||||
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
|
||||
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==
|
||||
|
@ -8590,7 +8599,7 @@ request-oauth@^1.0.0:
|
|||
qs "^6.9.6"
|
||||
uuid "^8.3.2"
|
||||
|
||||
request@^2.72.0, request@^2.87.0:
|
||||
request@^2.68.0, request@^2.72.0, request@^2.87.0:
|
||||
version "2.88.2"
|
||||
resolved "https://registry.yarnpkg.com/request/-/request-2.88.2.tgz#d73c918731cb5a87da047e207234146f664d12b3"
|
||||
integrity sha512-MsvtOrfG9ZcrOwAW+Qi+F6HbD0CWXEh9ou77uOb7FM2WPhwT7smM833PzanhJLsgXjN89Ir6V2PczXNnMpwKhw==
|
||||
|
|
Reference in New Issue