Skip to content

Commit

Permalink
feat(notification): add price to links
Browse files Browse the repository at this point in the history
Resolves #1188
  • Loading branch information
jef committed Dec 5, 2020
1 parent f7b32e8 commit 7883a90
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 51 deletions.
67 changes: 50 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@slack/web-api": "^5.14.0",
"chalk": "^4.1.0",
"cheerio": "^1.0.0-rc.3",
"discord-webhook-node": "^1.1.8",
"discord.js": "^12.5.1",
"dotenv": "^8.2.0",
"messaging-api-telegram": "^1.0.1",
"mqtt": "^4.2.6",
Expand Down
1 change: 1 addition & 0 deletions src/__test__/notification-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
11 changes: 4 additions & 7 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
62 changes: 42 additions & 20 deletions src/notification/discord.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions src/store/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,17 @@ async function lookupCardInStock(store: Store, page: Page, link: Link) {
}

if (store.labels.maxPrice) {
const price = await cardPrice(
link.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));

if (link.price) {
logger.info(Print.maxPrice(link, store, maxPrice, true));
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/store/model/amazon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export const Amazon: Store = {
{
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',
Expand Down
1 change: 1 addition & 0 deletions src/store/model/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7883a90

Please sign in to comment.