From a21740942bbbbe967948062fa06cfc82c31eb755 Mon Sep 17 00:00:00 2001 From: Evan Gentis Date: Sat, 19 Sep 2020 10:42:40 -0400 Subject: [PATCH] feat: webpage toggle, sound notification, fix evga links (#52) Co-authored-by: Evan Gentis Co-authored-by: Jef LeCompte --- .env.example | 2 ++ .gitignore | 4 ++++ README.md | 4 ++++ package-lock.json | 15 +++++++++++++++ package.json | 1 + src/config.ts | 8 ++++++-- src/index.ts | 6 +++++- src/notification/index.ts | 5 +++++ src/notification/sound.ts | 25 +++++++++++++++++++++++++ src/store/evga.ts | 23 +++++++++++++++++++++-- src/types/play-sound.d.ts | 1 + 11 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 src/notification/sound.ts create mode 100644 src/types/play-sound.d.ts diff --git a/.env.example b/.env.example index ae5852c..b74c718 100644 --- a/.env.example +++ b/.env.example @@ -8,4 +8,6 @@ SLACK_TOKEN="slack-token" STORES="bestbuy,bandh,nvidia" PHONE_NUMBER="1234567890" PHONE_CARRIER="tmobile" +OPEN_BROWSER="true" +PLAY_SOUND="false" SCREENSHOT="true" diff --git a/.gitignore b/.gitignore index 60de4b7..048d32c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,7 @@ node_modules/ .env success-*.png + +*.wav +*.mp3 +*.flac \ No newline at end of file diff --git a/README.md b/README.md index 6524e9e..28c6b6d 100644 --- a/README.md +++ b/README.md @@ -74,10 +74,14 @@ First, you're going to need to copy the `.env.example` to `.env`. The current op | `SLACK_CHANNEL` | Slack channel for posting (e.g., `update`); optional | | `SLACK_TOKEN` | Slack API token; optional | `STORES` | List of [stores](#Supported-stores) you want to be scraped; optional, default: `nvidia` | +| `OPEN_BROWSER` | Toggle for whether or not the browser should open when item is found, default: `true` | +| `PLAY_SOUND` | Play this sound notification if a card is found.; optional | | `SCREENSHOT` | Capture screenshot of page on successful hit; optional, default `true` | > :point_right: If you have multi-factor authentication (MFA), you will need to create an [app password](https://myaccount.google.com/apppasswords) and use this instead of your Gmail password. +> :point_right: Free sounds available [here](https://freesound.org/home/). Place sounds into `resources/sounds/` + #### Supported stores | **Store name** | **Store name environment variable** | diff --git a/package-lock.json b/package-lock.json index 9f22a2c..09c3621 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2721,6 +2721,12 @@ "pkg-dir": "^4.1.0" } }, + "find-exec": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/find-exec/-/find-exec-1.0.1.tgz", + "integrity": "sha512-4o6QkGkpg3xK5s/47rdK9LDZRsE4JR1mrXnaAOXBngG6UKeIDJXfwtNCAkljgyy6VRh75D3FFaB0tii9vDEtIA==", + "dev": true + }, "find-root": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/find-root/-/find-root-1.1.0.tgz", @@ -4643,6 +4649,15 @@ "find-up": "^4.0.0" } }, + "play-sound": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/play-sound/-/play-sound-1.1.3.tgz", + "integrity": "sha512-lqEzgtNAdfg2VUXItOtu5bTyWcqeFmnJmgvc8iHEeEOBEJdurqiGYfmCOzIpSBcwrs7XeSpvHv+Rw9dzRPc4aw==", + "dev": true, + "requires": { + "find-exec": "1.0.1" + } + }, "plur": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/plur/-/plur-4.0.0.tgz", diff --git a/package.json b/package.json index 116a9f3..f7f829a 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "@types/node": "^14.11.1", "@types/nodemailer": "^6.4.0", "@types/puppeteer": "^3.0.2", + "play-sound": "^1.1.3", "rimraf": "^3.0.2", "typescript": "^4.0.2", "xo": "^0.33.1" diff --git a/src/config.ts b/src/config.ts index 47d1a4a..4ad1b9a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -23,7 +23,8 @@ const notifications = { channel: process.env.SLACK_CHANNEL ?? '', token: process.env.SLACK_TOKEN ?? '' }, - test: process.env.NOTIFICATION_TEST ?? 'false' + test: process.env.NOTIFICATION_TEST ?? 'false', + playSound: process.env.PLAY_SOUND ?? 'false' }; const page = { @@ -38,9 +39,12 @@ const rateLimitTimeout = Number(process.env.RATE_LIMIT_TIMEOUT) ?? 5000; const stores = process.env.STORES ?? 'nvidia'; +const openBrowser = process.env.OPEN_BROWSER ?? 'true'; + export const Config = { notifications, rateLimitTimeout, page, - stores + stores, + openBrowser }; diff --git a/src/index.ts b/src/index.ts index 8cbbf34..5dbbf21 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,7 +67,11 @@ async function lookup(store: Store) { } const givenUrl = store.cartUrl ? store.cartUrl : link.url; - await open(givenUrl); + + if (Config.openBrowser === 'true') { + await open(givenUrl); + } + sendNotification(givenUrl); } diff --git a/src/notification/index.ts b/src/notification/index.ts index 2b61518..34a6f69 100644 --- a/src/notification/index.ts +++ b/src/notification/index.ts @@ -2,6 +2,7 @@ import {Config} from '../config'; import sendEmail from './email'; import sendSlaskMessage from './slack'; import sendSMS from './sms'; +import playSound from './sound'; export default function sendNotification(cartUrl: string) { if (Config.notifications.email.username && Config.notifications.email.password) { @@ -18,4 +19,8 @@ export default function sendNotification(cartUrl: string) { sendSMS(cartUrl); } } + + if (Config.notifications.playSound) { + playSound(); + } } diff --git a/src/notification/sound.ts b/src/notification/sound.ts new file mode 100644 index 0000000..ea2bfa3 --- /dev/null +++ b/src/notification/sound.ts @@ -0,0 +1,25 @@ +import playerLib = require('play-sound'); +import {Config} from '../config'; +import {Logger} from '../logger'; +import * as fs from 'fs'; + +const notificationSound = './resources/sounds/' + Config.notifications.playSound; +const player = playerLib(); + +export default function playSound() { + // Check if file exists + fs.access(notificationSound, fs.constants.F_OK, err => { + if (err) { + Logger.error(`error opening sound file: ${err.message}`); + return; + } + + player.play(notificationSound, (err: string) => { + Logger.info('✔ playing sound'); + + if (err) { + Logger.error(`error playing sound: ${err}`); + } + }); + }); +} diff --git a/src/store/evga.ts b/src/store/evga.ts index 1ee3e5d..229af67 100644 --- a/src/store/evga.ts +++ b/src/store/evga.ts @@ -5,10 +5,29 @@ export const Evga: Store = { links: [ { brand: 'evga', - model: 'ftw3, xc3 black, xc3 gaming, xc3 ultra gaming', - url: 'https://www.evga.com/products/productlist.aspx?type=0&family=GeForce+30+Series+Family&chipset=RTX+3080', + model: 'xc3 black', + url: 'https://www.evga.com/products/product.aspx?pn=10G-P5-3881-KR', + oosLabels: ['out of stock'] + }, + { + brand: 'evga', + model: 'ftw3', + url: 'https://www.evga.com/products/product.aspx?pn=10G-P5-3897-KR', + oosLabels: ['out of stock'] + }, + { + brand: 'evga', + model: 'xc3 gaming', + url: 'https://www.evga.com/products/product.aspx?pn=10G-P5-3883-KR', + oosLabels: ['out of stock'] + }, + { + brand: 'evga', + model: 'xc3 ultra gaming', + url: 'https://www.evga.com/products/product.aspx?pn=10G-P5-3885-KR', oosLabels: ['out of stock'] } ], name: 'evga' }; + diff --git a/src/types/play-sound.d.ts b/src/types/play-sound.d.ts new file mode 100644 index 0000000..c366eeb --- /dev/null +++ b/src/types/play-sound.d.ts @@ -0,0 +1 @@ +declare module 'play-sound';