forked from lankybutmacho/web-to-kindle-heroku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (44 loc) · 1.61 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
const express = require('express');
const path = require('path');
const puppeteer = require('puppeteer');
const execFile = require('child_process').execFile;
const fs = require('fs');
const PORT = process.env.PORT || 3000;
express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', async (req, res) => {
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
const page = await browser.newPage();
await page.setViewport({ width: 1072, height: 1448 });
await page.goto(process.env.SCREENSHOT_URL || 'https://darksky.net/details/40.7127,-74.0059/2021-1-6/us12/en');
// wait 5 sec
await page.waitFor(5000);
//await page.waitForNavigation({ waitUntil: 'networkidle0' });
await page.screenshot({
path: '/tmp/screenshot.png',
});
await browser.close();
await convert('/tmp/screenshot.png');
screenshot = fs.readFileSync('/tmp/screenshot.png');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': screenshot.length,
});
return res.end(screenshot);
})
.listen(PORT, () => console.log(`Listening on ${PORT}`));
function convert(filename) {
return new Promise((resolve, reject) => {
const args = [filename, '-gravity', 'center', '-extent', '1072x1448', '-colorspace', 'gray', '-depth', '8', filename];
execFile('convert', args, (error, stdout, stderr) => {
if (error) {
console.error({ error, stdout, stderr });
reject();
} else {
resolve();
}
});
});
}