-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (55 loc) · 1.56 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
require('dotenv').config()
const puppeteer = require('puppeteer');
var fs = require('fs');
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
async function getImages(url , index=0){
let username = process.env.HT_USER;
let password = process.env.HT_PASS;
let browser = await puppeteer.launch();
let page = await browser.newPage();
try {
if (username && password) {
await page.authenticate({username, password});
}
await page.goto(url, {waitUntil: 'networkidle2'});
let images_links = await page.$$eval('img', image => image.filter(image => image.naturalWidth == 0 || image.readyState == 'uninitialized').map(img => img.src));
if (images_links.length >= 1) {
await page.screenshot({path: `page-${index}.png`, fullPage: true });
images_links.forEach((image) => {
console.log(image);
fs.appendFile('broken_images.txt', image+'\n', function (err) {
if (err) throw err;
console.log('Saved!');
});
});
}
await browser.close();
}
catch (e){
console.error(e);
}
}
(async() => {
fs.writeFile('broken_images.txt', '', function (err) {
if (err) throw err;
console.log('Replaced!');
});
const same_site_links = process.env.SAME_SITE_LINKS.split(',');
try {
await asyncForEach(same_site_links, async (link, index) => {
try {
await getImages(link, index);
}
catch (e){
console.error(e);
}
});
}
catch (e){
console.error(e);
}
})();