-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (92 loc) · 2.82 KB
/
index.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import * as fs from 'node:fs/promises';
import DomParser from 'dom-parser';
// Probably I will reuse my code and I want to have my snippets on other place
import {
downloadAsset,
getIndexPage,
imagesEmitter,
leftFillNum,
sleep,
} from './lib/helpers.js';
const targetDir = './memes';
const pageUrl = 'https://memegen-link-examples-upleveled.netlify.app';
const downloadedImages = [];
const parser = new DomParser();
let filteredImages,
deletePromises = [];
// Use Events to avoid the global namespace 💩
imagesEmitter.on('😝', (data) => {
if (downloadedImages.length >= 10) {
// We can't control the chunk of data delivered by server
// if we have more then 10 images we filter the array
filteredImages = downloadedImages.filter((src, index) => {
return index < 10;
});
// "Everybody lies" Dr.House
Promise.all(filteredImages)
.then(() => {
imagesEmitter.emit('🤪');
})
.catch((error) => {
console.error(error.code);
});
console.log(
filteredImages,
`\nWe have always ${filteredImages.length}... this is Spartaaaaaa! 💪\n`,
);
}
// Create a DOM from the chunk of HTML
const dom = parser.parseFromString(data);
// Get image nodes
const nodes = dom.getElementsByTagName('img');
// Iterate nodes & extract img src
nodes.forEach((node) => {
const src = node.getAttribute('src');
// if image src is not in array we push it
if (downloadedImages.indexOf(src) === -1) {
downloadedImages.push(src);
}
});
});
// my left ear
imagesEmitter.on('🤪', () => {
filteredImages.forEach((src, index) => {
// console.log(`${index}. ${src}`);
const filename = `${leftFillNum(index, 2)}.jpg`;
downloadAsset(src, `${targetDir}/${filename}`).catch(() => {});
});
});
// 1. We check if there is meme folder
const initApp = async () => {
await fs.mkdir(targetDir).catch((err) => {
if (err.code === 'EEXIST') {
// console.info('As expected we have memes folder');
// Folder exist try to remove images if we have
// we make multiple promises
try {
fs.readdir(targetDir)
// If promise resolved and data are fetched
.then((filenames) => {
deletePromises = filenames.map(function (filename) {
console.log(`Deleted ${targetDir}/${filename}`);
return fs.unlink(`${targetDir}/${filename}`, () => {});
});
})
// If promise is rejected
.catch(() => {});
} catch (e) {}
// "Everybody lies" Dr.House
Promise.all(deletePromises)
.then(() => {
// start scrapping
getIndexPage(pageUrl);
})
.catch((error) => {
console.error(error);
});
}
});
};
// sleep for 1s to see meme folder gone
await sleep(1000);
await initApp();