Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: max price per series #451

Merged
merged 20 commits into from
Oct 7, 2020
4 changes: 3 additions & 1 deletion .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -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=""
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,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),
jastheace marked this conversation as resolved.
Show resolved Hide resolved
microCenterLocation: envOrString(process.env.MICROCENTER_LOCATION, 'web'),
showOnlyBrands: envOrArray(process.env.SHOW_ONLY_BRANDS),
showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS),
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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: {
Expand Down
6 changes: 3 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 20 additions & 2 deletions src/store/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,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;
}
}
Expand Down