fix: color logs and notification

Signed-off-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
Jef LeCompte
2020-09-23 18:43:25 -04:00
parent a154e95681
commit 76b28a6dbd
4 changed files with 53 additions and 28 deletions
+2 -2
View File
@@ -3,10 +3,10 @@ import {sendNotification} from '../notification';
const link: Link = {
brand: 'test:brand',
cartUrl: 'test:cartUrl',
cartUrl: 'https://www.example.com/cartUrl',
model: 'test:model',
series: 'test:series',
url: 'test:url'
url: 'https://www.example.com/url'
};
const store: Store = {
+33 -9
View File
@@ -10,7 +10,7 @@ const prettyJson = format.printf(info => {
info.message = JSON.stringify(info.message, null, 4);
}
return `[${timestamp}] ${info.level} :: ${info.message}`;
return colors.grey(`[${timestamp}]`) + ` ${info.level} ` + colors.grey('::') + ` ${info.message}`;
});
export const Logger = winston.createLogger({
@@ -26,16 +26,40 @@ export const Logger = winston.createLogger({
});
export const Print = {
captcha(link: Link, store: Store): string {
return colors.cyan(`✖ [${store.name}]`) + colors.grey(` [${link.brand} (${link.series})] ${link.model}`) + colors.yellow(' :: CAPTCHA');
captcha(link: Link, store: Store, color?: boolean): string {
if (color) {
return '✖ ' + buildProductString(link, store, true) + ' :: ' + colors.yellow('CAPTCHA');
}
return `${buildProductString(link, store)} :: CAPTCHA`;
},
inStock(link: Link, store: Store): string {
return colors.cyan(`✖ [${store.name}]`) + colors.grey(` [${link.brand} (${link.series})] ${link.model}`) + colors.green.bold(' :: IN STOCK 🚨🚀');
inStock(link: Link, store: Store, color?: boolean): string {
if (color) {
return colors.rainbow(`🚀🚨 ${buildProductString(link, store, true)} :: IN STOCK 🚨🚀`);
}
return `🚀🚨 ${buildProductString(link, store)} :: IN STOCK 🚨🚀`;
},
outOfStock(link: Link, store: Store): string {
return colors.cyan(`✖ [${store.name}]`) + colors.grey(` [${link.brand} (${link.series})] ${link.model}`) + colors.red(' :: OUT OF STOCK');
outOfStock(link: Link, store: Store, color?: boolean): string {
if (color) {
return '✖ ' + buildProductString(link, store, true) + ' :: ' + colors.red('OUT OF STOCK');
}
return `${buildProductString(link, store)} :: OUT OF STOCK`;
},
rateLimit(link: Link, store: Store): string {
return colors.cyan(`✖ [${store.name}]`) + colors.grey(` [${link.brand} (${link.series})] ${link.model}`) + colors.yellow(' :: RATE LIMIT EXCEEDED');
rateLimit(link: Link, store: Store, color?: boolean): string {
if (color) {
return '✖ ' + buildProductString(link, store, true) + ' :: ' + colors.yellow('RATE LIMIT EXCEEDED');
}
return `${buildProductString(link, store)} :: RATE LIMIT EXCEEDED`;
}
};
function buildProductString(link: Link, store: Store, color?: boolean): string {
if (color) {
return colors.cyan(`[${store.name}]`) + colors.grey(` [${link.brand} (${link.series})] ${link.model}`);
}
return `[${store.name}] [${link.brand} (${link.series})] ${link.model}`;
}
+14 -13
View File
@@ -5,29 +5,30 @@ import playerLib from 'play-sound';
const notificationSound = Config.notifications.playSound;
Logger.info(' searching for sound player...');
const player = playerLib();
if (player.player === null) {
Logger.warn('No sound player found.');
Logger.warn('✖ couldn\'t find sound player');
} else {
const playerName: string = player.player;
Logger.info(`✔ sound player found: ${playerName}`);
}
export function playSound() {
// Check if file exists
fs.access(notificationSound, fs.constants.F_OK, error => {
if (error) {
Logger.error(`✖ error opening sound file: ${error.message}`);
return;
}
player.play(notificationSound, (error: Error) => {
if (player.player !== null) {
fs.access(notificationSound, fs.constants.F_OK, error => {
if (error) {
Logger.error('✖ couldn\'t play sound', error);
Logger.error(`✖ error opening sound file: ${error.message}`);
return;
}
Logger.info('✔ played sound');
player.play(notificationSound, (error: Error) => {
if (error) {
Logger.error('✖ couldn\'t play sound', error);
}
Logger.info('✔ played sound');
});
});
});
}
}
+4 -4
View File
@@ -74,7 +74,7 @@ async function lookupCard(browser: Browser, store: Store, page: Page, link: Link
if (await lookupCardInStock(store, page)) {
const givenUrl = link.cartUrl ? link.cartUrl : link.url;
Logger.info(`${Print.inStock(link, store)}\n${givenUrl}`);
Logger.info(`${Print.inStock(link, store, true)}\n${givenUrl}`);
if (Config.browser.open) {
if (link.openCartAction === undefined) {
@@ -105,17 +105,17 @@ async function lookupCard(browser: Browser, store: Store, page: Page, link: Link
}
if (await lookupPageHasCaptcha(store, page)) {
Logger.warn(Print.captcha(link, store));
Logger.warn(Print.captcha(link, store, true));
await delay(getSleepTime());
return;
}
if (response && response.status() === 429) {
Logger.warn(Print.rateLimit(link, store));
Logger.warn(Print.rateLimit(link, store, true));
return;
}
Logger.info(Print.outOfStock(link, store));
Logger.info(Print.outOfStock(link, store, true));
}
async function lookupCardInStock(store: Store, page: Page) {