mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 12:17:37 +00:00
chore: refactor max price for series
Signed-off-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
+14
-3
@@ -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),
|
||||
|
||||
@@ -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
@@ -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) {
|
||||
|
||||
@@ -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
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user