-
Notifications
You must be signed in to change notification settings - Fork 0
/
memegeddon.js
60 lines (51 loc) · 2.45 KB
/
memegeddon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const {getMemesDb} = require('./lib/digester'),
{downloadAllMemes} = require('./lib/downloader.js'),
{uploadAllFiles, publicAllFiles} = require('./lib/uploader'),
{saveFile, timestamp, getFile, listFiles, zipDirectory, removeFolder} = require('./lib/utils'),
{delayPerPage, paths, cleanUpAndBackup} = require('./config'),
{saveMemes} = require('./lib/database');
async function stopEngine(originalMemes){
try {
const files = await listFiles(paths.data);
const filesContent = await Promise.all(files.map(file => getFile(`${paths.data}/${file}`, 'utf8')));
let memes = filesContent
.map(data => JSON.parse(data))
.reduce((a,b) => a.concat(b));
if(cleanUpAndBackup){
await saveFile(`${paths.memes}/data.json`, JSON.stringify(memes), "utf8");
await zipDirectory(paths.memes, `${paths.backup}/${timestamp()}.zip`);
await Promise.all([paths.memes, paths.data].map(folder => removeFolder(`${folder}/*`)));
} else {
await saveFile(`${paths.data}/data.json`, JSON.stringify(memes), "utf8");
}
if(originalMemes.length === memes.length){
console.log(`[Memegeddon][INFO][~(^-^)~] All memes are stores as expected. Congratulations, haters gonna hate!`);
process.exit(50); //@TODO: use a better exit code?
} else {
console.log(`[Memegeddon][ERROR][SYSTEM FAILURE][(⩾﹏⩽)] stopEngine has loses some memes in the process!!`);
process.exit(1);
}
} catch(err) {
console.log(`[Memegeddon][ERROR][SYSTEM FAILURE][(⩾﹏⩽)] stopEngine has broken!`);
console.error(err);
}
}
async function memesFunnel(memes){
try {
memes = await downloadAllMemes(memes);
await uploadAllFiles(memes);
memes = await publicAllFiles(memes);
await saveFile(`${paths.data}/${timestamp()}.json`, JSON.stringify(memes), "utf8");
await saveMemes(memes);
} catch (err) {
console.log(`[Memegeddon][ERROR][SYSTEM FAILURE][(⩾﹏⩽)] MemeFunnel has broken.`);
console.error(err);
}
}
getMemesDb("http://alpha-meme-maker.herokuapp.com", delayPerPage)
.on("data", memesFunnel)
.on("end", stopEngine)
.on("error", err => {
console.log(`[Memegeddon][ERROR][SYSTEM FAILURE][ಠ_ಠ] getMemesDb has broken. Time to run in circles!`);
console.error(err);
});