diff --git a/src/__test__/notification-test.ts b/src/__test__/notification-test.ts index c1a2e06..57f4f40 100644 --- a/src/__test__/notification-test.ts +++ b/src/__test__/notification-test.ts @@ -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 = { diff --git a/src/logger.ts b/src/logger.ts index b2ac9a4..f5d28f6 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -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}`; +} diff --git a/src/notification/sound.ts b/src/notification/sound.ts index 5227698..0867ae5 100644 --- a/src/notification/sound.ts +++ b/src/notification/sound.ts @@ -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'); + }); }); - }); + } } diff --git a/src/store/lookup.ts b/src/store/lookup.ts index 9704e27..1bb49f2 100644 --- a/src/store/lookup.ts +++ b/src/store/lookup.ts @@ -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) {