2021-09-07 10:11:15 -07:00
|
|
|
require('../src/dotenv')();
|
|
|
|
|
2021-08-18 05:39:08 -07:00
|
|
|
const Pageres = require('pageres');
|
|
|
|
const fs = require('fs');
|
2021-09-25 07:25:40 -07:00
|
|
|
const dbConnection = require('./db');
|
|
|
|
const Suml = require('suml');
|
2021-10-03 15:19:23 -07:00
|
|
|
const { calendar } = require('../src/calendar/calendar');
|
|
|
|
const md5 = require('js-md5');
|
2021-09-25 07:25:40 -07:00
|
|
|
|
2021-10-04 11:31:51 -07:00
|
|
|
const loadSuml = name => new Suml().parse(fs.readFileSync(`${__dirname}/../data/${name}.suml`).toString());
|
2021-09-25 07:25:40 -07:00
|
|
|
const config = loadSuml('config');
|
2021-08-18 05:39:08 -07:00
|
|
|
|
2021-10-04 14:05:38 -07:00
|
|
|
const dir = `${__dirname}/../static/calendar`;
|
|
|
|
|
2021-10-04 14:43:56 -07:00
|
|
|
const force = process.argv[2] === '-f' || process.argv[2] === '--force'
|
|
|
|
|
2021-08-18 11:19:24 -07:00
|
|
|
const shoot = async (url, filename) => {
|
2021-08-18 05:39:08 -07:00
|
|
|
const pr = new Pageres({
|
|
|
|
delay: 3,
|
2021-08-18 11:19:24 -07:00
|
|
|
scale: 2,
|
2021-08-18 05:39:08 -07:00
|
|
|
});
|
2021-08-18 11:19:24 -07:00
|
|
|
pr.src(process.env.BASE_URL + url, ['1500x300']);
|
2021-08-18 05:39:08 -07:00
|
|
|
for (let buffer of await pr.run()) {
|
2021-09-14 08:45:20 -07:00
|
|
|
fs.mkdirSync(dir, {recursive: true})
|
|
|
|
const target = `${dir}/${filename}.png`;
|
2021-08-18 05:39:08 -07:00
|
|
|
console.log(target);
|
|
|
|
fs.writeFileSync(target, buffer);
|
|
|
|
}
|
2021-08-18 11:19:24 -07:00
|
|
|
}
|
|
|
|
|
2021-09-25 07:25:40 -07:00
|
|
|
const dumpNameDays = async () => {
|
|
|
|
if (!config.names || !config.names.enabled || !config.names.namedays) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const db = await dbConnection();
|
|
|
|
const names = await db.all(`
|
|
|
|
SELECT n.name, n.namedays
|
|
|
|
FROM names n
|
|
|
|
WHERE n.namedays IS NOT NULL
|
|
|
|
AND approved = 1
|
|
|
|
AND deleted = 0
|
|
|
|
`);
|
|
|
|
const output = {};
|
|
|
|
for (let {name, namedays} of names) {
|
|
|
|
output[name] = namedays.split('|');
|
|
|
|
}
|
|
|
|
fs.writeFileSync(`${__dirname}/../data/names/namedays.json`, JSON.stringify(output));
|
|
|
|
}
|
|
|
|
|
2021-08-18 11:19:24 -07:00
|
|
|
(async () => {
|
2021-10-03 15:19:23 -07:00
|
|
|
const prevPath = `${__dirname}/../cache/calendar.json`
|
|
|
|
const prev = fs.existsSync(prevPath) ? JSON.parse(fs.readFileSync(prevPath)) : {};
|
|
|
|
|
|
|
|
const current = calendar.buildSummary();
|
|
|
|
const changedYears = new Set();
|
|
|
|
for (let day in current) {
|
|
|
|
if (!current.hasOwnProperty(day)) { continue; }
|
2021-10-04 14:05:38 -07:00
|
|
|
const year = day.substring(0, 4);
|
2021-10-04 14:43:56 -07:00
|
|
|
if (current[day] !== prev[day] || !fs.existsSync(`${dir}/${day}.png`) || force) {
|
2021-10-03 15:19:23 -07:00
|
|
|
await shoot(`/${config.calendar.route}/${day}?layout=basic`, `${day}`);
|
2021-10-04 14:05:38 -07:00
|
|
|
changedYears.add(year);
|
|
|
|
}
|
2021-10-04 14:43:56 -07:00
|
|
|
if (!fs.existsSync(`${dir}/${year}-overview.png`) || !fs.existsSync(`${dir}/${year}-labels.png`) || force) {
|
2021-10-04 14:05:38 -07:00
|
|
|
changedYears.add(year);
|
2021-10-03 15:19:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let year of changedYears) {
|
|
|
|
await shoot(`/${config.calendar.route}/${year}?layout=basic`, `${year}-overview`);
|
2021-10-04 14:05:38 -07:00
|
|
|
await shoot(`/${config.calendar.route}/${year}?layout=basic&labels=true`, `${year}-labels`);
|
2021-10-03 15:19:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(prevPath, JSON.stringify(current, null, 4));
|
|
|
|
|
2021-09-25 07:25:40 -07:00
|
|
|
await dumpNameDays();
|
2021-08-18 05:39:08 -07:00
|
|
|
})();
|