perf: browser abstraction (#68) (#81)

Signed-off-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
Jef LeCompte
2020-09-19 18:13:20 -04:00
committed by GitHub
parent 14b1e4cee6
commit ebbdfe3f63
2 changed files with 9 additions and 5 deletions
+5 -1
View File
@@ -3,18 +3,22 @@ import {Stores} from './store/model';
import {Logger} from './logger';
import {sendNotification} from './notification';
import {lookup} from './store';
import puppeteer from 'puppeteer';
/**
* Starts the bot.
*/
async function main() {
const results = [];
const browser = await puppeteer.launch();
for (const store of Stores) {
Logger.debug(store.links);
results.push(lookup(store));
results.push(lookup(browser, store));
}
await Promise.all(results);
await browser.close();
Logger.info('↗ trying stores again');
setTimeout(main, Config.rateLimitTimeout);
+4 -4
View File
@@ -24,16 +24,16 @@ function filterBrand(brand: string) {
* 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 Current browser in use.
* @param store Vendor of graphics cards.
*/
export async function lookup(store: Store) {
export async function lookup(browser: puppeteer.Browser, store: Store) {
/* eslint-disable no-await-in-loop */
for (const link of store.links) {
if (!filterBrand(link.brand)) {
continue;
}
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.setDefaultNavigationTimeout(Config.page.navigationTimeout);
await page.setUserAgent(Config.page.userAgent);
@@ -48,7 +48,7 @@ export async function lookup(store: Store) {
await page.goto(link.url, {waitUntil: 'networkidle0'});
} catch {
Logger.error(`✖ [${store.name}] ${graphicsCard} skipping; timed out`);
await browser.close();
await page.close();
continue;
}
@@ -79,7 +79,7 @@ export async function lookup(store: Store) {
sendNotification(givenUrl);
}
await browser.close();
await page.close();
}
/* eslint-enable no-await-in-loop */
}