feat: in stock wait time per link now

Signed-off-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
Jef LeCompte
2020-09-25 08:25:58 -04:00
parent d25a643425
commit c7a716f981
2 changed files with 16 additions and 8 deletions
+8 -1
View File
@@ -35,11 +35,18 @@ export const Print = {
},
inStock(link: Link, store: Store, color?: boolean): string {
if (color) {
return chalk.green.bold(`🚀🚨 ${buildProductString(link, store, true)} :: IN STOCK 🚨🚀`);
return chalk.bgGreen.white.bold(`🚀🚨 ${buildProductString(link, store, false)} :: IN STOCK 🚨🚀`);
}
return `🚀🚨 ${buildProductString(link, store)} :: IN STOCK 🚨🚀`;
},
inStockWaiting(link: Link, store: Store, color?: boolean): string {
if (color) {
return ' ' + buildProductString(link, store, true) + ' :: ' + chalk.yellow('IN STOCK, WAITING');
}
return ` ${buildProductString(link, store)} :: IN STOCK, WAITING`;
},
outOfStock(link: Link, store: Store, color?: boolean): string {
if (color) {
return '✖ ' + buildProductString(link, store, true) + ' :: ' + chalk.red('OUT OF STOCK');
+8 -7
View File
@@ -25,6 +25,11 @@ async function lookup(browser: Browser, store: Store) {
continue;
}
if (Config.page.inStockWaitTime && inStock[store.name]) {
Logger.info(Print.inStockWaiting(link, store, true));
continue;
}
const page = await browser.newPage();
page.setDefaultNavigationTimeout(Config.page.navigationTimeout);
await page.setUserAgent(Config.page.userAgent);
@@ -59,10 +64,10 @@ async function lookupCard(browser: Browser, store: Store, page: Page, link: Link
sendNotification(link, store);
if (Config.page.inStockWaitTime) {
inStock[store.name] = true;
inStock[link.url] = true;
setTimeout(() => {
inStock[store.name] = false;
inStock[link.url] = false;
}, 1000 * Config.page.inStockWaitTime);
}
@@ -119,11 +124,7 @@ async function lookupPageHasCaptcha(store: Store, page: Page) {
export async function tryLookupAndLoop(browser: Browser, store: Store) {
Logger.debug(`[${store.name}] Starting lookup...`);
try {
if (Config.page.inStockWaitTime && inStock[store.name]) {
Logger.info(`[${store.name}] Has stock, waiting before trying to lookup again...`);
} else {
await lookup(browser, store);
}
await lookup(browser, store);
} catch (error) {
Logger.error(error);
}