From 8adc07a03e411dd536bebfdc7270db4bbf8ddb34 Mon Sep 17 00:00:00 2001 From: jastheace Date: Tue, 6 Oct 2020 21:51:32 -0400 Subject: [PATCH] feat: max price per series (#451) --- .env-example | 4 +++- src/config.ts | 4 +++- src/index.ts | 5 +++++ src/logger.ts | 6 +++--- src/store/lookup.ts | 22 ++++++++++++++++++++-- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/.env-example b/.env-example index f4a62b7c90..3081143d4a 100644 --- a/.env-example +++ b/.env-example @@ -13,7 +13,9 @@ HEADLESS="" IN_STOCK_WAIT_TIME="" LOG_LEVEL="" LOW_BANDWIDTH="" -MAX_PRICE="" +MAX_PRICE_3070="" +MAX_PRICE_3080="" +MAX_PRICE_3090="" MICROCENTER_LOCATION="" NVIDIA_ADD_TO_CART_ATTEMPTS="" NVIDIA_SESSION_TTL="" diff --git a/src/config.ts b/src/config.ts index a51346d541..e73967d4a4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -143,7 +143,9 @@ const proxy = { const store = { country: envOrString(process.env.COUNTRY, 'usa'), - maxPrice: envOrNumber(process.env.MAX_PRICE), + maxPrice3070: envOrNumber(process.env.MAX_PRICE_3070), + maxPrice3080: envOrNumber(process.env.MAX_PRICE_3080), + maxPrice3090: envOrNumber(process.env.MAX_PRICE_3090), microCenterLocation: envOrString(process.env.MICROCENTER_LOCATION, 'web'), showOnlyBrands: envOrArray(process.env.SHOW_ONLY_BRANDS), showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS), diff --git a/src/index.ts b/src/index.ts index 554b71c617..051805b9c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,6 +40,11 @@ async function main() { args.push(`--proxy-server=http://${config.proxy.address}:${config.proxy.port}`); } + // Check for deprecated configuration values + if (process.env.MAX_PRICE) { + logger.warn('ℹ MAX_PRICE is deprecated, please use MAX_PRICE_$[series]'); + } + const browser = await puppeteer.launch({ args, defaultViewport: { diff --git a/src/logger.ts b/src/logger.ts index e82dd08051..2b682d5b58 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -78,12 +78,12 @@ export const Print = { return `ℹ ${buildProductString(link, store)} :: IN STOCK, WAITING`; }, - maxPrice(link: Link, store: Store, price: number, color?: boolean): string { + maxPrice(link: Link, store: Store, price: number, setPrice: number, color?: boolean): string { if (color) { - return '✖ ' + buildProductString(link, store, true) + ' :: ' + chalk.yellow(`PRICE ${price} EXCEEDS LIMIT ${config.store.maxPrice}`); + return '✖ ' + buildProductString(link, store, true) + ' :: ' + chalk.yellow(`PRICE ${price} EXCEEDS LIMIT ${setPrice}`); } - return `✖ ${buildProductString(link, store)} :: PRICE ${price} EXCEEDS LIMIT ${config.store.maxPrice}`; + return `✖ ${buildProductString(link, store)} :: PRICE ${price} EXCEEDS LIMIT ${setPrice}`; }, message(message: string, topic: string, store: Store, color?: boolean): string { if (color) { diff --git a/src/store/lookup.ts b/src/store/lookup.ts index ae5adcb2a7..820ef65253 100644 --- a/src/store/lookup.ts +++ b/src/store/lookup.ts @@ -149,9 +149,27 @@ async function lookupCardInStock(store: Store, page: Page, link: Link) { } if (store.labels.maxPrice) { - const priceLimit = await cardPriceLimit(page, store.labels.maxPrice, config.store.maxPrice, baseOptions); + let priceLimit; + let maxPrice = 0; + switch (link.series) { + case '3070': + priceLimit = await cardPriceLimit(page, store.labels.maxPrice, config.store.maxPrice3070, baseOptions); + maxPrice = config.store.maxPrice3070; + break; + case '3080': + priceLimit = await cardPriceLimit(page, store.labels.maxPrice, config.store.maxPrice3080, baseOptions); + maxPrice = config.store.maxPrice3080; + break; + case '3090': + priceLimit = await cardPriceLimit(page, store.labels.maxPrice, config.store.maxPrice3090, baseOptions); + maxPrice = config.store.maxPrice3090; + break; + default: + break; + } + if (priceLimit) { - logger.info(Print.maxPrice(link, store, priceLimit, true)); + logger.info(Print.maxPrice(link, store, priceLimit, maxPrice, true)); return false; } }