2021-09-07 10:11:15 -07:00
|
|
|
|
require('../src/dotenv')();
|
|
|
|
|
|
2021-07-10 07:46:29 -07:00
|
|
|
|
const Pageres = require('pageres');
|
|
|
|
|
const isHighLoadTime = require('./overload');
|
|
|
|
|
const dbConnection = require('./db');
|
|
|
|
|
const locales = require('../src/locales');
|
|
|
|
|
const { ulid } = require('ulid');
|
|
|
|
|
|
|
|
|
|
const awsConfig = require('./aws');
|
|
|
|
|
const S3 = require('aws-sdk/clients/s3');
|
|
|
|
|
const s3 = new S3(awsConfig);
|
|
|
|
|
|
|
|
|
|
const urlBases = {}
|
|
|
|
|
for (let [code, , url, ] of locales) {
|
|
|
|
|
urlBases[code] = url + '/card/@'; // 'http://localhost:3000/card/@'
|
|
|
|
|
}
|
2021-09-05 05:12:40 -07:00
|
|
|
|
const domainLocaleMap = {};
|
|
|
|
|
for (let [code, , url, ] of locales) {
|
|
|
|
|
domainLocaleMap[url.replace(/^https?:\/\//, '')] = code;
|
|
|
|
|
}
|
2021-07-10 07:46:29 -07:00
|
|
|
|
|
2021-07-11 03:17:33 -07:00
|
|
|
|
const sleep = ms => new Promise(res => setTimeout(res, ms));
|
|
|
|
|
|
|
|
|
|
const modes = ['light', 'dark'];
|
|
|
|
|
|
2021-10-13 12:56:39 -07:00
|
|
|
|
const shoot = async (db, mode) => {
|
|
|
|
|
const profiles = (await db.all(`
|
2021-07-11 03:17:33 -07:00
|
|
|
|
SELECT profiles.id, profiles.locale, users.username
|
|
|
|
|
FROM profiles
|
2021-10-13 12:56:39 -07:00
|
|
|
|
LEFT JOIN users on profiles.userId = users.id
|
|
|
|
|
WHERE profiles.${mode === 'dark' ? 'cardDark' : 'card'} = ''
|
2021-07-11 03:17:33 -07:00
|
|
|
|
ORDER BY RANDOM()
|
2021-07-14 07:39:46 -07:00
|
|
|
|
LIMIT 6
|
2021-07-11 03:17:33 -07:00
|
|
|
|
`)).filter(({locale}) => !isHighLoadTime(locale));
|
|
|
|
|
|
2021-10-13 12:56:39 -07:00
|
|
|
|
if (profiles.length === 0) {
|
|
|
|
|
console.log('No profiles in the queue');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const results = {}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const pr = new Pageres({
|
|
|
|
|
darkMode: mode === 'dark',
|
|
|
|
|
delay: 3,
|
|
|
|
|
scale: 2,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (let {locale, username} of profiles) {
|
|
|
|
|
console.log(`Shooting @${username} (${locale}, ${mode})`);
|
|
|
|
|
pr.src(urlBases[locale] + username, ['1024x300'])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let buffer of await pr.run()) {
|
|
|
|
|
const [, domain, username] = buffer.filename.match(/(.*)!card!@(.*)-1024x300\.png/);
|
|
|
|
|
const locale = domainLocaleMap[domain];
|
|
|
|
|
results[locale + '/' + username] = buffer;
|
2021-07-10 07:46:29 -07:00
|
|
|
|
}
|
2021-10-13 12:56:39 -07:00
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-11 03:17:33 -07:00
|
|
|
|
|
2021-10-13 12:56:39 -07:00
|
|
|
|
for (let {id, locale, username} of profiles) {
|
|
|
|
|
const cardId = ulid();
|
|
|
|
|
let key = `card/${locale}/${username}-${cardId}.png`;
|
|
|
|
|
if (mode === 'dark') {
|
|
|
|
|
key = mode === 'dark' ? key.replace('.png', '-dark.png') : key;
|
2021-07-11 03:17:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 12:56:39 -07:00
|
|
|
|
console.log(`Uploading @${username} (${locale}, ${mode}) – ${cardId}`);
|
|
|
|
|
|
|
|
|
|
const buffer = results[locale + '/' + username.replace(/\.+$/, '')];
|
|
|
|
|
|
|
|
|
|
if (buffer === undefined) {
|
|
|
|
|
console.error('Cannot find the proper buffer!');
|
2021-07-11 03:17:33 -07:00
|
|
|
|
continue;
|
2021-07-10 07:46:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 12:56:39 -07:00
|
|
|
|
const s3putResponse = await s3.putObject({
|
|
|
|
|
Key: key,
|
|
|
|
|
Body: buffer,
|
|
|
|
|
ContentType: 'image/png',
|
|
|
|
|
ACL: 'public-read',
|
|
|
|
|
}).promise();
|
|
|
|
|
|
|
|
|
|
await db.get(`
|
|
|
|
|
UPDATE profiles
|
|
|
|
|
SET ${mode === 'dark' ? 'cardDark' : 'card'}='https://${awsConfig.params.Bucket}.s3.${awsConfig.region}.amazonaws.com/${key}'
|
|
|
|
|
WHERE id='${id}'`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
|
const db = await dbConnection();
|
|
|
|
|
while (true) {
|
|
|
|
|
for (let mode of modes) {
|
|
|
|
|
await sleep(3000);
|
|
|
|
|
console.log(`Starting mode: ${mode}`)
|
|
|
|
|
await shoot(db, mode);
|
2021-07-11 03:17:33 -07:00
|
|
|
|
}
|
2021-07-10 07:46:29 -07:00
|
|
|
|
}
|
|
|
|
|
})();
|