Skip to content

Commit 38b5363

Browse files
committed
promise-parallel-throttle usage
1 parent e03cd81 commit 38b5363

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

index.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"use strict";
2-
const fs = require("fs-extra");
2+
const IO = require("fs-extra");
33
const PDFMerger = require("pdf-merger-js");
4-
const puppeteer = require("puppeteer");
5-
const { outputPath, overwrite, maxParallel } = require("./config.json");
4+
const Throttle = require("promise-parallel-throttle");
5+
const Puppeteer = require("puppeteer");
6+
const { outputPath, overwrite, maxInProgress } = require("./config.json");
67

78
function sleep(ms) {
89
return new Promise(resolve => setTimeout(resolve, ms));
@@ -35,11 +36,11 @@ async function convertToPDF(tab, url, name, i, stylesheet) {
3536
let filename = `${i}.pdf`;
3637
let path = `${outputPath}/${name}/${filename}`;
3738

38-
if (!overwrite && await fs.pathExists(path)) {
39+
if (!overwrite && await IO.pathExists(path)) {
3940
return path;
4041
}
4142

42-
await fs.ensureDir(path.replace(filename, ""));
43+
await IO.ensureDir(path.replace(filename, ""));
4344
let page = await ensureGoTo(tab, url);
4445
await page.addStyleTag({ "content": stylesheet });
4546

@@ -54,7 +55,7 @@ async function convertToPDF(tab, url, name, i, stylesheet) {
5455
async function scrapeGuide(guide, browser, cookies, stylesheet) {
5556
let { url, title } = guide;
5657
let path = `${outputPath}/${title}.pdf`;
57-
if (!overwrite && await fs.pathExists(path)) {
58+
if (!overwrite && await IO.pathExists(path)) {
5859
console.log(path);
5960
return;
6061
}
@@ -75,9 +76,9 @@ async function scrapeGuide(guide, browser, cookies, stylesheet) {
7576
}
7677

7778
(async() => {
78-
const stylesheet = await fs.readFile("stylesheet.css", "utf8");
79-
const cookies = await fs.readJSON("cookies.json");
80-
const browser = await puppeteer.launch();
79+
const stylesheet = await IO.readFile("stylesheet.css", "utf8");
80+
const cookies = await IO.readJSON("cookies.json");
81+
const browser = await Puppeteer.launch();
8182

8283
let page = await newPage(browser, cookies);
8384
page = await ensureGoTo(page, "https://primagames.com/accounts/account/my_guides");
@@ -88,9 +89,7 @@ async function scrapeGuide(guide, browser, cookies, stylesheet) {
8889
await page.close();
8990
console.log(`Found ${guides.length} eGuides`);
9091

91-
for (let i = 0; i < guides.length; i += maxParallel) {
92-
await Promise.all(guides.slice(i, i + maxParallel).map(guide => scrapeGuide(guide, browser, cookies, stylesheet)));
93-
}
92+
await Throttle.all(guides.map(guide => () => scrapeGuide(guide, browser, cookies, stylesheet)), { maxInProgress });
9493
await browser.close();
9594
process.exit(0);
9695
})().catch(err => {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"dependencies": {
2424
"fs-extra": "^9.0.0",
2525
"pdf-merger-js": "^3.0.1",
26+
"promise-parallel-throttle": "^3.3.0",
2627
"puppeteer": "^3.0.0"
2728
}
2829
}

0 commit comments

Comments
 (0)