mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 04:07:36 +00:00
feat(notification): add price to links (#1209)
fix(store): label selection ordering and pricing fix(nvidia): deprecation notice removed outside of usa fix(amazon): maxPrice selector Resolves #1188 Resolves #673 Resolves #1187
This commit is contained in:
@@ -5,6 +5,7 @@ const link: Link = {
|
||||
brand: 'test:brand',
|
||||
cartUrl: 'https://www.example.com/cartUrl',
|
||||
model: 'test:model',
|
||||
price: 100,
|
||||
series: 'test:series',
|
||||
url: 'https://www.example.com/url'
|
||||
};
|
||||
|
||||
+1
-1
@@ -180,7 +180,7 @@ const notifications = {
|
||||
desktop: process.env.DESKTOP_NOTIFICATIONS === 'true',
|
||||
discord: {
|
||||
notifyGroup: envOrArray(process.env.DISCORD_NOTIFY_GROUP),
|
||||
webHookUrl: envOrArray(process.env.DISCORD_WEB_HOOK)
|
||||
webhooks: envOrArray(process.env.DISCORD_WEB_HOOK)
|
||||
},
|
||||
email: {
|
||||
password: envOrString(process.env.EMAIL_PASSWORD),
|
||||
|
||||
+4
-7
@@ -118,11 +118,9 @@ export const Print = {
|
||||
|
||||
return `ℹ ${buildProductString(link, store)} :: IN STOCK, WAITING`;
|
||||
},
|
||||
// eslint-disable-next-line max-params
|
||||
maxPrice(
|
||||
link: Link,
|
||||
store: Store,
|
||||
price: number,
|
||||
maxPrice: number,
|
||||
color?: boolean
|
||||
): string {
|
||||
@@ -131,14 +129,13 @@ export const Print = {
|
||||
'✖ ' +
|
||||
buildProductString(link, store, true) +
|
||||
' :: ' +
|
||||
chalk.yellow(`PRICE ${price} EXCEEDS LIMIT ${maxPrice}`)
|
||||
chalk.yellow(`PRICE ${link.price ?? ''} EXCEEDS LIMIT ${maxPrice}`)
|
||||
);
|
||||
}
|
||||
|
||||
return `✖ ${buildProductString(
|
||||
link,
|
||||
store
|
||||
)} :: PRICE ${price} EXCEEDS LIMIT ${maxPrice}`;
|
||||
return `✖ ${buildProductString(link, store)} :: PRICE ${
|
||||
link.price ?? ''
|
||||
} EXCEEDS LIMIT ${maxPrice}`;
|
||||
},
|
||||
message(
|
||||
message: string,
|
||||
|
||||
+42
-20
@@ -1,41 +1,63 @@
|
||||
import {Link, Store} from '../store/model';
|
||||
import {MessageBuilder, Webhook} from 'discord-webhook-node';
|
||||
import Discord from 'discord.js';
|
||||
import {config} from '../config';
|
||||
import {logger} from '../logger';
|
||||
|
||||
const discord = config.notifications.discord;
|
||||
const hooks = discord.webHookUrl;
|
||||
const notifyGroup = discord.notifyGroup;
|
||||
const {notifyGroup, webhooks} = discord;
|
||||
|
||||
function getIdAndToken(webhook: string) {
|
||||
const match = /.*\/webhooks\/(\d+)\/(.+)/.exec(webhook);
|
||||
|
||||
if (!match) {
|
||||
throw new Error('could not get discord webhook');
|
||||
}
|
||||
|
||||
return {
|
||||
id: match[1],
|
||||
token: match[2]
|
||||
};
|
||||
}
|
||||
|
||||
export function sendDiscordMessage(link: Link, store: Store) {
|
||||
if (discord.webHookUrl.length > 0) {
|
||||
if (webhooks.length > 0) {
|
||||
logger.debug('↗ sending discord message');
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const embed = new MessageBuilder();
|
||||
embed.setTitle('Stock Notification');
|
||||
if (link.cartUrl)
|
||||
embed.addField('Add To Cart Link', link.cartUrl, true);
|
||||
embed.addField('Product Page', link.url, true);
|
||||
const embed = new Discord.MessageEmbed()
|
||||
.setTitle('_**Stock alert!**_')
|
||||
.setDescription(
|
||||
'> provided by [streetmerchant](https://github.com/jef/streetmerchant) with :heart:'
|
||||
)
|
||||
.setThumbnail(
|
||||
'https://raw.githubusercontent.com/jef/streetmerchant/main/media/streetmerchant-square.png'
|
||||
)
|
||||
.setColor('#52b788')
|
||||
.setTimestamp();
|
||||
|
||||
embed.addField('Store', store.name, true);
|
||||
if (link.price) embed.addField('Price', `$${link.price}`, true);
|
||||
embed.addField('Product Page', link.url);
|
||||
if (link.cartUrl) embed.addField('Add to Cart', link.cartUrl);
|
||||
embed.addField('Brand', link.brand, true);
|
||||
embed.addField('Series', link.series, true);
|
||||
embed.addField('Model', link.model, true);
|
||||
|
||||
if (notifyGroup) {
|
||||
embed.setText(notifyGroup.join(' '));
|
||||
}
|
||||
|
||||
embed.setColor(0x76b900);
|
||||
embed.setTimestamp();
|
||||
embed.addField('Series', link.series, true);
|
||||
|
||||
const promises = [];
|
||||
for (const hook of hooks) {
|
||||
promises.push(new Webhook(hook).send(embed));
|
||||
for (const webhook of webhooks) {
|
||||
const {id, token} = getIdAndToken(webhook);
|
||||
const client = new Discord.WebhookClient(id, token);
|
||||
promises.push({
|
||||
client,
|
||||
message: client.send(notifyGroup.join(' '), {
|
||||
embeds: [embed],
|
||||
username: 'streetmerchant'
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
(await Promise.all(promises)).forEach(({client}) => client.destroy());
|
||||
|
||||
logger.info('✔ discord message sent');
|
||||
} catch (error: unknown) {
|
||||
|
||||
@@ -116,27 +116,22 @@ export function includesLabels(
|
||||
);
|
||||
}
|
||||
|
||||
export async function cardPrice(
|
||||
export async function getPrice(
|
||||
page: Page,
|
||||
query: Pricing,
|
||||
max: number,
|
||||
options: Selector
|
||||
): Promise<number | null> {
|
||||
if (!max || max === -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const selector = {...options, selector: query.container};
|
||||
const cardPrice = await extractPageContents(page, selector);
|
||||
const priceString = await extractPageContents(page, selector);
|
||||
|
||||
if (cardPrice) {
|
||||
const priceSeperator = query.euroFormat ? /\./g : /,/g;
|
||||
const cardpriceNumber = Number.parseFloat(
|
||||
cardPrice.replace(priceSeperator, '').match(/\d+/g)!.join('.')
|
||||
if (priceString) {
|
||||
const priceSeparator = query.euroFormat ? /\./g : /,/g;
|
||||
const price = Number.parseFloat(
|
||||
priceString.replace(priceSeparator, '').match(/\d+/g)!.join('.')
|
||||
);
|
||||
|
||||
logger.debug(`Raw card price: ${cardPrice} | Limit: ${max}`);
|
||||
return cardpriceNumber > max ? cardpriceNumber : null;
|
||||
logger.debug('received price', price);
|
||||
return price;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
+38
-41
@@ -1,7 +1,7 @@
|
||||
import {Browser, Page, PageEventObj, Request, Response} from 'puppeteer';
|
||||
import {Link, Store, getStores} from './model';
|
||||
import {Print, logger} from '../logger';
|
||||
import {Selector, cardPrice, pageIncludesLabels} from './includes-labels';
|
||||
import {Selector, getPrice, pageIncludesLabels} from './includes-labels';
|
||||
import {
|
||||
closePage,
|
||||
delay,
|
||||
@@ -303,6 +303,43 @@ async function lookupCardInStock(store: Store, page: Page, link: Link) {
|
||||
type: 'textContent'
|
||||
};
|
||||
|
||||
if (store.labels.captcha) {
|
||||
if (await pageIncludesLabels(page, store.labels.captcha, baseOptions)) {
|
||||
logger.warn(Print.captcha(link, store, true));
|
||||
await delay(getSleepTime(store));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (store.labels.bannedSeller) {
|
||||
if (
|
||||
await pageIncludesLabels(page, store.labels.bannedSeller, baseOptions)
|
||||
) {
|
||||
logger.warn(Print.bannedSeller(link, store, true));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (store.labels.maxPrice) {
|
||||
const maxPrice = config.store.maxPrice.series[link.series];
|
||||
|
||||
link.price = await getPrice(page, store.labels.maxPrice, baseOptions);
|
||||
|
||||
if (link.price && link.price > maxPrice && maxPrice > 0) {
|
||||
logger.info(Print.maxPrice(link, store, maxPrice, true));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Fixme: currently causing issues
|
||||
// Do API inventory validation in realtime (no cache) if available
|
||||
// if (
|
||||
// store.realTimeInventoryLookup !== undefined &&
|
||||
// link.itemNumber !== undefined
|
||||
// ) {
|
||||
// return store.realTimeInventoryLookup(link.itemNumber);
|
||||
// }
|
||||
|
||||
if (store.labels.inStock) {
|
||||
const options = {
|
||||
...baseOptions,
|
||||
@@ -336,46 +373,6 @@ async function lookupCardInStock(store: Store, page: Page, link: Link) {
|
||||
}
|
||||
}
|
||||
|
||||
if (store.labels.bannedSeller) {
|
||||
if (
|
||||
await pageIncludesLabels(page, store.labels.bannedSeller, baseOptions)
|
||||
) {
|
||||
logger.warn(Print.bannedSeller(link, store, true));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (store.labels.maxPrice) {
|
||||
const price = await cardPrice(
|
||||
page,
|
||||
store.labels.maxPrice,
|
||||
config.store.maxPrice.series[link.series],
|
||||
baseOptions
|
||||
);
|
||||
const maxPrice = config.store.maxPrice.series[link.series];
|
||||
if (price) {
|
||||
logger.info(Print.maxPrice(link, store, price, maxPrice, true));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (store.labels.captcha) {
|
||||
if (await pageIncludesLabels(page, store.labels.captcha, baseOptions)) {
|
||||
logger.warn(Print.captcha(link, store, true));
|
||||
await delay(getSleepTime(store));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Fixme: currently causing issues
|
||||
// Do API inventory validation in realtime (no cache) if available
|
||||
// if (
|
||||
// store.realTimeInventoryLookup !== undefined &&
|
||||
// link.itemNumber !== undefined
|
||||
// ) {
|
||||
// return store.realTimeInventoryLookup(link.itemNumber);
|
||||
// }
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,7 @@ export const AmazonCa: Store = {
|
||||
text: ['add to cart']
|
||||
},
|
||||
maxPrice: {
|
||||
container: 'span[class*="PriceString"]',
|
||||
euroFormat: false
|
||||
container: '#priceblock_ourprice'
|
||||
}
|
||||
},
|
||||
links: [
|
||||
|
||||
@@ -12,8 +12,7 @@ export const AmazonEs: Store = {
|
||||
text: ['añadir a la cesta']
|
||||
},
|
||||
maxPrice: {
|
||||
container: 'span[class*="PriceString"]',
|
||||
euroFormat: true
|
||||
container: '#priceblock_ourprice'
|
||||
},
|
||||
outOfStock: [
|
||||
{
|
||||
|
||||
@@ -12,7 +12,7 @@ export const AmazonFr: Store = {
|
||||
text: ['ajouter au panier']
|
||||
},
|
||||
maxPrice: {
|
||||
container: 'span[class*="PriceString"]',
|
||||
container: '#priceblock_ourprice',
|
||||
euroFormat: true
|
||||
},
|
||||
outOfStock: [
|
||||
|
||||
@@ -12,7 +12,7 @@ export const AmazonIt: Store = {
|
||||
text: ['Aggiungi al carrello']
|
||||
},
|
||||
maxPrice: {
|
||||
container: 'span[class*="PriceString"]'
|
||||
container: '#priceblock_ourprice'
|
||||
}
|
||||
},
|
||||
links: [
|
||||
|
||||
@@ -16,7 +16,7 @@ export const AmazonNl: Store = {
|
||||
]
|
||||
},
|
||||
maxPrice: {
|
||||
container: 'span[class*="PriceString"]',
|
||||
container: '#priceblock_ourprice',
|
||||
euroFormat: true
|
||||
},
|
||||
outOfStock: [
|
||||
|
||||
@@ -14,7 +14,7 @@ export const AmazonUk: Store = {
|
||||
text: ['in stock']
|
||||
},
|
||||
maxPrice: {
|
||||
container: 'span[class*="PriceString"]'
|
||||
container: '#priceblock_ourprice'
|
||||
},
|
||||
outOfStock: [
|
||||
{
|
||||
|
||||
@@ -18,17 +18,17 @@ export const Amazon: Store = {
|
||||
}
|
||||
],
|
||||
maxPrice: {
|
||||
container: '#price_inside_buybox'
|
||||
container: '#priceblock_ourprice'
|
||||
}
|
||||
},
|
||||
links: [
|
||||
{
|
||||
brand: 'test:brand',
|
||||
cartUrl:
|
||||
'https://www.amazon.com/gp/aws/cart/add.html?ASIN.1=B07TDN1SC5&Quantity.1=1',
|
||||
'https://www.amazon.com/gp/aws/cart/add.html?ASIN.1=B083248S3B&Quantity.1=1',
|
||||
model: 'test:model',
|
||||
series: 'test:series',
|
||||
url: 'https://www.amazon.com/dp/B07TDN1SC5'
|
||||
url: 'https://www.amazon.com/dp/B083248S3B'
|
||||
},
|
||||
{
|
||||
brand: 'asus',
|
||||
|
||||
@@ -232,7 +232,8 @@ function warnIfStoreDeprecated(store: Store) {
|
||||
switch (store.name) {
|
||||
case 'nvidia':
|
||||
case 'nvidia-api':
|
||||
logger.warn(`${store.name} is deprecated in favor of bestbuy`);
|
||||
if (config.store.country === 'usa')
|
||||
logger.warn(`${store.name} is deprecated in favor of bestbuy`);
|
||||
break;
|
||||
case 'evga':
|
||||
logger.warn(
|
||||
|
||||
@@ -137,6 +137,7 @@ export type Link = {
|
||||
labels?: Labels;
|
||||
model: Model;
|
||||
openCartAction?: (browser: Browser) => Promise<string>;
|
||||
price?: number | null;
|
||||
series: Series;
|
||||
screenshot?: string;
|
||||
url: string;
|
||||
|
||||
Reference in New Issue
Block a user