@@ -7,6 +7,7 @@ import { formatTorrents } from '@pct-org/torrent/utils'
7
7
import { BaseHelper } from '@pct-org/scraper/helpers/base'
8
8
import { MOVIE_TYPE } from '@pct-org/types/movie'
9
9
import { SHOW_TYPE } from '@pct-org/types/show'
10
+ import * as pLimit from 'p-limit'
10
11
11
12
import {
12
13
ScrapedItem ,
@@ -62,24 +63,27 @@ export abstract class BaseProvider {
62
63
/**
63
64
* Starts scraping the provided configs
64
65
*/
65
- async scrapeConfigs ( ) : Promise < void > {
66
+ public async scrapeConfigs ( ) : Promise < void > {
66
67
this . logger . log ( `Started scraping...` )
67
68
68
- await pMap (
69
- this . configs ,
70
- ( config ) => this . scrapeConfig ( config ) ,
71
- {
72
- concurrency : 1
73
- }
74
- )
69
+ const limit = pLimit ( 1 )
70
+
71
+ await Promise . all ( this . configs . map ( ( config ) => (
72
+ limit ( ( ) => this . scrapeConfig ( config ) )
73
+ ) ) )
75
74
76
75
this . logger . log ( `Done scraping` )
77
76
}
78
77
79
78
/**
80
79
* Set the configuration to scrape with.
81
80
*/
82
- protected setConfig ( { query, contentType, regexps = [ ] , language = 'en' } : ScraperProviderConfig ) : void {
81
+ protected setConfig ( {
82
+ query,
83
+ contentType,
84
+ regexps = [ ] ,
85
+ language = 'en'
86
+ } : ScraperProviderConfig ) : void {
83
87
this . contentType = contentType
84
88
this . query = query
85
89
this . language = language
@@ -105,36 +109,32 @@ export abstract class BaseProvider {
105
109
this . logger . log ( `Total pages ${ totalPages } ` )
106
110
107
111
const torrents = await this . getAllTorrents ( totalPages )
108
- const allContent = await this . getAllContent ( torrents )
112
+ const contents = await this . getAllContent ( torrents )
109
113
110
- this . logger . log ( `Total content ${ allContent . length } ` )
114
+ this . logger . log ( `Total content ${ contents . length } ` )
111
115
112
- await pMap (
113
- allContent ,
114
- async ( content ) => {
115
- const isInBlacklist = await this . isItemBlackListed ( content )
116
+ const limit = pLimit ( this . maxWebRequests )
116
117
117
- // Only get data for this item if it's not in the blacklist
118
- if ( ! isInBlacklist ) {
119
- try {
120
- await this . enhanceAndImport ( content )
118
+ await Promise . all ( contents . map ( ( content ) => limit ( async ( ) => {
119
+ const isInBlacklist = await this . isItemBlackListed ( content )
121
120
122
- } catch ( err ) {
123
- const errorMessage = err . message || err
121
+ // Only get data for this item if it's not in the blacklist
122
+ if ( ! isInBlacklist ) {
123
+ try {
124
+ await this . enhanceAndImport ( content )
124
125
125
- this . logger . error ( `BaseProvider.scrapeConfig: ${ errorMessage } ` , err . stack )
126
+ } catch ( err ) {
127
+ const errorMessage = err . message || err
126
128
127
- // Log the content so it can be better debugged from logs
128
- if ( errorMessage . includes ( 'Could not find any data with slug' ) ) {
129
- this . logger . error ( JSON . stringify ( content ) )
130
- }
129
+ this . logger . error ( `BaseProvider.scrapeConfig: ${ errorMessage } ` , err . stack )
130
+
131
+ // Log the content so it can be better debugged from logs
132
+ if ( errorMessage . includes ( 'Could not find any data with slug' ) ) {
133
+ this . logger . error ( JSON . stringify ( content ) )
131
134
}
132
135
}
133
- } ,
134
- {
135
- concurrency : this . maxWebRequests
136
136
}
137
- )
137
+ } ) ) )
138
138
} catch ( err ) {
139
139
this . logger . error ( `Catch BaseProvider.scrapeConfig: ${ err . message || err } ` , err . stack )
140
140
}
@@ -146,7 +146,10 @@ export abstract class BaseProvider {
146
146
* @returns {Promise<Boolean|Error> }
147
147
*/
148
148
protected async isItemBlackListed ( content : ScrapedItem ) : Promise < boolean | Error > {
149
- const { slug, imdb } = content
149
+ const {
150
+ slug,
151
+ imdb
152
+ } = content
150
153
151
154
const blacklistedItem = await this . blackListModel . findOne ( {
152
155
$or : [
@@ -253,7 +256,11 @@ export abstract class BaseProvider {
253
256
/**
254
257
* Extract content information based on a regex.
255
258
*/
256
- abstract extractContent ( { torrent, regex, lang } ) : ScrapedItem | undefined
259
+ abstract extractContent ( {
260
+ torrent,
261
+ regex,
262
+ lang
263
+ } ) : ScrapedItem | undefined
257
264
258
265
/**
259
266
* Get content info from a given torrent.
@@ -307,7 +314,8 @@ export abstract class BaseProvider {
307
314
return items . set ( slug , item )
308
315
} , {
309
316
concurrency : 1
310
- } ) . then ( ( ) => Array . from ( items . values ( ) ) )
317
+ } )
318
+ . then ( ( ) => Array . from ( items . values ( ) ) )
311
319
}
312
320
313
321
/**
@@ -352,7 +360,7 @@ export abstract class BaseProvider {
352
360
getAllTorrents ( totalPages : number ) : Promise < Array < any > > {
353
361
let torrents = [ ]
354
362
355
- return pTimes ( totalPages , async ( page ) => {
363
+ return pTimes ( totalPages , async ( page ) => {
356
364
this . logger . debug ( `Started searching ${ this . name } on page ${ page + 1 } out of ${ totalPages } ` )
357
365
358
366
// Get the page
@@ -364,17 +372,19 @@ export abstract class BaseProvider {
364
372
} , {
365
373
concurrency : 1 ,
366
374
stopOnError : false
367
- } ) . then ( ( ) => {
368
- this . logger . log ( `Found ${ torrents . length } torrents.` )
375
+ } )
376
+ . then ( ( ) => {
377
+ this . logger . log ( `Found ${ torrents . length } torrents.` )
369
378
370
- return Promise . resolve ( torrents )
371
- } ) . catch ( ( err ) => {
372
- // Only log the errors
373
- this . logger . error ( `Catch BaseProvider.getAllTorrents: ${ err . message || err } ` )
379
+ return Promise . resolve ( torrents )
380
+ } )
381
+ . catch ( ( err ) => {
382
+ // Only log the errors
383
+ this . logger . error ( `Catch BaseProvider.getAllTorrents: ${ err . message || err } ` )
374
384
375
- // We still want to resolve all the pages that did go well
376
- return Promise . resolve ( torrents )
377
- } )
385
+ // We still want to resolve all the pages that did go well
386
+ return Promise . resolve ( torrents )
387
+ } )
378
388
}
379
389
380
390
/**
0 commit comments