chore(misc): normalize logs (#242)

This commit is contained in:
Jef LeCompte
2020-09-23 14:20:06 -04:00
committed by GitHub
parent 8466f9f398
commit 7c50e2b5aa
21 changed files with 239 additions and 183 deletions
+7 -7
View File
@@ -1,14 +1,14 @@
import {Link} from '../store/model';
import {Link, Store} from '../store/model';
import {Logger, Print} from '../logger';
import notifier from 'node-notifier';
export function sendDesktopNotification(cartUrl: string, link: Link) {
export function sendDesktopNotification(link: Link, store: Store) {
(async () => {
const title = link.brand + ' ' + link.model + ' IN STOCK';
const message = cartUrl;
notifier.notify({
message,
title
message: link.cartUrl ? link.cartUrl : link.url,
title: Print.inStock(link, store)
});
Logger.info('✔ desktop notification sent');
})();
}
+7 -5
View File
@@ -1,17 +1,18 @@
import {Link, Store} from '../store/model';
import {MessageBuilder, Webhook} from 'discord-webhook-node';
import {Config} from '../config';
import {Link} from '../store/model';
import {Logger} from '../logger';
const hook = new Webhook(Config.notifications.discord.webHookUrl);
const notifyGroup = Config.notifications.discord.notifyGroup;
export function sendDiscordMessage(cartUrl: string, link: Link) {
export function sendDiscordMessage(link: Link, store: Store) {
(async () => {
try {
const embed = new MessageBuilder();
embed.setTitle('Stock Notification');
embed.addField('URL', cartUrl, true);
embed.addField('URL', link.cartUrl ? link.cartUrl : link.url, true);
embed.addField('Store', store.name, true);
embed.addField('Brand', link.brand, true);
embed.addField('Model', link.model, true);
@@ -22,9 +23,10 @@ export function sendDiscordMessage(cartUrl: string, link: Link) {
embed.setColor(0x76B900);
embed.setTimestamp();
await hook.send(embed);
Logger.info(`↗ discord message sent: ${cartUrl}`);
Logger.info('✔ discord message sent');
} catch (error) {
Logger.error(error);
Logger.error('✖ couldn\'t send discord message', error);
}
})();
}
+14 -20
View File
@@ -1,11 +1,10 @@
import {Link, Store} from '../store/model';
import {Logger, Print} from '../logger';
import {Config} from '../config';
import {Link} from '../store/model';
import {Logger} from '../logger';
import Mail from 'nodemailer/lib/mailer';
import nodemailer from 'nodemailer';
const email = Config.notifications.email;
const subject = 'NVIDIA - BUY NOW';
const transporter = nodemailer.createTransport({
auth: {
@@ -15,30 +14,25 @@ const transporter = nodemailer.createTransport({
service: 'gmail'
});
const mailOptions: Mail.Options = {
from: email.username,
subject,
to: email.username
};
export function sendEmail(cartUrl: string, link: Link) {
mailOptions.text = cartUrl;
if (link.screenshot) {
mailOptions.attachments = [
export function sendEmail(link: Link, store: Store) {
const mailOptions: Mail.Options = {
attachments: link.screenshot ? [
{
filename: link.screenshot,
path: `./${link.screenshot}`
}
];
}
] : undefined,
from: email.username,
subject: Print.inStock(link, store),
text: link.cartUrl ? link.cartUrl : link.url,
to: email.username
};
transporter.sendMail(mailOptions, (error, info) => {
transporter.sendMail(mailOptions, error => {
if (error) {
Logger.error(error);
Logger.error('✖ couldn\'t send email', error);
} else {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Logger.info(`↗ email sent: ${info.response}`);
Logger.info('✔ email sent');
}
});
}
+37 -26
View File
@@ -1,5 +1,6 @@
import {Link, Store} from '../store/model';
import {Config} from '../config';
import {Link} from '../store/model';
import {Logger} from '../logger';
import {playSound} from './sound';
import {sendDesktopNotification} from './desktop';
import {sendDiscordMessage} from './discord';
@@ -13,44 +14,53 @@ import {sendTweet} from './twitter';
const notifications = Config.notifications;
export function sendNotification(cartUrl: string, link: Link) {
export function sendNotification(link: Link, store: Store) {
if (notifications.email.username && notifications.email.password) {
sendEmail(cartUrl, link);
}
if (notifications.slack.channel && notifications.slack.token) {
sendSlackMessage(cartUrl);
}
if (notifications.telegram.accessToken && notifications.telegram.chatId) {
sendTelegramMessage(cartUrl);
}
if (notifications.discord.webHookUrl) {
sendDiscordMessage(cartUrl, link);
Logger.debug('↗ sending email');
sendEmail(link, store);
}
if (notifications.phone.number) {
Logger.debug('↗ sending sms');
const carrier = notifications.phone.carrier.toLowerCase();
if (carrier && notifications.phone.availableCarriers.has(carrier)) {
sendSMS(cartUrl, link);
sendSMS(link, store);
}
}
if (notifications.pushBulletApiKey) {
sendPushBulletNotification(cartUrl, link);
}
if (notifications.pushover.token && notifications.pushover.username) {
sendPushoverNotification(cartUrl);
}
if (notifications.playSound) {
Logger.debug('↗ playing sound');
playSound();
}
if (notifications.desktop) {
sendDesktopNotification(cartUrl, link);
Logger.debug('↗ sending desktop notification');
sendDesktopNotification(link, store);
}
if (notifications.discord.webHookUrl) {
Logger.debug('↗ sending discord message');
sendDiscordMessage(link, store);
}
if (notifications.slack.channel && notifications.slack.token) {
Logger.debug('↗ sending slack message');
sendSlackMessage(link, store);
}
if (notifications.telegram.accessToken && notifications.telegram.chatId) {
Logger.debug('↗ sending telegram message');
sendTelegramMessage(link, store);
}
if (notifications.pushBulletApiKey) {
Logger.debug('↗ sending pushbullet message');
sendPushBulletNotification(link, store);
}
if (notifications.pushover.token && notifications.pushover.username) {
Logger.debug('↗ sending pushover message');
sendPushoverNotification(link, store);
}
if (
@@ -59,6 +69,7 @@ export function sendNotification(cartUrl: string, link: Link) {
notifications.twitter.consumerKey &&
notifications.twitter.consumerSecret
) {
sendTweet(cartUrl, link);
Logger.debug('↗ sending twitter message');
sendTweet(link, store);
}
}
+14 -11
View File
@@ -1,19 +1,22 @@
import {Link, Store} from '../store/model';
import {Logger, Print} from '../logger';
import {Config} from '../config';
import {Link} from '../store/model';
import {Logger} from '../logger';
import PushBullet from 'pushbullet';
const pushBulletApiKey = Config.notifications.pushBulletApiKey;
export function sendPushBulletNotification(cartUrl: string, link: Link) {
export function sendPushBulletNotification(link: Link, store: Store) {
const pusher = new PushBullet(pushBulletApiKey);
const title = `🚨 ${link.brand} ${link.model} ${link.series} 👀`;
pusher.note({}, title, cartUrl, (err: Error, result: string) => {
if (err) {
Logger.error(err);
} else {
Logger.info(`↗ pushbullet notification sent: ${result}`);
}
});
pusher.note(
{},
Print.inStock(link, store),
link.cartUrl ? link.cartUrl : link.url,
(error: Error) => {
if (error) {
Logger.error('✖ couldn\'t send pushbullet message', error);
} else {
Logger.info('✔ pushbullet message sent');
}
});
}
+9 -7
View File
@@ -1,5 +1,6 @@
import {Link, Store} from '../store/model';
import {Logger, Print} from '../logger';
import {Config} from '../config';
import {Logger} from '../logger';
import Push from 'pushover-notifications';
const pushover = Config.notifications.pushover;
@@ -8,16 +9,17 @@ const push = new Push({
user: pushover.username
});
export function sendPushoverNotification(cartUrl: string) {
export function sendPushoverNotification(link: Link, store: Store) {
const message = {
message: cartUrl
message: link.cartUrl ? link.cartUrl : link.url,
title: Print.inStock(link, store)
};
push.send(message, (err: Error, result: string) => {
if (err) {
Logger.error(err);
push.send(message, (error: Error) => {
if (error) {
Logger.error('✖ couldn\'t send pushover message', error);
} else {
Logger.info(` pushover notification sent: ${result}`);
Logger.info('✔ pushover message sent');
}
});
}
+13 -6
View File
@@ -1,23 +1,30 @@
import {Link, Store} from '../store/model';
import {Logger, Print} from '../logger';
import {Config} from '../config';
import {Logger} from '../logger';
import {WebClient} from '@slack/web-api';
const channel = Config.notifications.slack.channel;
const token = Config.notifications.slack.token;
const web = new WebClient(token);
export function sendSlackMessage(cartUrl: string) {
export function sendSlackMessage(link: Link, store: Store) {
(async () => {
const givenUrl = link.cartUrl ? link.cartUrl : link.url;
try {
const result = await web.chat.postMessage({channel, text: cartUrl});
const result = await web.chat.postMessage({
channel,
text: `${Print.inStock(link, store)}\n${givenUrl}`
});
if (!result.ok) {
Logger.error(result.error);
Logger.error('✖ couldn\'t send slack message', result);
return;
}
Logger.info(` slack message sent to '${channel}': ${cartUrl}`);
Logger.info('✔ slack message sent');
} catch (error) {
Logger.error(error);
Logger.error('✖ couldn\'t send slack message', error);
}
})();
}
+17 -20
View File
@@ -1,10 +1,9 @@
import {Link, Store} from '../store/model';
import {Logger, Print} from '../logger';
import {Config} from '../config';
import {Link} from '../store/model';
import {Logger} from '../logger';
import Mail from 'nodemailer/lib/mailer';
import nodemailer from 'nodemailer';
const subject = 'NVIDIA - BUY NOW';
const [email, phone] = [Config.notifications.email, Config.notifications.phone];
const transporter = nodemailer.createTransport({
@@ -15,37 +14,35 @@ const transporter = nodemailer.createTransport({
service: 'gmail'
});
const mailOptions: Mail.Options = {
from: Config.notifications.email.username,
subject,
to: generateAddress()
};
export function sendSMS(text: string, link: Link) {
mailOptions.text = text;
if (link.screenshot) {
mailOptions.attachments = [
export function sendSMS(link: Link, store: Store) {
const mailOptions: Mail.Options = {
attachments: link.screenshot ? [
{
filename: link.screenshot,
path: `./${link.screenshot}`
}
];
}
] : undefined,
from: email.username,
subject: Print.inStock(link, store),
text: link.cartUrl ? link.cartUrl : link.url,
to: generateAddress()
};
transporter.sendMail(mailOptions, (error, info) => {
transporter.sendMail(mailOptions, error => {
if (error) {
Logger.error(error);
Logger.error('✖ couldn\'t send sms', error);
} else {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Logger.info(`↗ sms sent: ${info.response}`);
Logger.info('✔ sms sent');
}
});
}
function generateAddress() {
const carrier = phone.carrier.toLowerCase();
if (carrier && phone.availableCarriers.has(carrier)) {
return [phone.number, phone.availableCarriers.get(carrier)].join('@');
}
Logger.error('✖ unknown carrier', carrier);
}
+10 -10
View File
@@ -5,29 +5,29 @@ import playerLib from 'play-sound';
const notificationSound = Config.notifications.playSound;
Logger.info('Searching for sound player...');
Logger.info(' searching for sound player...');
const player = playerLib();
if (player.player === null) {
Logger.warn('No sound player found.');
} else {
const playerName: string = player.player;
Logger.info(`Sound player found: ${playerName}`);
Logger.info(`✔ sound player found: ${playerName}`);
}
export function playSound() {
// Check if file exists
fs.access(notificationSound, fs.constants.F_OK, err => {
if (err) {
Logger.error(`error opening sound file: ${err.message}`);
fs.access(notificationSound, fs.constants.F_OK, error => {
if (error) {
Logger.error(`error opening sound file: ${error.message}`);
return;
}
player.play(notificationSound, (err: string) => {
Logger.info('↗ playing sound');
if (err) {
Logger.error(`error playing sound: ${err}`);
player.play(notificationSound, (error: Error) => {
if (error) {
Logger.error('✖ couldn\'t play sound', error);
}
Logger.info('✔ played sound');
});
});
}
+8 -5
View File
@@ -1,5 +1,6 @@
import {Link, Store} from '../store/model';
import {Logger, Print} from '../logger';
import {Config} from '../config';
import {Logger} from '../logger';
import {TelegramClient} from 'messaging-api-telegram';
const telegram = Config.notifications.telegram;
@@ -8,13 +9,15 @@ const client = new TelegramClient({
accessToken: telegram.accessToken
});
export function sendTelegramMessage(text: string) {
export function sendTelegramMessage(link: Link, store: Store) {
(async () => {
const givenUrl = link.cartUrl ? link.cartUrl : link.url;
try {
await client.sendMessage(telegram.chatId, text);
Logger.info(` telegram message sent to '${telegram.chatId}': ${text}`);
await client.sendMessage(telegram.chatId, `${Print.inStock(link, store)}\n${givenUrl}`);
Logger.info('✔ telegram message sent');
} catch (error) {
Logger.error(error);
Logger.error('✖ couldn\'t send telegram message', error);
}
})();
}
+8 -8
View File
@@ -1,6 +1,6 @@
import {Link, Store} from '../store/model';
import {Logger, Print} from '../logger';
import {Config} from '../config';
import {Link} from '../store/model';
import {Logger} from '../logger';
import Twitter from 'twitter';
const twitter = Config.notifications.twitter;
@@ -12,18 +12,18 @@ const client = new Twitter({
consumer_secret: twitter.consumerSecret
});
export function sendTweet(cartUrl: string, link: Link) {
let status = `🛎️ Stock Notification: ${link.brand} ${link.model}\n${cartUrl}`;
export function sendTweet(link: Link, store: Store) {
let status = `${Print.inStock(link, store)}\n${link.cartUrl ? link.cartUrl : link.url}`;
if (twitter.tweetTags) {
status += `\n\n${twitter.tweetTags}`;
}
client.post('statuses/update', {status}, err => {
if (err) {
Logger.error(err);
client.post('statuses/update', {status}, error => {
if (error) {
Logger.error('✖ couldn\'t send twitter notification', error);
} else {
Logger.info(` twitter notification sent: ${cartUrl}`);
Logger.info('✔ twitter notification sent');
}
});
}