chore: refactor max price for series

Signed-off-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
Jef LeCompte
2020-10-06 22:03:19 -04:00
parent 8adc07a03e
commit 86bef5a617
6 changed files with 32 additions and 26 deletions
+3 -1
View File
@@ -80,7 +80,9 @@ Here is a list of variables that you can use to customize your newly copied `.en
| `IN_STOCK_WAIT_TIME` | Time to wait between requests to the same link if it has that card in stock | In seconds, default: `0` |
| `LOG_LEVEL` | [Logging levels](https://github.com/winstonjs/winston#logging-levels) | Debugging related, default: `info` |
| `LOW_BANDWIDTH` | Blocks images/fonts to reduce traffic | Disables ad blocker, default: `false` |
| `MAX_PRICE` | Maximum price allowed for a match, applies to all cards (does not apply to these sites: Nvidia, Asus, EVGA) | Default: leave empty for no limit, otherwise enter a price (enter whole dollar amounts only, avoid use of: dollar symbols, commas, and periods.) e.g.: `1234` - Cards above `1234` will be skipped. |
| `MAX_PRICE_SERIES_3070` | Maximum price allowed for a match, applies 3070 series cards (does not apply to these sites: Nvidia, Asus, EVGA) | Default: leave empty for no limit, otherwise enter a price (enter whole dollar amounts only, avoid use of: dollar symbols, commas, and periods.) e.g.: `1234` - Cards above `1234` will be skipped. |
| `MAX_PRICE_SERIES_3080` | Maximum price allowed for a match, applies 3080 series cards (does not apply to these sites: Nvidia, Asus, EVGA) | Default: leave empty for no limit, otherwise enter a price (enter whole dollar amounts only, avoid use of: dollar symbols, commas, and periods.) e.g.: `1234` - Cards above `1234` will be skipped. |
| `MAX_PRICE_SERIES_3090` | Maximum price allowed for a match, applies 3090 series cards (does not apply to these sites: Nvidia, Asus, EVGA) | Default: leave empty for no limit, otherwise enter a price (enter whole dollar amounts only, avoid use of: dollar symbols, commas, and periods.) e.g.: `1234` - Cards above `1234` will be skipped. |
| `MICROCENTER_LOCATION` | Specific MicroCenter location to search | Default : `web` |
| `NVIDIA_ADD_TO_CART_ATTEMPTS` | The maximum number of times the `nvidia-api` add to cart feature will be attempted before failing | Default: `10` |
| `NVIDIA_SESSION_TTL` | The time in milliseconds to keep the cart active while using `nvidia-api` | Default: `60000` |
+14 -3
View File
@@ -1,7 +1,9 @@
import {banner} from './banner';
console.log(banner);
import {config as config_} from 'dotenv';
import {logger} from './logger';
import path from 'path';
config_({path: path.resolve(__dirname, '../.env')});
@@ -141,11 +143,20 @@ const proxy = {
port: envOrNumber(process.env.PROXY_PORT, 80)
};
// Check for deprecated configuration values
if (process.env.MAX_PRICE) {
logger.warn(' MAX_PRICE is deprecated, please use MAX_PRICE_SERIES_{{series}}');
}
const store = {
country: envOrString(process.env.COUNTRY, 'usa'),
maxPrice3070: envOrNumber(process.env.MAX_PRICE_3070),
maxPrice3080: envOrNumber(process.env.MAX_PRICE_3080),
maxPrice3090: envOrNumber(process.env.MAX_PRICE_3090),
maxPrice: {
series: {
3070: envOrNumber(process.env.MAX_PRICE_3070),
3080: envOrNumber(process.env.MAX_PRICE_3080),
3090: 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),
-5
View File
@@ -40,11 +40,6 @@ 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: {
+3 -3
View File
@@ -78,12 +78,12 @@ export const Print = {
return ` ${buildProductString(link, store)} :: IN STOCK, WAITING`;
},
maxPrice(link: Link, store: Store, price: number, setPrice: number, color?: boolean): string {
maxPrice(link: Link, store: Store, price: number, maxPrice: number, color?: boolean): string {
if (color) {
return '✖ ' + buildProductString(link, store, true) + ' :: ' + chalk.yellow(`PRICE ${price} EXCEEDS LIMIT ${setPrice}`);
return '✖ ' + buildProductString(link, store, true) + ' :: ' + chalk.yellow(`PRICE ${price} EXCEEDS LIMIT ${maxPrice}`);
}
return `${buildProductString(link, store)} :: PRICE ${price} EXCEEDS LIMIT ${setPrice}`;
return `${buildProductString(link, store)} :: PRICE ${price} EXCEEDS LIMIT ${maxPrice}`;
},
message(message: string, topic: string, store: Store, color?: boolean): string {
if (color) {
+2 -4
View File
@@ -53,7 +53,7 @@ export async function pageIncludesLabels(page: Page, query: LabelQuery, options:
}
export async function extractPageContents(page: Page, selector: Selector): Promise<string | null> {
const content = await page.evaluate((options: Selector) => {
return page.evaluate((options: Selector) => {
// eslint-disable-next-line no-undef
const element: globalThis.HTMLElement | null = document.querySelector(options.selector);
@@ -76,8 +76,6 @@ export async function extractPageContents(page: Page, selector: Selector): Promi
return 'Error: selector.type is unknown';
}
}, selector);
return content;
}
/**
@@ -91,7 +89,7 @@ export function includesLabels(domText: string, searchLabels: string[]): boolean
return searchLabels.some(label => domTextLowerCase.includes(label));
}
export async function cardPriceLimit(page: Page, query: Pricing, max: number, options: Selector) {
export async function cardPrice(page: Page, query: Pricing, max: number, options: Selector) {
if (!max) {
return null;
}
+10 -10
View File
@@ -1,7 +1,7 @@
import {Browser, Page, Response} from 'puppeteer';
import {Link, Store} from './model';
import {Print, logger} from '../logger';
import {Selector, cardPriceLimit, pageIncludesLabels} from './includes-labels';
import {Selector, cardPrice, pageIncludesLabels} from './includes-labels';
import {closePage, delay, getSleepTime, isStatusCodeInRange} from '../util';
import {config} from '../config';
import {disableBlockerInPage} from '../adblocker';
@@ -149,27 +149,27 @@ async function lookupCardInStock(store: Store, page: Page, link: Link) {
}
if (store.labels.maxPrice) {
let priceLimit;
let price;
let maxPrice = 0;
switch (link.series) {
case '3070':
priceLimit = await cardPriceLimit(page, store.labels.maxPrice, config.store.maxPrice3070, baseOptions);
maxPrice = config.store.maxPrice3070;
price = await cardPrice(page, store.labels.maxPrice, config.store.maxPrice.series['3070'], baseOptions);
maxPrice = config.store.maxPrice.series['3070'];
break;
case '3080':
priceLimit = await cardPriceLimit(page, store.labels.maxPrice, config.store.maxPrice3080, baseOptions);
maxPrice = config.store.maxPrice3080;
price = await cardPrice(page, store.labels.maxPrice, config.store.maxPrice.series['3080'], baseOptions);
maxPrice = config.store.maxPrice.series['3080'];
break;
case '3090':
priceLimit = await cardPriceLimit(page, store.labels.maxPrice, config.store.maxPrice3090, baseOptions);
maxPrice = config.store.maxPrice3090;
price = await cardPrice(page, store.labels.maxPrice, config.store.maxPrice.series['3080'], baseOptions);
maxPrice = config.store.maxPrice.series['3090'];
break;
default:
break;
}
if (priceLimit) {
logger.info(Print.maxPrice(link, store, priceLimit, maxPrice, true));
if (price) {
logger.info(Print.maxPrice(link, store, price, maxPrice, true));
return false;
}
}