diff --git a/.env-example b/.env-example index 4d5ef14..d470a64 100644 --- a/.env-example +++ b/.env-example @@ -22,6 +22,7 @@ PUSHOVER_USER="" PAGE_SLEEP_MIN="" PAGE_SLEEP_MAX="" SHOW_ONLY_BRANDS="" +SHOW_ONLY_MODELS="" SHOW_ONLY_SERIES="" SLACK_CHANNEL="" SLACK_TOKEN="" diff --git a/README.md b/README.md index ea1bc34..e9d4f2b 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ Here is a list of variables that you can use to customize your newly copied `.en | `PAGE_SLEEP_MAX` | Maximum sleep time between queries of the same store | In milliseconds, default: `10000` | | `SCREENSHOT` | Capture screenshot of page if a card is found | Default: `true` | | `SHOW_ONLY_BRANDS` | Filter to show specified brands | Comma separated, e.g.: `evga,zotac` | +| `SHOW_ONLY_MODELS` | Filter to show specified models | Comma separated, e.g.: `founders edition,rog strix` | | `SHOW_ONLY_SERIES` | Filter to show specified series | Comma separated, e.g.: `3080` | | `SLACK_CHANNEL` | Slack channel for posting | E.g.: `update`, no need for `#` | | `SLACK_TOKEN` | Slack API token | | diff --git a/src/config.ts b/src/config.ts index ffa6107..2957f57 100644 --- a/src/config.ts +++ b/src/config.ts @@ -121,6 +121,7 @@ const store = { country: envOrString(process.env.COUNTRY, 'usa'), microCenterLocation: envOrString(process.env.MICROCENTER_LOCATION, 'web'), showOnlyBrands: envOrArray(process.env.SHOW_ONLY_BRANDS), + showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS), showOnlySeries: envOrArray(process.env.SHOW_ONLY_SERIES, ['3070', '3080', '3090']), stores: envOrArray(process.env.STORES, ['nvidia']) }; diff --git a/src/store/filter.ts b/src/store/filter.ts new file mode 100644 index 0000000..c1d43bc --- /dev/null +++ b/src/store/filter.ts @@ -0,0 +1,62 @@ +import {Config} from '../config'; +import {Link} from './model'; + +/** + * Returns true if the brand should be checked for stock + * + * @param brand The brand of the GPU + */ +function filterBrand(brand: Link['brand']): boolean { + if (Config.store.showOnlyBrands.length === 0) { + return true; + } + + return Config.store.showOnlyBrands.includes(brand); +} + +/** + * Returns true if the model should be checked for stock + * + * @param model The model of the GPU + */ +function filterModel(model: Link['model']): boolean { + if (Config.store.showOnlyModels.length === 0) { + return true; + } + + const sanitizedModel = model.replace(/\s/g, ''); + for (const configModel of Config.store.showOnlyModels) { + const sanitizedConfigModel = configModel.replace(/\s/g, ''); + if (sanitizedModel === sanitizedConfigModel) { + return true; + } + } + + return false; +} + +/** + * Returns true if the series should be checked for stock + * + * @param series The series of the GPU + */ +function filterSeries(series: Link['series']): boolean { + if (Config.store.showOnlySeries.length === 0) { + return true; + } + + return Config.store.showOnlySeries.includes(series); +} + +/** + * Returns true if the link should be checked for stock + * + * @param link The store link of the GPU + */ +export function filterStoreLink(link: Link): boolean { + return ( + filterBrand(link.brand) && + filterModel(link.model) && + filterSeries(link.series) + ); +} diff --git a/src/store/index.ts b/src/store/index.ts index 126f358..ea07f9b 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,2 +1 @@ export * from './lookup'; -export * from './includes-labels'; diff --git a/src/store/lookup.ts b/src/store/lookup.ts index 6e4e57a..9d13e2d 100644 --- a/src/store/lookup.ts +++ b/src/store/lookup.ts @@ -3,38 +3,13 @@ import {Link, Store} from './model'; import {Logger, Print} from '../logger'; import {closePage, delay, getSleepTime} from '../util'; import {Config} from '../config'; +import {filterStoreLink} from './filter'; import {includesLabels} from './includes-labels'; import open from 'open'; import {sendNotification} from '../notification'; const inStock: Record = {}; -/** - * Returns true if the brand should be checked for stock - * - * @param brand The brand of the GPU - */ -function filterBrand(brand: Link['brand']) { - if (Config.store.showOnlyBrands.length === 0) { - return true; - } - - return Config.store.showOnlyBrands.includes(brand); -} - -/** - * Returns true if the series should be checked for stock - * - * @param series The series of the GPU - */ -function filterSeries(series: Link['series']) { - if (Config.store.showOnlySeries.length === 0) { - return true; - } - - return Config.store.showOnlySeries.includes(series); -} - /** * Responsible for looking up information about a each product within * a `Store`. It's important that we ignore `no-await-in-loop` here @@ -46,11 +21,7 @@ function filterSeries(series: Link['series']) { async function lookup(browser: Browser, store: Store) { /* eslint-disable no-await-in-loop */ for (const link of store.links) { - if (!filterSeries(link.series)) { - continue; - } - - if (!filterBrand(link.brand)) { + if (!filterStoreLink(link)) { continue; } diff --git a/src/store/model/index.ts b/src/store/model/index.ts index 434abe8..374e24b 100644 --- a/src/store/model/index.ts +++ b/src/store/model/index.ts @@ -52,6 +52,18 @@ for (const name of Config.store.stores) { Logger.info(`ℹ selected stores: ${Array.from(list.keys()).join(', ')}`); +if (Config.store.showOnlyBrands.length > 0) { + Logger.info(`ℹ selected brands: ${Config.store.showOnlyBrands.join(', ')}`); +} + +if (Config.store.showOnlyModels.length > 0) { + Logger.info(`ℹ selected models: ${Config.store.showOnlyModels.join(', ')}`); +} + +if (Config.store.showOnlySeries.length > 0) { + Logger.info(`ℹ selected series: ${Config.store.showOnlySeries.join(', ')}`); +} + export const Stores = Array.from(list.values()) as Store[]; export * from './store';