11"use strict" ;
2- const fs = require ( "fs-extra" ) ;
2+ const IO = require ( "fs-extra" ) ;
33const 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
78function 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) {
5455async 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 => {
0 commit comments