feat: max price filtering (#383)

This commit is contained in:
xninjax
2020-10-02 10:59:06 -06:00
committed by GitHub
parent 02f9ed5e53
commit fd294d2baa
22 changed files with 107 additions and 5 deletions
+1
View File
@@ -13,6 +13,7 @@ HEADLESS=""
IN_STOCK_WAIT_TIME="" IN_STOCK_WAIT_TIME=""
LOG_LEVEL="" LOG_LEVEL=""
LOW_BANDWIDTH="" LOW_BANDWIDTH=""
MAX_PRICE=“”
MICROCENTER_LOCATION="" MICROCENTER_LOCATION=""
NVIDIA_ADD_TO_CART_ATTEMPTS="" NVIDIA_ADD_TO_CART_ATTEMPTS=""
NVIDIA_SESSION_TTL="" NVIDIA_SESSION_TTL=""
+5 -2
View File
@@ -4,10 +4,13 @@
build/ build/
node_modules/ node_modules/
.env .env*
.*env
!.env-example
success-*.png success-*.png
*.wav *.wav
*.mp3 *.mp3
*.flac *.flac
*.exe *.exe
desktop.ini
+2 -1
View File
@@ -81,7 +81,7 @@ Here is a list of variables that you can use to customize your newly copied `.en
| `LOW_BANDWIDTH` | Blocks images/fonts to reduce traffic | Disables ad blocker, default: `false` | | `LOW_BANDWIDTH` | Blocks images/fonts to reduce traffic | Disables ad blocker, default: `false` |
| `MICROCENTER_LOCATION` | Specific MicroCenter location to search | Default : `web` | | `MICROCENTER_LOCATION` | Specific MicroCenter location to search | Default : `web` |
| `NVIDIA_ADD_TO_CART_ATTEMPTS` | The maximum number of times the `nvidia-api` add to cart feature will be attempted before failing | Default: `10` | | `NVIDIA_ADD_TO_CART_ATTEMPTS` | The maximum number of times the `nvidia-api` add to cart feature will be attempted before failing | Default: `10` |
| `NVIDIA_SESSION_TTL` | The time in seconds to keep the cart active while using `nvidia-api` | Default: `60000` | | `NVIDIA_SESSION_TTL` | The time in milliseconds to keep the cart active while using `nvidia-api` | Default: `60000` |
| `OPEN_BROWSER` | Toggle for whether or not the browser should open when item is found | Default: `true` | | `OPEN_BROWSER` | Toggle for whether or not the browser should open when item is found | Default: `true` |
| `PAGE_TIMEOUT` | Navigation Timeout in milliseconds | `0` for infinite, default: `30000` | | `PAGE_TIMEOUT` | Navigation Timeout in milliseconds | `0` for infinite, default: `30000` |
| `PHONE_NUMBER` | 10 digit phone number | E.g.: `1234567890`, email configuration required | | `PHONE_NUMBER` | 10 digit phone number | E.g.: `1234567890`, email configuration required |
@@ -104,6 +104,7 @@ Here is a list of variables that you can use to customize your newly copied `.en
| `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 | |
| `STORES` | [Supported stores](#supported-stores) you want to be scraped | Comma separated, default: `nvidia` | | `STORES` | [Supported stores](#supported-stores) you want to be scraped | Comma separated, default: `nvidia` |
| `MAX_PRICE` | Maximum price allowed for a match, applies to all cards (does not apply to these sites: Nvidia, Asus, EVGA) | Default: leave empty for no limit, otherwise enter a price (enter whole dollar amounts only, avoid use of: dollar symbols, commas, and periods.) e.g.: `1234` - Cards above `1234` will be skipped. |
| `COUNTRY` | [Supported country](#supported-countries) you want to be scraped | Currently only used by Nvidia, default: `usa` | | `COUNTRY` | [Supported country](#supported-countries) you want to be scraped | Currently only used by Nvidia, default: `usa` |
| `SCREENSHOT` | Capture screenshot of page if a card is found | Default: `true` | | `SCREENSHOT` | Capture screenshot of page if a card is found | Default: `true` |
| `TELEGRAM_ACCESS_TOKEN` | Telegram access token | | | `TELEGRAM_ACCESS_TOKEN` | Telegram access token | |
+1
View File
@@ -140,6 +140,7 @@ const proxy = {
const store = { const store = {
country: envOrString(process.env.COUNTRY, 'usa'), country: envOrString(process.env.COUNTRY, 'usa'),
maxPrice: envOrNumber(process.env.MAX_PRICE),
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), showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS),
+7
View File
@@ -74,6 +74,13 @@ export const Print = {
return ` ${buildProductString(link, store)} :: IN STOCK, WAITING`; return ` ${buildProductString(link, store)} :: IN STOCK, WAITING`;
}, },
maxPrice(link: Link, store: Store, price: number, color?: boolean): string {
if (color) {
return '✖ ' + buildProductString(link, store, true) + ' :: ' + chalk.yellow(`PRICE ${price} EXCEEDS LIMIT ${Config.store.maxPrice}`);
}
return `${buildProductString(link, store)} :: PRICE ${price} EXCEEDS LIMIT ${Config.store.maxPrice}`;
},
message(message: string, topic: string, store: Store, color?: boolean): string { message(message: string, topic: string, store: Store, color?: boolean): string {
if (color) { if (color) {
return '✖ ' + buildSetupString(topic, store, true) + ' :: ' + chalk.yellow(message); return '✖ ' + buildSetupString(topic, store, true) + ' :: ' + chalk.yellow(message);
+20 -1
View File
@@ -1,4 +1,4 @@
import {Element, LabelQuery} from './model'; import {Element, LabelQuery, Pricing} from './model';
import {Logger} from '../logger'; import {Logger} from '../logger';
import {Page} from 'puppeteer'; import {Page} from 'puppeteer';
@@ -90,3 +90,22 @@ export function includesLabels(domText: string, searchLabels: string[]): boolean
const domTextLowerCase = domText.toLowerCase(); const domTextLowerCase = domText.toLowerCase();
return searchLabels.some(label => domTextLowerCase.includes(label)); return searchLabels.some(label => domTextLowerCase.includes(label));
} }
export async function cardPriceLimit(page: Page, query: Pricing, max: number, options: Selector) {
if (!max) {
return null;
}
const selector = {...options, selector: query.container};
const cardPrice = await extractPageContents(page, selector);
if (cardPrice) {
const priceSeperator = query.euroFormat ? /\./g : /,/g;
const cardpriceNumber = Number.parseFloat(cardPrice.replace(priceSeperator, '').match(/\d+/g)!.join('.'));
Logger.debug(`Raw card price: ${cardPrice} | Limit: ${max}`);
return cardpriceNumber > max ? cardpriceNumber : null;
}
return null;
}
+9 -1
View File
@@ -1,7 +1,7 @@
import {Browser, Page, Response} from 'puppeteer'; import {Browser, Page, Response} from 'puppeteer';
import {Link, Store} from './model'; import {Link, Store} from './model';
import {Logger, Print} from '../logger'; import {Logger, Print} from '../logger';
import {Selector, pageIncludesLabels} from './includes-labels'; import {Selector, cardPriceLimit, pageIncludesLabels} from './includes-labels';
import {closePage, delay, getSleepTime, isStatusCodeInRange} from '../util'; import {closePage, delay, getSleepTime, isStatusCodeInRange} from '../util';
import {Config} from '../config'; import {Config} from '../config';
import {disableBlockerInPage} from '../adblocker'; import {disableBlockerInPage} from '../adblocker';
@@ -145,6 +145,14 @@ async function lookupCardInStock(store: Store, page: Page, link: Link) {
} }
} }
if (store.labels.maxPrice) {
const priceLimit = await cardPriceLimit(page, store.labels.maxPrice, Config.store.maxPrice, baseOptions);
if (priceLimit) {
Logger.info(Print.maxPrice(link, store, priceLimit, true));
return false;
}
}
if (store.labels.captcha) { if (store.labels.captcha) {
if (await pageIncludesLabels(page, store.labels.captcha, baseOptions)) { if (await pageIncludesLabels(page, store.labels.captcha, baseOptions)) {
Logger.warn(Print.captcha(link, store, true)); Logger.warn(Print.captcha(link, store, true));
+4
View File
@@ -9,6 +9,10 @@ export const Adorama: Store = {
inStock: { inStock: {
container: '.buy-section.purchase', container: '.buy-section.purchase',
text: ['add to cart'] text: ['add to cart']
},
maxPrice: {
container: '.your-price',
euroFormat: false
} }
}, },
links: [ links: [
+4
View File
@@ -9,6 +9,10 @@ export const AmazonCa: Store = {
inStock: { inStock: {
container: '#desktop_buybox', container: '#desktop_buybox',
text: ['add to cart'] text: ['add to cart']
},
maxPrice: {
container: 'span[class*="PriceString"]',
euroFormat: false
} }
}, },
links: [ links: [
+4
View File
@@ -9,6 +9,10 @@ export const AmazonDe: Store = {
inStock: { inStock: {
container: '#desktop_buybox', container: '#desktop_buybox',
text: ['in den einkaufswagen'] text: ['in den einkaufswagen']
},
maxPrice: {
container: 'span[class*="PriceString"]',
euroFormat: true
} }
}, },
links: [ links: [
+4
View File
@@ -9,6 +9,10 @@ export const AmazonNl: Store = {
inStock: { inStock: {
container: '#desktop_buybox', container: '#desktop_buybox',
text: ['in winkelwagen plaatsen'] text: ['in winkelwagen plaatsen']
},
maxPrice: {
container: 'span[class*="PriceString"]',
euroFormat: true
} }
}, },
links: [ links: [
+3
View File
@@ -9,6 +9,9 @@ export const Amazon: Store = {
inStock: { inStock: {
container: '#desktop_buybox', container: '#desktop_buybox',
text: ['add to cart'] text: ['add to cart']
},
maxPrice: {
container: 'span[class*="PriceString"]'
} }
}, },
links: [ links: [
+4
View File
@@ -6,6 +6,10 @@ export const BAndH: Store = {
inStock: { inStock: {
container: 'div[data-selenium="addToCartSection"]', container: 'div[data-selenium="addToCartSection"]',
text: ['add to cart'] text: ['add to cart']
},
maxPrice: {
container: 'div[data-selenium="pricingPrice"]',
euroFormat: false
} }
}, },
links: [ links: [
+4
View File
@@ -5,6 +5,10 @@ export const BestBuyCa: Store = {
inStock: { inStock: {
container: '#root', container: '#root',
text: ['available online'] text: ['available online']
},
maxPrice: {
container: 'div[class^="productPricingContainer"] span[class^="screenReaderOnly_"',
euroFormat: false
} }
}, },
links: [ links: [
+4
View File
@@ -5,6 +5,10 @@ export const BestBuy: Store = {
inStock: { inStock: {
container: '.v-m-bottom-g', container: '.v-m-bottom-g',
text: ['add to cart'] text: ['add to cart']
},
maxPrice: {
container: 'div[class="priceView-hero-price priceView-customer-price"] > span',
euroFormat: false
} }
}, },
links: [ links: [
+4
View File
@@ -43,6 +43,10 @@ export const MicroCenter: Store = {
inStock: { inStock: {
container: '#cart-options', container: '#cart-options',
text: ['in stock'] text: ['in stock']
},
maxPrice: {
container: 'span[id="pricing"]',
euroFormat: false
} }
}, },
links: [ links: [
+4
View File
@@ -9,6 +9,10 @@ export const NeweggCa: Store = {
inStock: { inStock: {
container: '#landingpage-cart .btn-primary span', container: '#landingpage-cart .btn-primary span',
text: ['add to cart'] text: ['add to cart']
},
maxPrice: {
container: '#landingpage-price > div > div > ul > li.price-current > strong',
euroFormat: false
} }
}, },
links: [ links: [
+4
View File
@@ -9,6 +9,10 @@ export const Newegg: Store = {
inStock: { inStock: {
container: '#landingpage-cart .btn-primary span', container: '#landingpage-cart .btn-primary span',
text: ['add to cart'] text: ['add to cart']
},
maxPrice: {
container: '#landingpage-price > div > div > ul > li.price-current > strong',
euroFormat: false
} }
}, },
links: [ links: [
+4
View File
@@ -9,6 +9,10 @@ export const OfficeDepot: Store = {
inStock: { inStock: {
container: '#productPurchase', container: '#productPurchase',
text: ['add to cart'] text: ['add to cart']
},
maxPrice: {
container: 'span[class^="price_column right"]',
euroFormat: false
} }
}, },
links: [ links: [
+5
View File
@@ -5,7 +5,12 @@ export const Pny: Store = {
inStock: { inStock: {
container: '#ctl01_lbtnAddToCart', container: '#ctl01_lbtnAddToCart',
text: ['add to cart'] text: ['add to cart']
},
maxPrice: {
container: 'span[itemprop="price"]',
euroFormat: false
} }
}, },
links: [ links: [
{ {
+6
View File
@@ -5,6 +5,11 @@ export type Element = {
text: string[]; text: string[];
}; };
export type Pricing = {
container: string;
euroFormat?: boolean;
};
export type Series = 'test:series' | '3070' | '3080' | '3090'; export type Series = 'test:series' | '3070' | '3080' | '3090';
export type Link = { export type Link = {
@@ -25,6 +30,7 @@ export type Labels = {
container?: string; container?: string;
inStock?: LabelQuery; inStock?: LabelQuery;
outOfStock?: LabelQuery; outOfStock?: LabelQuery;
maxPrice?: Pricing;
}; };
export type StatusCodeRangeArray = Array<(number | [number, number])>; export type StatusCodeRangeArray = Array<(number | [number, number])>;
+4
View File
@@ -6,6 +6,10 @@ export const Zotac: Store = {
inStock: { inStock: {
container: '.add-to-cart-wrapper', container: '.add-to-cart-wrapper',
text: ['add to cart'] text: ['add to cart']
},
maxPrice: {
container: 'div[class="product-shop"] span[class="price"]',
euroFormat: false
} }
}, },
links: [ links: [