Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: filter models #261

Merged
merged 6 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -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=""
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
};
Expand Down
62 changes: 62 additions & 0 deletions src/store/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {Config} from '../config';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was on my todo list to refactor this out. Love it!

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 change: 0 additions & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './lookup';
export * from './includes-labels';
33 changes: 2 additions & 31 deletions src/store/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
12 changes: 12 additions & 0 deletions src/store/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';