feat: filter models (#261)

This commit is contained in:
admon84
2020-09-23 20:53:19 -06:00
committed by GitHub
parent d6a27c988c
commit e1b34a9ccf
7 changed files with 79 additions and 32 deletions
+1
View File
@@ -22,6 +22,7 @@ PUSHOVER_USER=""
PAGE_SLEEP_MIN="" PAGE_SLEEP_MIN=""
PAGE_SLEEP_MAX="" PAGE_SLEEP_MAX=""
SHOW_ONLY_BRANDS="" SHOW_ONLY_BRANDS=""
SHOW_ONLY_MODELS=""
SHOW_ONLY_SERIES="" SHOW_ONLY_SERIES=""
SLACK_CHANNEL="" SLACK_CHANNEL=""
SLACK_TOKEN="" SLACK_TOKEN=""
+1
View File
@@ -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` | | `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` | | `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_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` | | `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_CHANNEL` | Slack channel for posting | E.g.: `update`, no need for `#` |
| `SLACK_TOKEN` | Slack API token | | | `SLACK_TOKEN` | Slack API token | |
+1
View File
@@ -121,6 +121,7 @@ const store = {
country: envOrString(process.env.COUNTRY, 'usa'), country: envOrString(process.env.COUNTRY, 'usa'),
microCenterLocation: envOrString(process.env.MICROCENTER_LOCATION, 'web'), microCenterLocation: envOrString(process.env.MICROCENTER_LOCATION, 'web'),
showOnlyBrands: envOrArray(process.env.SHOW_ONLY_BRANDS), showOnlyBrands: envOrArray(process.env.SHOW_ONLY_BRANDS),
showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS),
showOnlySeries: envOrArray(process.env.SHOW_ONLY_SERIES, ['3070', '3080', '3090']), showOnlySeries: envOrArray(process.env.SHOW_ONLY_SERIES, ['3070', '3080', '3090']),
stores: envOrArray(process.env.STORES, ['nvidia']) stores: envOrArray(process.env.STORES, ['nvidia'])
}; };
+62
View File
@@ -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
View File
@@ -1,2 +1 @@
export * from './lookup'; export * from './lookup';
export * from './includes-labels';
+2 -31
View File
@@ -3,38 +3,13 @@ import {Link, Store} from './model';
import {Logger, Print} from '../logger'; import {Logger, Print} from '../logger';
import {closePage, delay, getSleepTime} from '../util'; import {closePage, delay, getSleepTime} from '../util';
import {Config} from '../config'; import {Config} from '../config';
import {filterStoreLink} from './filter';
import {includesLabels} from './includes-labels'; import {includesLabels} from './includes-labels';
import open from 'open'; import open from 'open';
import {sendNotification} from '../notification'; import {sendNotification} from '../notification';
const inStock: Record<string, boolean> = {}; 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 * Responsible for looking up information about a each product within
* a `Store`. It's important that we ignore `no-await-in-loop` here * 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) { async function lookup(browser: Browser, store: Store) {
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
for (const link of store.links) { for (const link of store.links) {
if (!filterSeries(link.series)) { if (!filterStoreLink(link)) {
continue;
}
if (!filterBrand(link.brand)) {
continue; continue;
} }
+12
View File
@@ -52,6 +52,18 @@ for (const name of Config.store.stores) {
Logger.info(` selected stores: ${Array.from(list.keys()).join(', ')}`); 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 const Stores = Array.from(list.values()) as Store[];
export * from './store'; export * from './store';