-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathlookup.ts
583 lines (495 loc) · 15.2 KB
/
lookup.ts
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import {
Browser,
PageEventObject,
Page,
HTTPRequest,
HTTPResponse,
ResponseForRequest,
} from 'puppeteer';
import {Link, Store, getStores} from './model';
import {Print, logger} from '../logger';
import {Selector, getPrice, pageIncludesLabels} from './includes-labels';
import {
closePage,
delay,
getRandomUserAgent,
getSleepTime,
isStatusCodeInRange,
noop,
} from '../util';
import {disableBlockerInPage, enableBlockerInPage} from '../adblocker';
import {config} from '../config';
import {fetchLinks} from './fetch-links';
import {filterStoreLink} from './filter';
import open from 'open';
import {processBackoffDelay} from './model/helpers/backoff';
import {sendNotification} from '../messaging';
import {handleCaptchaAsync} from './captcha-handler';
import useProxy from '@doridian/puppeteer-page-proxy';
import {promises as fs} from 'fs';
import path from 'path';
const inStock: Record<string, boolean> = {};
const linkBuilderLastRunTimes: Record<string, number> = {};
function nextProxy(store: Store) {
if (!store.proxyList) {
return;
}
if (store.currentProxyIndex === undefined) {
store.currentProxyIndex = 0;
} else {
store.currentProxyIndex++;
}
if (store.currentProxyIndex >= store.proxyList.length) {
store.currentProxyIndex = 0;
}
logger.debug(
`ℹ [${store.name}] Next proxy index: ${store.currentProxyIndex} / Count: ${
store.proxyList.length
} (${store.proxyList[store.currentProxyIndex]})`
);
return store.proxyList[store.currentProxyIndex];
}
async function handleLowBandwidth(request: HTTPRequest) {
if (!config.browser.lowBandwidth) {
return false;
}
const typ = request.resourceType();
if (typ === 'font' || typ === 'image') {
try {
await request.abort();
} catch {
logger.debug('Failed to abort request.');
}
return true;
}
return false;
}
async function handleProxy(request: HTTPRequest, proxy?: string) {
if (!proxy) {
return false;
}
try {
await useProxy(request, proxy);
} catch (error: unknown) {
logger.error('handleProxy', error);
try {
await request.abort();
} catch {
logger.debug('Failed to abort request.');
}
}
return true;
}
async function handleAdBlock(request: HTTPRequest, adBlockRequestHandler: any) {
if (!adBlockRequestHandler) {
return false;
}
return new Promise(resolve => {
const continueFunc = async () => {
resolve(false);
};
const abortFunc = async () => {
try {
await request.abort();
} catch {
logger.debug('Failed to abort request.');
}
resolve(true);
};
const respondFunc = async (response: ResponseForRequest) => {
try {
await request.respond(response);
} catch {
logger.debug('Failed to abort request.');
}
resolve(true);
};
const requestProxy = new Proxy(request, {
get(target, prop, receiver) {
if (prop === 'continue') {
return continueFunc;
}
if (prop === 'abort') {
return abortFunc;
}
if (prop === 'respond') {
return respondFunc;
}
return Reflect.get(target, prop, receiver);
},
});
adBlockRequestHandler(requestProxy);
});
}
/**
* Responsible for looking up information about a each product within
* a `Store`. It's important that we ignore `no-await-in-loop` here
* because we don't want to get rate limited within the same store.
*
* @param browser Puppeteer browser.
* @param store Vendor of items.
*/
async function lookup(browser: Browser, store: Store) {
if (!getStores().has(store.name)) {
return;
}
if (store.linksBuilder) {
const lastRunTime = linkBuilderLastRunTimes[store.name] ?? -1;
const ttl = store.linksBuilder.ttl ?? Number.MAX_SAFE_INTEGER;
if (lastRunTime === -1 || Date.now() - lastRunTime > ttl) {
logger.info(`[${store.name}] Running linksBuilder...`);
try {
await fetchLinks(store, browser);
linkBuilderLastRunTimes[store.name] = Date.now();
} catch (error: unknown) {
logger.error(error);
}
}
}
/* eslint-disable no-await-in-loop */
for (const link of store.links) {
if (!filterStoreLink(link)) {
continue;
}
if (config.page.inStockWaitTime && inStock[link.url]) {
logger.info(Print.inStockWaiting(link, store, true));
continue;
}
const proxy = nextProxy(store);
const useAdBlock = !config.browser.lowBandwidth && !store.disableAdBlocker;
const customContext = config.browser.isIncognito;
const context = customContext
? await browser.createIncognitoBrowserContext()
: browser.defaultBrowserContext();
const page = await context.newPage();
await page.setRequestInterception(true);
page.setDefaultNavigationTimeout(config.page.timeout);
await page.setUserAgent(await getRandomUserAgent());
let adBlockRequestHandler: any;
let pageProxy;
if (useAdBlock) {
const onProxyFunc = (event: keyof PageEventObject, handler: any) => {
if (event !== 'request') {
page.on(event, handler);
return;
}
adBlockRequestHandler = handler;
};
pageProxy = new Proxy(page, {
get(target, prop, receiver) {
if (prop === 'on') {
return onProxyFunc;
}
// Give dummy setRequestInterception to avoid AdBlock from messing with it
if (prop === 'setRequestInterception') {
return noop;
}
return Reflect.get(target, prop, receiver);
},
});
await enableBlockerInPage(pageProxy);
}
await page.setRequestInterception(true);
page.on('request', async request => {
if (await handleLowBandwidth(request)) {
return;
}
if (await handleAdBlock(request, adBlockRequestHandler)) {
return;
}
if (await handleProxy(request, proxy)) {
return;
}
try {
await request.continue();
} catch {
logger.debug('Failed to continue request.');
}
});
if (store.captchaDeterrent) {
await runCaptchaDeterrent(browser, store, page);
}
let statusCode = 0;
try {
statusCode = await lookupIem(browser, store, page, link);
} catch (error: unknown) {
if (store.currentProxyIndex !== undefined && store.proxyList) {
const proxy = `${store.currentProxyIndex + 1}/${
store.proxyList.length
}`;
logger.error(
`✖ [${proxy}] [${store.name}] ${link.brand} ${link.series} ${
link.model
} - ${(error as Error).message}`
);
} else {
logger.error(
`✖ [${store.name}] ${link.brand} ${link.series} ${link.model} - ${
(error as Error).message
}`
);
}
const client = await page.target().createCDPSession();
await client.send('Network.clearBrowserCookies');
}
if (pageProxy) {
await disableBlockerInPage(pageProxy);
}
if (
store.currentProxyIndex !== undefined &&
store.proxyList &&
store.proxyList?.length > 1
) {
const client = await page.target().createCDPSession();
await client.send('Network.clearBrowserCookies');
}
// Must apply backoff before closing the page, e.g. if CloudFlare is
// used to detect bot traffic, it introduces a 5 second page delay
// before redirecting to the next page
await processBackoffDelay(store, link, statusCode);
await closePage(page);
if (customContext) {
await context.close();
}
}
/* eslint-enable no-await-in-loop */
}
async function lookupIem(
browser: Browser,
store: Store,
page: Page,
link: Link
): Promise<number> {
const givenWaitFor = store.waitUntil ? store.waitUntil : 'networkidle0';
const response: HTTPResponse | null = await page.goto(link.url, {
waitUntil: givenWaitFor,
});
const successStatusCodes = store.successStatusCodes ?? [[0, 399]];
const statusCode = await handleResponse(browser, store, page, link, response);
if (!isStatusCodeInRange(statusCode, successStatusCodes)) {
return statusCode;
}
if (await isItemInStock(store, page, link)) {
const givenUrl =
link.cartUrl && config.store.autoAddToCart ? link.cartUrl : link.url;
logger.info(`${Print.inStock(link, store, true)}\n${givenUrl}`);
if (config.browser.open) {
await (link.openCartAction === undefined
? open(givenUrl)
: link.openCartAction(browser));
}
sendNotification(link, store);
if (config.page.inStockWaitTime) {
inStock[link.url] = true;
setTimeout(() => {
inStock[link.url] = false;
}, 1000 * config.page.inStockWaitTime);
}
if (config.page.screenshot) {
logger.debug('ℹ saving screenshot');
await fs.mkdir(config.page.screenshotDir, {recursive: true});
link.screenshot = path.join(
config.page.screenshotDir,
`success-${Date.now()}.png`
);
await page.screenshot({path: link.screenshot});
}
}
return statusCode;
}
// eslint-disable-next-line max-params
async function handleResponse(
browser: Browser,
store: Store,
page: Page,
link: Link,
response?: HTTPResponse | null,
recursionDepth = 0
) {
if (!response) {
logger.debug(Print.noResponse(link, store, true));
}
const successStatusCodes = store.successStatusCodes ?? [[0, 399]];
let statusCode = response?.status() ?? 0;
if (!isStatusCodeInRange(statusCode, successStatusCodes)) {
if (statusCode === 429) {
logger.warn(Print.rateLimit(link, store, true));
} else if (statusCode === 503) {
if (await checkIsCloudflare(store, page, link)) {
if (recursionDepth > 4) {
logger.warn(Print.recursionLimit(link, store, true));
} else {
const response: HTTPResponse | null = await page.waitForNavigation({
waitUntil: 'networkidle0',
});
recursionDepth++;
statusCode = await handleResponse(
browser,
store,
page,
link,
response,
recursionDepth
);
}
} else {
logger.warn(Print.badStatusCode(link, store, statusCode, true));
}
} else {
logger.warn(Print.badStatusCode(link, store, statusCode, true));
}
}
return statusCode;
}
async function checkIsCloudflare(store: Store, page: Page, link: Link) {
const baseOptions: Selector = {
requireVisible: true,
selector: 'body',
type: 'textContent',
};
const cloudflareLabel = {
container: 'div[class="attribution"] a[rel="noopener noreferrer"]',
text: ['Cloudflare'],
};
if (await pageIncludesLabels(page, cloudflareLabel, baseOptions)) {
logger.warn(Print.cloudflare(link, store, true));
return true;
}
return false;
}
async function isItemInStock(
store: Store,
page: Page,
link: Link
): Promise<boolean> {
const baseOptions: Selector = {
requireVisible: false,
selector: store.labels.container ?? 'body',
type: 'textContent',
};
if (store.labels.captcha) {
if (await pageIncludesLabels(page, store.labels.captcha, baseOptions)) {
logger.warn(Print.captcha(link, store, true));
if (config.captchaHandler.service && store.labels.captchaHandler) {
logger.debug(`[${store.name}] captcha handler called`);
if (!(await handleCaptchaAsync(page, store))) {
logger.warn(`[${store.name}] captcha handler failed`);
return false;
} else {
logger.debug(`[${store.name}] captcha handler done, checking item`);
return await isItemInStock(store, page, link);
}
} else {
logger.debug(`[${store.name}] captcha handler skipped`);
await delay(getSleepTime(store));
return false;
}
}
}
if (store.labels.bannedSeller) {
if (
await pageIncludesLabels(page, store.labels.bannedSeller, baseOptions)
) {
logger.warn(Print.bannedSeller(link, store, true));
return false;
}
}
if (store.labels.outOfStock) {
if (await pageIncludesLabels(page, store.labels.outOfStock, baseOptions)) {
logger.info(Print.outOfStock(link, store, true));
return false;
}
}
if (link.labels?.inStock) {
const options = {
...baseOptions,
requireVisible: true,
type: 'outerHTML' as const,
};
if (!(await pageIncludesLabels(page, link.labels.inStock, options))) {
logger.info(Print.outOfStock(link, store, true));
return false;
}
}
if (store.labels.inStock) {
const options = {
...baseOptions,
requireVisible: true,
type: 'outerHTML' as const,
};
if (!(await pageIncludesLabels(page, store.labels.inStock, options))) {
logger.info(Print.outOfStock(link, store, true));
return false;
}
}
if (store.labels.maxPrice) {
const maxPrice = config.store.maxPrice.series[link.series];
link.price = await getPrice(page, store.labels.maxPrice, baseOptions);
if (link.price && link.price > maxPrice && maxPrice > 0) {
logger.info(Print.maxPrice(link, store, maxPrice, true));
return false;
}
}
return true;
}
async function runCaptchaDeterrent(browser: Browser, store: Store, page: Page) {
const successStatusCodes = store.successStatusCodes ?? [[0, 399]];
let statusCode = 0;
let deterrentLinks: string[] = [];
logger.debug(`[${store.name}] Navigating to random anti-captcha page...`);
if (store.captchaDeterrent?.hardLinks?.length) {
deterrentLinks = deterrentLinks.concat(store.captchaDeterrent.hardLinks);
}
if (store.captchaDeterrent?.searchUrl) {
if (store.captchaDeterrent.searchTerms) {
store.captchaDeterrent.searchTerms.forEach(element =>
deterrentLinks.push(
store.captchaDeterrent?.searchUrl
? store.captchaDeterrent.searchUrl.replace('%%s', element)
: ''
)
);
}
}
if (deterrentLinks.length > 0) {
const link: Link = {
brand: 'captcha-deterrent',
model: 'captcha-deterrent',
series: 'captcha-deterrent',
url: deterrentLinks[Math.floor(Math.random() * deterrentLinks.length)],
};
logger.debug(`Selected captcha-deterrent link: ${link.url}`);
try {
const givenWaitFor = store.waitUntil ? store.waitUntil : 'networkidle0';
const response: HTTPResponse | null = await page.goto(link.url, {
waitUntil: givenWaitFor,
});
statusCode = await handleResponse(browser, store, page, link, response);
setTimeout(() => {
// Do nothing
}, 3000);
} catch (error: unknown) {
logger.error(error);
}
if (!isStatusCodeInRange(statusCode, successStatusCodes)) {
logger.warn(
`✖ [${store.name}] - Failed to navigate to anti-captcha target: ${link.url}`
);
}
}
}
export async function tryLookupAndLoop(browser: Browser, store: Store) {
if (!browser.isConnected()) {
logger.silly(`[${store.name}] Ending this loop as browser is disposed...`);
return;
}
logger.silly(`[${store.name}] Starting lookup...`);
try {
await lookup(browser, store);
} catch (error: unknown) {
logger.error(error);
}
const sleepTime = getSleepTime(store);
logger.silly(`[${store.name}] Lookup done, next one in ${sleepTime} ms`);
setTimeout(tryLookupAndLoop, sleepTime, browser, store);
}