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
+20 -1
View File
@@ -1,4 +1,4 @@
import {Element, LabelQuery} from './model';
import {Element, LabelQuery, Pricing} from './model';
import {Logger} from '../logger';
import {Page} from 'puppeteer';
@@ -90,3 +90,22 @@ export function includesLabels(domText: string, searchLabels: string[]): boolean
const domTextLowerCase = domText.toLowerCase();
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 {Link, Store} from './model';
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 {Config} from '../config';
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 (await pageIncludesLabels(page, store.labels.captcha, baseOptions)) {
Logger.warn(Print.captcha(link, store, true));
+4
View File
@@ -9,6 +9,10 @@ export const Adorama: Store = {
inStock: {
container: '.buy-section.purchase',
text: ['add to cart']
},
maxPrice: {
container: '.your-price',
euroFormat: false
}
},
links: [
+4
View File
@@ -9,6 +9,10 @@ export const AmazonCa: Store = {
inStock: {
container: '#desktop_buybox',
text: ['add to cart']
},
maxPrice: {
container: 'span[class*="PriceString"]',
euroFormat: false
}
},
links: [
+4
View File
@@ -9,6 +9,10 @@ export const AmazonDe: Store = {
inStock: {
container: '#desktop_buybox',
text: ['in den einkaufswagen']
},
maxPrice: {
container: 'span[class*="PriceString"]',
euroFormat: true
}
},
links: [
+4
View File
@@ -9,6 +9,10 @@ export const AmazonNl: Store = {
inStock: {
container: '#desktop_buybox',
text: ['in winkelwagen plaatsen']
},
maxPrice: {
container: 'span[class*="PriceString"]',
euroFormat: true
}
},
links: [
+3
View File
@@ -9,6 +9,9 @@ export const Amazon: Store = {
inStock: {
container: '#desktop_buybox',
text: ['add to cart']
},
maxPrice: {
container: 'span[class*="PriceString"]'
}
},
links: [
+4
View File
@@ -6,6 +6,10 @@ export const BAndH: Store = {
inStock: {
container: 'div[data-selenium="addToCartSection"]',
text: ['add to cart']
},
maxPrice: {
container: 'div[data-selenium="pricingPrice"]',
euroFormat: false
}
},
links: [
+4
View File
@@ -5,6 +5,10 @@ export const BestBuyCa: Store = {
inStock: {
container: '#root',
text: ['available online']
},
maxPrice: {
container: 'div[class^="productPricingContainer"] span[class^="screenReaderOnly_"',
euroFormat: false
}
},
links: [
+4
View File
@@ -5,6 +5,10 @@ export const BestBuy: Store = {
inStock: {
container: '.v-m-bottom-g',
text: ['add to cart']
},
maxPrice: {
container: 'div[class="priceView-hero-price priceView-customer-price"] > span',
euroFormat: false
}
},
links: [
+4
View File
@@ -43,6 +43,10 @@ export const MicroCenter: Store = {
inStock: {
container: '#cart-options',
text: ['in stock']
},
maxPrice: {
container: 'span[id="pricing"]',
euroFormat: false
}
},
links: [
+4
View File
@@ -9,6 +9,10 @@ export const NeweggCa: Store = {
inStock: {
container: '#landingpage-cart .btn-primary span',
text: ['add to cart']
},
maxPrice: {
container: '#landingpage-price > div > div > ul > li.price-current > strong',
euroFormat: false
}
},
links: [
+4
View File
@@ -9,6 +9,10 @@ export const Newegg: Store = {
inStock: {
container: '#landingpage-cart .btn-primary span',
text: ['add to cart']
},
maxPrice: {
container: '#landingpage-price > div > div > ul > li.price-current > strong',
euroFormat: false
}
},
links: [
+4
View File
@@ -9,6 +9,10 @@ export const OfficeDepot: Store = {
inStock: {
container: '#productPurchase',
text: ['add to cart']
},
maxPrice: {
container: 'span[class^="price_column right"]',
euroFormat: false
}
},
links: [
+5
View File
@@ -5,7 +5,12 @@ export const Pny: Store = {
inStock: {
container: '#ctl01_lbtnAddToCart',
text: ['add to cart']
},
maxPrice: {
container: 'span[itemprop="price"]',
euroFormat: false
}
},
links: [
{
+6
View File
@@ -5,6 +5,11 @@ export type Element = {
text: string[];
};
export type Pricing = {
container: string;
euroFormat?: boolean;
};
export type Series = 'test:series' | '3070' | '3080' | '3090';
export type Link = {
@@ -25,6 +30,7 @@ export type Labels = {
container?: string;
inStock?: LabelQuery;
outOfStock?: LabelQuery;
maxPrice?: Pricing;
};
export type StatusCodeRangeArray = Array<(number | [number, number])>;
+4
View File
@@ -6,6 +6,10 @@ export const Zotac: Store = {
inStock: {
container: '.add-to-cart-wrapper',
text: ['add to cart']
},
maxPrice: {
container: 'div[class="product-shop"] span[class="price"]',
euroFormat: false
}
},
links: [