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/calendarTwitter.js

61 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-09-16 03:52:10 -07:00
require('../src/dotenv')();
const Twitter = require('twitter');
const Suml = require('suml');
const fs = require('fs');
const { calendar } = require('../src/calendar/calendar');
2021-09-16 03:52:10 -07:00
const { Day } = require('../src/calendar/helpers');
const locales = require('../src/locales');
2021-09-16 04:28:10 -07:00
const loadSuml = name => new Suml().parse(fs.readFileSync(`${__dirname}/../data/${name}.suml`).toString());
2021-09-16 03:52:10 -07:00
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')
2021-09-16 03:52:10 -07:00
return name;
}
(async () => {
const day = Day.today();
const events = calendar.getCurrentYear().eventsByDate[day.toString()];
2021-09-16 03:52:10 -07:00
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`;
}
2021-12-05 01:46:42 -08:00
tweet += `\n${domain}/${encodeURIComponent(config.calendar.route)}/${day}`;
2021-09-16 03:52:10 -07:00
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);
}
})();