[calendar][bot] threads
This commit is contained in:
parent
f4deeadd7a
commit
e1e1fa7336
|
@ -2,7 +2,8 @@ require('./src/dotenv')();
|
|||
|
||||
import { loadSuml } from './server/loader';
|
||||
import fs from 'fs';
|
||||
import {buildDict, buildList, buildLocaleList} from "./src/helpers";
|
||||
import {buildDict, buildList} from "./src/helpers";
|
||||
import buildLocaleList from "./src/buildLocaleList";
|
||||
|
||||
const config = loadSuml('config');
|
||||
const translations = loadSuml('translations');
|
||||
|
|
|
@ -1,34 +1,14 @@
|
|||
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 fs = require('fs');
|
||||
const buildLocaleList = require('../src/buildLocaleList');
|
||||
|
||||
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 locales = buildLocaleList('_');
|
||||
|
||||
const publishers = {
|
||||
async twitter(tweet, image) {
|
||||
async twitter(tweet, image, previousId) {
|
||||
const client = new Twitter({
|
||||
consumer_key: process.env.TWITTER_CALENDAR_CONSUMER_KEY,
|
||||
consumer_secret: process.env.TWITTER_CALENDAR_CONSUMER_SECRET,
|
||||
|
@ -37,13 +17,18 @@ const publishers = {
|
|||
});
|
||||
|
||||
try {
|
||||
const tweetResponse = await client.post('statuses/update', {status: tweet});
|
||||
const tweetResponse = await client.post('statuses/update', {
|
||||
status: tweet,
|
||||
...(previousId ? {in_reply_to_status_id: previousId} : {}),
|
||||
});
|
||||
console.log(tweetResponse);
|
||||
|
||||
return tweetResponse.id_str;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
async mastodon(tweet, image) {
|
||||
async mastodon(tweet, image, previousId) {
|
||||
const client = new Mastodon({
|
||||
access_token: process.env.MASTODON_ACCESS_TOKEN,
|
||||
api_url: `https://${process.env.MASTODON_INSTANCE}/api/v1/`
|
||||
|
@ -52,7 +37,10 @@ const publishers = {
|
|||
const mediaIds = [];
|
||||
if (image) {
|
||||
try {
|
||||
const mediaResponse = await client.post('media', { file: image, description: 'Screenshot of the link above' });
|
||||
const mediaResponse = await client.post('media', {
|
||||
file: image,
|
||||
description: 'Screenshot of the link above'
|
||||
});
|
||||
console.log(mediaResponse);
|
||||
mediaIds.push(mediaResponse.data.id);
|
||||
} catch (error) {
|
||||
|
@ -61,41 +49,56 @@ const publishers = {
|
|||
}
|
||||
|
||||
try {
|
||||
const tweetResponse = await client.post('statuses', { status: tweet, media_ids: mediaIds, visibility: 'unlisted' });
|
||||
console.log(tweetResponse);
|
||||
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;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
(async () => {
|
||||
const day = Day.today();
|
||||
const events = calendar.getCurrentYear().eventsByDate[day.toString()];
|
||||
console.log(events);
|
||||
const tmpDir = `${__dirname}/../cache/tmp`;
|
||||
fs.mkdirSync(tmpDir, {recursive: true});
|
||||
const imageTmpPath = `${tmpDir}/calendar-tmp.png`;
|
||||
|
||||
if (events === undefined || events.length === 0) {
|
||||
const lastPostId = {};
|
||||
|
||||
const timer = ms => new Promise( res => setTimeout(res, ms));
|
||||
|
||||
(async () => {
|
||||
if (process.argv.length !== 4) {
|
||||
console.error('Missing parameters. Usage: node server/calendarBot.js <locales> <publishers>');
|
||||
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);
|
||||
for (let locale of process.argv[2].split(',')) {
|
||||
console.log('------------');
|
||||
console.log(locales[locale].name);
|
||||
|
||||
const { day, message, image } = await (await fetch(locales[locale].url + '/api/calendar/today')).json();
|
||||
console.log('<<<', message, '>>>');
|
||||
if (!message) { continue; }
|
||||
|
||||
fs.writeFileSync(imageTmpPath, Buffer.from(await (await fetch(image)).arrayBuffer()), {encoding: 'binary'});
|
||||
let imageStream = null;
|
||||
try {
|
||||
imageStream = fs.createReadStream(imageTmpPath);
|
||||
} catch {}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
|
|
@ -6,7 +6,8 @@ import cookieParser from 'cookie-parser';
|
|||
import grant from "grant";
|
||||
import router from "./routes/user";
|
||||
import { loadSuml } from './loader';
|
||||
import {isGranted, buildLocaleList} from "../src/helpers";
|
||||
import {isGranted} from "../src/helpers";
|
||||
import buildLocaleList from "../src/buildLocaleList";
|
||||
import cookieSettings from "../src/cookieSettings";
|
||||
import SQL from "sql-template-strings";
|
||||
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
import { Router } from 'express';
|
||||
import SQL from 'sql-template-strings';
|
||||
import avatar from '../avatar';
|
||||
import {buildDict, now, shuffle, handleErrorAsync, buildLocaleList} from "../../src/helpers";
|
||||
import {buildDict, now, shuffle, handleErrorAsync} from "../../src/helpers";
|
||||
import locales from '../../src/locales';
|
||||
import {calculateStats, statsFile} from '../../src/stats';
|
||||
import fs from 'fs';
|
||||
import { caches } from "../../src/cache";
|
||||
import mailer from "../../src/mailer";
|
||||
import {profilesSnapshot} from "./profile";
|
||||
import buildLocaleList from "../../src/buildLocaleList";
|
||||
|
||||
const router = Router();
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { Router } from 'express';
|
||||
import {buildLocaleList, handleErrorAsync} from "../../src/helpers";
|
||||
import {handleErrorAsync} from "../../src/helpers";
|
||||
import buildLocaleList from "../../src/buildLocaleList";
|
||||
|
||||
const router = Router();
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Router } from 'express';
|
||||
import SQL from 'sql-template-strings';
|
||||
import {ulid} from "ulid";
|
||||
import {buildDict, makeId, now, handleErrorAsync, buildLocaleList} from "../../src/helpers";
|
||||
import {buildDict, makeId, now, handleErrorAsync} from "../../src/helpers";
|
||||
import jwt from "../../src/jwt";
|
||||
import mailer from "../../src/mailer";
|
||||
import { loadSuml } from '../loader';
|
||||
|
@ -11,6 +11,7 @@ import cookieSettings from "../../src/cookieSettings";
|
|||
import {validateCaptcha} from "../captcha";
|
||||
import assert from "assert";
|
||||
import {addMfaInfo} from './mfa';
|
||||
import buildLocaleList from "../../src/buildLocaleList";
|
||||
|
||||
const config = loadSuml('config');
|
||||
const translations = loadSuml('translations');
|
||||
|
|
|
@ -4,24 +4,7 @@ const dbConnection = require('./db');
|
|||
const {calculateStats, statsFile} = require('../src/stats');
|
||||
const locales = require('../src/locales');
|
||||
const fs = require('fs');
|
||||
|
||||
// TODO duplication
|
||||
const buildDict = (fn, ...args) => {
|
||||
const dict = {};
|
||||
for (let [key, value] of fn(...args)) {
|
||||
dict[key] = value;
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
const buildLocaleList = () => {
|
||||
return buildDict(function* () {
|
||||
for (let [code, name, url, published] of locales) {
|
||||
if (published) {
|
||||
yield [code, {name, url, published}];
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
const buildLocaleList = require('../src/buildLocaleList');
|
||||
|
||||
async function calculate() {
|
||||
const db = await dbConnection();
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
const locales = require('./locales')
|
||||
|
||||
module.exports = (current, includeUnpublished = false) => {
|
||||
const d = {};
|
||||
for (let [code, name, url, published] of locales) {
|
||||
if (published || current === code || includeUnpublished) {
|
||||
d[code] = {name, url, published, code};
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
import md5 from 'js-md5';
|
||||
import { Base64 } from 'js-base64';
|
||||
import {Base64} from 'js-base64';
|
||||
import _ from 'lodash';
|
||||
import locales from './locales';
|
||||
|
||||
export const buildDict = (fn, ...args) => {
|
||||
const dict = {};
|
||||
|
@ -150,16 +149,6 @@ export const isTroll = (body) => {
|
|||
return ['cipeusz', 'feminazi', 'bruksela', 'zboczeń'].some(t => body.indexOf(t) > -1);
|
||||
}
|
||||
|
||||
export const buildLocaleList = (current, includeUnpublished = false) => {
|
||||
return buildDict(function* () {
|
||||
for (let [code, name, url, published] of locales) {
|
||||
if (published || current === code || includeUnpublished) {
|
||||
yield [code, {name, url, published, code}];
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const zip = (list, reverse) => {
|
||||
return buildDict(function* () {
|
||||
for (let [k, v] of list) {
|
||||
|
|
Reference in New Issue