forked from philhawksworth/social-image-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.js
55 lines (47 loc) · 1.13 KB
/
capture.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
const axios = require('axios');
const puppeteer = require('puppeteer');
var browser, page;
/*
* Global config variables
*/
const conf = {
path: './dist/card',
index: 'https://www.hawksworx.com/card-urls.json'
}
/*
* Take a snapshot with puppeteer
*/
async function snap(url, file) {
try {
console.log('snapping :', url);
await page.setViewport({ width: 1024, height: 512 });
await page.goto(url);
await page.screenshot({ path: file, type: 'png' });
console.log('snapped :', file);
}
catch (err) {
console.log('err :', err);
}
}
async function setupAndProcess(array) {
browser = await puppeteer.launch({ headless: true });
page = await browser.newPage();
for (const url of array) {
let re = /\/|\./gi;
let filename = url.split("://")[1].replace(re,"-");
let path = `${conf.path}/${filename}.png`
await snap(url, path);
}
browser.close();
console.log('Done!');
}
/*
* Discover the list of urls for which we need snapshots
*/
axios.get(conf.index)
.then((response) => {
setupAndProcess(response.data.cards);
})
.catch((error) => {
console.log('error :', error)
});