-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
38 lines (30 loc) · 1.13 KB
/
main.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
const fs = require("fs");
const yaml = require("js-yaml");
const { chromium } = require("playwright-chromium");
// load browser configuration
const { userDataDir, launchOptions, downloadsPath } = yaml.safeLoad(
fs.readFileSync("./config.yaml", "utf-8")
);
// load the downloader function
const download = require("./download");
// load the info needed to download the items
const downloadItems = require("./downloadItems");
chromium // launch browser
.launchPersistentContext(userDataDir, launchOptions)
.then(async browser => {
// enqueue downloads
let downloads = [];
for (let item of downloadItems) {
let page = await browser.newPage(); // new tab for each newspaper
downloads.push(
// promise a download v--- toggle to see errors
download(item, page, downloadsPath, false)
.then(console.log) // logs success
.catch(console.log) // logs failure
);
}
// wait for all downloads to finish / fail
await Promise.allSettled(downloads);
// close the browser
await browser.close();
}).catch(console.log).finally(() => console.log('\n'))