mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-23 20:54:48 -04:00
[calendar][bot] threads
This commit is contained in:
parent
14c1f97d6e
commit
8b024153fa
@ -2,7 +2,8 @@ require('./src/dotenv')();
|
|||||||
|
|
||||||
import { loadSuml } from './server/loader';
|
import { loadSuml } from './server/loader';
|
||||||
import fs from 'fs';
|
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 config = loadSuml('config');
|
||||||
const translations = loadSuml('translations');
|
const translations = loadSuml('translations');
|
||||||
|
@ -1,34 +1,14 @@
|
|||||||
require('../src/dotenv')();
|
require('../src/dotenv')();
|
||||||
const Twitter = require('twitter');
|
const Twitter = require('twitter');
|
||||||
const Mastodon = require('mastodon');
|
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 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 locales = buildLocaleList('_');
|
||||||
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 = {
|
const publishers = {
|
||||||
async twitter(tweet, image) {
|
async twitter(tweet, image, previousId) {
|
||||||
const client = new Twitter({
|
const client = new Twitter({
|
||||||
consumer_key: process.env.TWITTER_CALENDAR_CONSUMER_KEY,
|
consumer_key: process.env.TWITTER_CALENDAR_CONSUMER_KEY,
|
||||||
consumer_secret: process.env.TWITTER_CALENDAR_CONSUMER_SECRET,
|
consumer_secret: process.env.TWITTER_CALENDAR_CONSUMER_SECRET,
|
||||||
@ -37,13 +17,18 @@ const publishers = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
try {
|
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);
|
console.log(tweetResponse);
|
||||||
|
|
||||||
|
return tweetResponse.id_str;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async mastodon(tweet, image) {
|
async mastodon(tweet, image, previousId) {
|
||||||
const client = new Mastodon({
|
const client = new Mastodon({
|
||||||
access_token: process.env.MASTODON_ACCESS_TOKEN,
|
access_token: process.env.MASTODON_ACCESS_TOKEN,
|
||||||
api_url: `https://${process.env.MASTODON_INSTANCE}/api/v1/`
|
api_url: `https://${process.env.MASTODON_INSTANCE}/api/v1/`
|
||||||
@ -52,7 +37,10 @@ const publishers = {
|
|||||||
const mediaIds = [];
|
const mediaIds = [];
|
||||||
if (image) {
|
if (image) {
|
||||||
try {
|
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);
|
console.log(mediaResponse);
|
||||||
mediaIds.push(mediaResponse.data.id);
|
mediaIds.push(mediaResponse.data.id);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -61,41 +49,56 @@ const publishers = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const tweetResponse = await client.post('statuses', { status: tweet, media_ids: mediaIds, visibility: 'unlisted' });
|
const tweetResponse = await client.post('statuses', {
|
||||||
console.log(tweetResponse);
|
status: tweet,
|
||||||
|
media_ids: mediaIds,
|
||||||
|
visibility: 'unlisted',
|
||||||
|
...(previousId ? {in_reply_to_id: previousId} : {}),
|
||||||
|
});
|
||||||
|
console.log(tweetResponse.data);
|
||||||
|
return tweetResponse.data.id;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
(async () => {
|
const tmpDir = `${__dirname}/../cache/tmp`;
|
||||||
const day = Day.today();
|
fs.mkdirSync(tmpDir, {recursive: true});
|
||||||
const events = calendar.getCurrentYear().eventsByDate[day.toString()];
|
const imageTmpPath = `${tmpDir}/calendar-tmp.png`;
|
||||||
console.log(events);
|
|
||||||
|
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
|
for (let locale of process.argv[2].split(',')) {
|
||||||
|
console.log('------------');
|
||||||
|
console.log(locales[locale].name);
|
||||||
|
|
||||||
let tweet = `[${language}] ${day.toString()}\n\n${translations.calendar.banner}:\n`;
|
const { day, message, image } = await (await fetch(locales[locale].url + '/api/calendar/today')).json();
|
||||||
for (let event of events) {
|
console.log('<<<', message, '>>>');
|
||||||
tweet += ` - ${getEventName(event.name)}\n`;
|
if (!message) { continue; }
|
||||||
}
|
|
||||||
tweet += `\n${domain}/${encodeURIComponent(config.calendar.route)}/${day}`;
|
|
||||||
|
|
||||||
let image = null;
|
fs.writeFileSync(imageTmpPath, Buffer.from(await (await fetch(image)).arrayBuffer()), {encoding: 'binary'});
|
||||||
|
let imageStream = null;
|
||||||
try {
|
try {
|
||||||
image = fs.createReadStream(`${__dirname}/../static/calendar/${day}.png`);
|
imageStream = fs.createReadStream(imageTmpPath);
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
console.log('------------');
|
for (let publisher of process.argv[3].split(',')) {
|
||||||
console.log(tweet);
|
|
||||||
console.log('------------');
|
|
||||||
|
|
||||||
for (let publisher of process.argv.slice(2)) {
|
|
||||||
console.log('Publishing: ' + publisher);
|
console.log('Publishing: ' + publisher);
|
||||||
publishers[publisher](tweet, image);
|
const postId = await publishers[publisher](
|
||||||
console.log('------------');
|
message,
|
||||||
|
imageStream,
|
||||||
|
lastPostId[publisher]
|
||||||
|
);
|
||||||
|
console.log(postId);
|
||||||
|
lastPostId[publisher] = postId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
@ -6,7 +6,8 @@ import cookieParser from 'cookie-parser';
|
|||||||
import grant from "grant";
|
import grant from "grant";
|
||||||
import router from "./routes/user";
|
import router from "./routes/user";
|
||||||
import { loadSuml } from './loader';
|
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 cookieSettings from "../src/cookieSettings";
|
||||||
import SQL from "sql-template-strings";
|
import SQL from "sql-template-strings";
|
||||||
|
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import SQL from 'sql-template-strings';
|
import SQL from 'sql-template-strings';
|
||||||
import avatar from '../avatar';
|
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 locales from '../../src/locales';
|
||||||
import {calculateStats, statsFile} from '../../src/stats';
|
import {calculateStats, statsFile} from '../../src/stats';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { caches } from "../../src/cache";
|
import { caches } from "../../src/cache";
|
||||||
import mailer from "../../src/mailer";
|
import mailer from "../../src/mailer";
|
||||||
import {profilesSnapshot} from "./profile";
|
import {profilesSnapshot} from "./profile";
|
||||||
|
import buildLocaleList from "../../src/buildLocaleList";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import {buildLocaleList, handleErrorAsync} from "../../src/helpers";
|
import {handleErrorAsync} from "../../src/helpers";
|
||||||
|
import buildLocaleList from "../../src/buildLocaleList";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import SQL from 'sql-template-strings';
|
import SQL from 'sql-template-strings';
|
||||||
import {ulid} from "ulid";
|
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 jwt from "../../src/jwt";
|
||||||
import mailer from "../../src/mailer";
|
import mailer from "../../src/mailer";
|
||||||
import { loadSuml } from '../loader';
|
import { loadSuml } from '../loader';
|
||||||
@ -11,6 +11,7 @@ import cookieSettings from "../../src/cookieSettings";
|
|||||||
import {validateCaptcha} from "../captcha";
|
import {validateCaptcha} from "../captcha";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import {addMfaInfo} from './mfa';
|
import {addMfaInfo} from './mfa';
|
||||||
|
import buildLocaleList from "../../src/buildLocaleList";
|
||||||
|
|
||||||
const config = loadSuml('config');
|
const config = loadSuml('config');
|
||||||
const translations = loadSuml('translations');
|
const translations = loadSuml('translations');
|
||||||
|
@ -4,24 +4,7 @@ const dbConnection = require('./db');
|
|||||||
const {calculateStats, statsFile} = require('../src/stats');
|
const {calculateStats, statsFile} = require('../src/stats');
|
||||||
const locales = require('../src/locales');
|
const locales = require('../src/locales');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const buildLocaleList = require('../src/buildLocaleList');
|
||||||
// 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}];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async function calculate() {
|
async function calculate() {
|
||||||
const db = await dbConnection();
|
const db = await dbConnection();
|
||||||
|
11
src/buildLocaleList.js
Normal file
11
src/buildLocaleList.js
Normal file
@ -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 md5 from 'js-md5';
|
||||||
import { Base64 } from 'js-base64';
|
import {Base64} from 'js-base64';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import locales from './locales';
|
|
||||||
|
|
||||||
export const buildDict = (fn, ...args) => {
|
export const buildDict = (fn, ...args) => {
|
||||||
const dict = {};
|
const dict = {};
|
||||||
@ -150,16 +149,6 @@ export const isTroll = (body) => {
|
|||||||
return ['cipeusz', 'feminazi', 'bruksela', 'zboczeń'].some(t => body.indexOf(t) > -1);
|
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) => {
|
export const zip = (list, reverse) => {
|
||||||
return buildDict(function* () {
|
return buildDict(function* () {
|
||||||
for (let [k, v] of list) {
|
for (let [k, v] of list) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user