forked from 11ty/11ty-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-screenshots.js
62 lines (55 loc) · 1.4 KB
/
node-screenshots.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
const puppeteer = require("puppeteer");
const slugify = require("slugify");
const sharp = require("sharp");
const fastglob = require("fast-glob");
async function pause(time) {
let p = new Promise(resolve => {
setTimeout(() => {
resolve();
}, time);
});
await p;
}
async function screenshot(url, fileSlug) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url, {
waitUntil: ["load", "networkidle0"]
});
await pause(1000);
await page.setViewport({
width: 1024,
height: 768,
deviceScaleFactor: 1,
});
let fullFilename = `./img/sites/${fileSlug}_full.jpg`;
let filename = `./img/sites/${fileSlug}.jpg`;
await page.screenshot({
path: fullFilename,
type: "jpeg",
quality: 80
});
await sharp(fullFilename).resize(405).toFile(filename);
await browser.close();
}
(async () => {
let sites = await fastglob("./_data/sites/*.json", {
caseSensitiveMatch: false
});
let i = 0;
for(let site of sites) {
i++;
let siteData = require(site);
if(siteData.url && !siteData.disabled) {
let filename = site.split("/").pop().replace(/\.json/, "");
console.log( `${i} of ${sites.length}`, "Fetching", siteData.url, "to", filename );
try {
await screenshot(siteData.url, filename);
} catch(e) {
console.log( ">>> Error:", e );
}
} else {
console.log( `${i} of ${sites.length}`, "Skipping", siteData.url );
}
}
})();