-
Notifications
You must be signed in to change notification settings - Fork 0
/
socialimages.js
51 lines (39 loc) · 1.19 KB
/
socialimages.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
const chromium = require('chrome-aws-lambda');
const pages = require('./dynamicPages.json');
const fs = require("fs");
(async function() {
const executablePath = await chromium.executablePath
const browser = await chromium.puppeteer.launch({
args: await chromium.args,
executablePath: executablePath,
headless: true,
});
let dimensions = {
width: 1200,
height: 630
}
for(i=0;i < pages.length; i++){
let state = pages[i];
let uri = (state.location).toLowerCase();
uri = uri.replace(/ /g, '_');
let host = 'https://covidcasesbythenumbers.com/'; //todo replace with .env
const page = await browser.newPage();
await page.goto(host + '/social-image/' + uri);
await page.evaluateHandle('document.fonts.ready');
const dir = './dist/img';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
await page.setViewport({
width: dimensions.width,
height: dimensions.height
})
await page.screenshot({
path: `${dir}/${uri}.png`,
type: 'png',
clip: {x:0, y:0, width: dimensions.width, height: dimensions.height}
});
await page.close();
}
await browser.close();
})().catch(console.error)