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: allow filtering per model by a specific series #595

Merged
merged 2 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Here is a list of variables that you can use to customize your newly copied `.en
| `PUSHOVER_PRIORITY` | Pushover message priority |
| `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_MODELS` | Filter to show specified models | Both supported formats are comma separated <br/><br/>1. Standard E.g.: `founders edition,rog strix` <br/><br/> 2. Advanced E.g: `MODEL:SERIES`, E.g: `founders edition:3090,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
8 changes: 7 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,13 @@ const store = {
},
microCenterLocation: envOrArray(process.env.MICROCENTER_LOCATION, ['web']),
showOnlyBrands: envOrArray(process.env.SHOW_ONLY_BRANDS),
showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS),
showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS).map(entry => {
const [name, series] = entry.match(/[^:]+/g) ?? [];
return {
name: envOrString(name),
series: envOrString(series)
};
}),
showOnlySeries: envOrArray(process.env.SHOW_ONLY_SERIES, ['3070', '3080', '3090']),
stores: envOrArray(process.env.STORES, ['nvidia']).map(entry => {
const [name, minPageSleep, maxPageSleep] = entry.match(/[^:]+/g) ?? [];
Expand Down
16 changes: 11 additions & 5 deletions src/store/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ function filterBrand(brand: Link['brand']): boolean {
* Returns true if the model should be checked for stock
*
* @param model The model of the GPU
* @param series The series of the GPU
*/
function filterModel(model: Link['model']): boolean {
function filterModel(model: Link['model'], series: Link['series']): 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) {
const sanitizedSeries = series.replace(/\s/g, '');
for (const configModelEntry of config.store.showOnlyModels) {
const sanitizedConfigModel = configModelEntry.name.replace(/\s/g, '');
const sanitizedConfigSeries = configModelEntry.series.replace(/\s/g, '');
if (sanitizedConfigSeries ?
sanitizedSeries === sanitizedConfigSeries && sanitizedModel === sanitizedConfigModel :
sanitizedModel === sanitizedConfigModel
) {
return true;
}
}
Expand Down Expand Up @@ -56,7 +62,7 @@ export function filterSeries(series: Link['series']): boolean {
export function filterStoreLink(link: Link): boolean {
return (
filterBrand(link.brand) &&
filterModel(link.model) &&
filterModel(link.model, link.series) &&
filterSeries(link.series)
);
}
4 changes: 3 additions & 1 deletion src/store/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ if (config.store.showOnlyBrands.length > 0) {
}

if (config.store.showOnlyModels.length > 0) {
logger.info(`ℹ selected models: ${config.store.showOnlyModels.join(', ')}`);
logger.info(`ℹ selected models: ${config.store.showOnlyModels.map(entry => {
return entry.series ? entry.name + ' (' + entry.series + ')' : entry.name;
}).join(', ')}`);
}

if (config.store.showOnlySeries.length > 0) {
Expand Down