Skip to content

Commit

Permalink
feat: max price per series (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
jastheace committed Oct 7, 2020
1 parent 160ae37 commit 8adc07a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .env-example
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
Expand Up @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Expand Up @@ -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: {
Expand Down
6 changes: 3 additions & 3 deletions src/logger.ts
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
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 8adc07a

Please sign in to comment.