-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathcrawlerConductor.js
More file actions
185 lines (167 loc) · 6.39 KB
/
crawlerConductor.js
File metadata and controls
185 lines (167 loc) · 6.39 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const os = require('os');
const cores = os.cpus().length;
const chalk = require('chalk');
const asyncLib = require('async');
const crawl = require('./crawler');
const { createTimer } = require('./helpers/timer');
const { downloadChrome } = require('./helpers/chromiumDownload');
const notABot = require('./helpers/notABot');
const MAX_NUMBER_OF_RETRIES = 2;
/**
* @param {CrawlAndSaveDataOptions} options
* @returns {Promise<void>}
* @description Wrapper function to call the crawler with the provided options.
*/
async function crawlAndSaveData({
urlString,
dataCollectors,
log,
filterOutFirstParty,
dataCallback,
emulateMobile,
proxyHost,
antiBotDetection,
executablePath,
maxLoadTimeMs,
extraExecutionTimeMs,
collectorFlags,
seleniumHub,
}) {
const url = new URL(urlString);
/**
* @type {function(...any):void}
*/
const prefixedLog = (...msg) => {
const now = new Date();
const curTime = new Intl.DateTimeFormat('en-GB', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
}).format(now);
log(chalk.gray(`${curTime} ${url.hostname}:`), ...msg);
};
const data = await crawl(url, {
log: prefixedLog,
// @ts-ignore
collectors: dataCollectors.map((collector) => new collector.constructor()),
filterOutFirstParty,
emulateMobile,
proxyHost,
runInEveryFrame: antiBotDetection ? notABot : undefined,
executablePath,
maxLoadTimeMs,
extraExecutionTimeMs,
collectorFlags,
seleniumHub,
});
dataCallback(url, data);
}
/**
* @param {CrawlerConductorOptions} options
*/
module.exports = async (options) => {
const log = options.logFunction || (() => {});
const failureCallback = options.failureCallback || (() => {});
let numberOfCrawlers = options.numberOfCrawlers || Math.floor(cores * 0.8);
numberOfCrawlers = Math.min(numberOfCrawlers, options.urls.length);
// Increase number of listeners so we have at least one listener for each async process
if (numberOfCrawlers > process.getMaxListeners()) {
const maxListeners = numberOfCrawlers * 4 + 1;
process.setMaxListeners(maxListeners);
}
log(chalk.cyan(`Number of crawlers: ${numberOfCrawlers}\n`));
// make sure the browser is downloaded before we start parallel tasks
let executablePath = null;
if (!options.seleniumHub) {
executablePath = await downloadChrome(log, options.chromiumVersion);
}
/** @type {Set<string>} */
const inProgress = new Set();
await asyncLib.eachOfLimit(options.urls, numberOfCrawlers, (urlItem, idx, callback) => {
const urlString = typeof urlItem === 'string' ? urlItem : urlItem.url;
let dataCollectors = options.dataCollectors;
// there can be a different set of collectors for every item
if (typeof urlItem !== 'string' && urlItem.dataCollectors) {
dataCollectors = urlItem.dataCollectors;
}
inProgress.add(urlString);
log(chalk.cyan(`Processing entry #${Number(idx) + 1} (${urlString}).`));
const timer = createTimer();
const crawlAndSaveDataOptions = {
urlString,
dataCollectors,
log,
filterOutFirstParty: options.filterOutFirstParty,
dataCallback: options.dataCallback,
emulateMobile: options.emulateMobile,
proxyHost: options.proxyHost,
antiBotDetection: options.antiBotDetection !== false,
executablePath,
maxLoadTimeMs: options.maxLoadTimeMs,
extraExecutionTimeMs: options.extraExecutionTimeMs,
collectorFlags: JSON.parse(JSON.stringify(options.collectorFlags || {})), // clone so that we can modify it for each call
seleniumHub: options.seleniumHub,
};
const task = crawlAndSaveData.bind(null, crawlAndSaveDataOptions);
asyncLib.retry(
{
times: MAX_NUMBER_OF_RETRIES,
interval: 0,
errorFilter: () => {
crawlAndSaveDataOptions.collectorFlags.enableAsyncStacktraces = false; // disable async stack traces because they sometimes are the cause of crash
return true;
},
},
task,
(err) => {
if (err) {
console.log(err);
log(chalk.red(`Max number of retries (${MAX_NUMBER_OF_RETRIES}) exceeded for "${urlString}".`));
failureCallback(urlString, err);
} else {
log(chalk.cyan(`Processing "${urlString}" took ${timer.getElapsedTime()}s.`));
}
inProgress.delete(urlString);
log(chalk.cyan(`In progress (${inProgress.size}): ${Array.from(inProgress).join(', ')}`));
callback();
},
);
});
};
/**
* @typedef {import('./collectors/BaseCollector')} BaseCollector
*/
/**
* @typedef {Object} CrawlAndSaveDataOptions
* @property {string} urlString
* @property {BaseCollector[]} dataCollectors
* @property {function(...any):void} log
* @property {boolean} filterOutFirstParty
* @property {function(URL, import('./crawler').CollectResult): void} dataCallback
* @property {boolean} emulateMobile
* @property {string} proxyHost
* @property {boolean} antiBotDetection
* @property {string} executablePath
* @property {number} maxLoadTimeMs
* @property {number} extraExecutionTimeMs
* @property {import('./collectors/BaseCollector').CollectorFlags} collectorFlags
* @property {string} seleniumHub
*/
/**
* @typedef {Object} CrawlerConductorOptions
* @property {Array<string|{url:string,dataCollectors?:BaseCollector[]}>} urls
* @property {function(URL, import('./crawler').CollectResult): void} dataCallback
* @property {BaseCollector[]=} dataCollectors
* @property {function(string, Error): void=} failureCallback
* @property {number=} numberOfCrawlers
* @property {function=} logFunction
* @property {boolean} filterOutFirstParty
* @property {boolean} emulateMobile
* @property {string} proxyHost
* @property {boolean=} antiBotDetection
* @property {string=} chromiumVersion
* @property {number=} maxLoadTimeMs
* @property {number=} extraExecutionTimeMs
* @property {import('./collectors/BaseCollector').CollectorFlags=} collectorFlags
* @property {string=} seleniumHub
*/