mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 01:47:39 +00:00
feat: filter models (#261)
This commit is contained in:
@@ -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=""
|
||||
|
||||
@@ -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 | |
|
||||
|
||||
@@ -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'])
|
||||
};
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
@@ -1,2 +1 @@
|
||||
export * from './lookup';
|
||||
export * from './includes-labels';
|
||||
|
||||
+2
-31
@@ -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<string, boolean> = {};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user