-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
76 lines (67 loc) · 2.68 KB
/
app.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const Promise = require('bluebird');
const File = require('async-file');
const os = require('os');
const Log4js = require('log4js');
const Puppeteer = require('puppeteer');
const dayjs = require('dayjs');
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
const fs = require('fs').promises
const Devices = require('./devices');
const logger = Log4js.getLogger();
logger.level = 'debug';
const errorCatcher = (err) => {
logger.error(err);
if (process.env.NODE_ENV === 'test') return;
};
(async () => {
logger.debug('Loading websites.json...');
const websites = JSON.parse(await File.readFile('websites.json'));
logger.debug(`Loaded ${websites.length} web sites setting.`);
const folderName = `${dayjs().format('YYYYMMDDHHmmss')}`;
let s3client = undefined
if (process.env.S3) {
s3client = new S3Client({ region: process.env.AWS_S3_BUCKET_REGION });
} else {
await fs.mkdir(`screenshots/${folderName}`);
}
const options = {
args: (os.platform() === 'linux' ? ['--no-sandbox', '--disable-setuid-sandbox'] : [])
};
if (process.env.CHROME_EXECUTE_PATH) {
options.executablePath = process.env.CHROME_EXECUTE_PATH;
}
const browser = await Puppeteer.launch(options);
const devices = Object.entries(Devices);
await Promise.each(websites, async (website, index) => {
await Promise.each(devices, async ([deviceName, emulateOptions]) => {
const page = await browser.newPage();
// NOTE: 長野高専のスマートフォンページの遷移でダイアログが出るので対応する
page.on('dialog', async (dialog) => {
if (dialog.message() === 'スマートフォン用サイトを表示しますか?') await dialog.accept();
});
await page.emulate(emulateOptions);
const fileIndex = (index + 1).toString().padStart(2, '0');
const filePath = `${folderName}/${fileIndex}_${website.name}_${deviceName}.png`;
logger.info(`Get ${website.name}(${deviceName.toUpperCase()}) [${website.url}] ...`);
try {
await page.goto(website.url, { waituntil: 'networkidle0' });
const screenshotBuffer = await page.screenshot({ type: 'png', fullPage: true });
if (process.env.S3) {
await s3client.send(new PutObjectCommand({
Bucket: process.env.AWS_S3_BUCKET_NAME,
Key: filePath,
Body: screenshotBuffer
}));
} else {
await fs.writeFile(`screenshots/${filePath}`, screenshotBuffer);
}
} catch (e) {
errorCatcher(e);
} finally {
await page.close();
}
logger.info(`Saved! ${filePath}`);
}).catch(e => errorCatcher(e));
}).catch(e => errorCatcher(e));
browser.close();
})();