refactor: doc and notification (#42)

Signed-off-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
Jef LeCompte
2020-09-18 19:34:09 -04:00
committed by GitHub
parent 643045c7e0
commit fad9ea04c7
6 changed files with 32 additions and 33 deletions
+3 -2
View File
@@ -1,7 +1,8 @@
EMAIL_USERNAME="youremail@gmail.com" EMAIL_USERNAME="youremail@gmail.com"
EMAIL_PASSWORD="secretpassword" EMAIL_PASSWORD="secretpassword"
EMAIL_TEST="true" NOTIFICATION_TEST="false"
NOTIFICATION_METHODS="email" PAGE_TIMEOUT=30000
RATE_LIMIT_TIMEOUT=5000
SLACK_CHANNEL="SlackChannelName" SLACK_CHANNEL="SlackChannelName"
SLACK_TOKEN="slack-token" SLACK_TOKEN="slack-token"
STORES="bestbuy,bandh,nvidia" STORES="bestbuy,bandh,nvidia"
+8 -4
View File
@@ -64,10 +64,14 @@ First, you're going to need to copy the `.env.example` to `.env`. The current op
| **Environment variable** | **Description** | | **Environment variable** | **Description** |
|:---:|:---:| |:---:|:---:|
| `EMAIL_USERNAME` | Gmail address; e.g. `jensen.robbed.us@gmail.com` | | `EMAIL_USERNAME` | Gmail address (e.g., `jensen.robbed.us@gmail.com`); optional |
| `EMAIL_PASSWORD` | Gmail password; see below if you have MFA | | `EMAIL_PASSWORD` | Gmail password; see below if you have MFA; optional |
| `NOTIFICATION_TEST` | Test all the notifications configured; optional, default: `false` |
| `PAGE_TIMEOUT` | Navigation Timeout in milliseconds (`0` for infinite); optional, default: `30000` |
| `RATE_LIMIT_TIMEOUT` | Rate limit timeout for each full store cycle; optional, default: `5000` |
| `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` | | `STORES` | List of [stores](#Supported-stores) you want to be scraped; optional, default: `nvidia` |
| `PAGE_TIMEOUT` | Navigation Timeout in milliseconds (0 for infinite, 60000 for 1 minute); optional, default: `0` |
> :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: 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.
@@ -76,11 +80,11 @@ First, you're going to need to copy the `.env.example` to `.env`. The current op
| **Store name** | **Store name environment variable** | | **Store name** | **Store name environment variable** |
|:---:|:---:| |:---:|:---:|
| Best Buy | `bestbuy`| | Best Buy | `bestbuy`|
| Amazon | `amazon`|
| B&H | `bandh`| | B&H | `bandh`|
| EVGA | `evga`| | EVGA | `evga`|
| Newegg | `newegg`| | Newegg | `newegg`|
| Nvidia | `nvidia`| | Nvidia | `nvidia`|
| Amazon | `amazon`|
> :point_right: Look at [`.env.example`](.env.example) for an example for `.env`. > :point_right: Look at [`.env.example`](.env.example) for an example for `.env`.
+15 -21
View File
@@ -3,38 +3,32 @@ import {config} from 'dotenv';
config({path: resolve(__dirname, '../.env')}); config({path: resolve(__dirname, '../.env')});
const email = {
username: process.env.EMAIL_USERNAME,
password: process.env.EMAIL_PASSWORD,
test: process.env.EMAIL_TEST ?? 'false'
};
const notifications = { const notifications = {
email: email.username && email.password email: {
username: process.env.EMAIL_USERNAME ?? '',
password: process.env.EMAIL_PASSWORD ?? ''
},
slack: {
channel: process.env.SLACK_CHANNEL ?? '',
token: process.env.SLACK_TOKEN ?? ''
},
test: process.env.NOTIFICATION_TEST ?? 'false'
}; };
const page = { const page = {
height: 1920, height: 1920,
navigationTimeout: Number(process.env.PAGE_TIMEOUT) ?? 30000,
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36', userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
width: 1080, width: 1080
navigationTimeout: Number(process.env.PAGE_TIMEOUT) ?? 30000
}; };
const rateLimitTimeout = Number(process.env.RATE_LIMIT_TIMEOUT) ?? 5000;
const stores = process.env.STORES ?? 'nvidia'; const stores = process.env.STORES ?? 'nvidia';
const notificationMethods = process.env.NOTIFICATION_METHODS ?? 'email';
const slack = {
channel: process.env.SLACK_CHANNEL,
token: process.env.SLACK_TOKEN
};
export const Config = { export const Config = {
email,
notifications, notifications,
rateLimitTimeout,
page, page,
rateLimitTimeout: 5000, stores
stores,
slack,
notificationMethods
}; };
+2 -2
View File
@@ -8,7 +8,7 @@ import {Logger} from './logger';
/** /**
* Send test email. * Send test email.
*/ */
if (Config.email.test === 'true') { if (Config.notifications.test === 'true') {
sendNotification('test'); sendNotification('test');
} }
@@ -25,7 +25,7 @@ async function main() {
await Promise.all(results); await Promise.all(results);
Logger.info('↗ trying stores again'); Logger.info('↗ trying stores again');
setTimeout(main, Config.rateLimitTimeout); setTimeout(main, Config.page.rateLimitTimeout);
} }
/** /**
+2 -2
View File
@@ -3,11 +3,11 @@ import sendEmail from './email';
import sendSlaskMessage from './slack'; import sendSlaskMessage from './slack';
export default function sendNotification(cartUrl: string) { export default function sendNotification(cartUrl: string) {
if (Config.notificationMethods.toLocaleLowerCase().includes('email')) { if (Config.notifications.email.username && Config.notifications.email.password) {
sendEmail(cartUrl); sendEmail(cartUrl);
} }
if (Config.notificationMethods.toLocaleLowerCase().includes('slack')) { if (Config.notifications.slack.channel && Config.notifications.slack.token) {
sendSlaskMessage(cartUrl); sendSlaskMessage(cartUrl);
} }
} }
+2 -2
View File
@@ -2,8 +2,8 @@ import {WebClient} from '@slack/web-api';
import {Config} from '../config'; import {Config} from '../config';
import {Logger} from '../logger'; import {Logger} from '../logger';
const channel = Config.slack.channel ?? ''; const channel = Config.notifications.slack.channel;
const token = Config.slack.token ?? ''; const token = Config.notifications.slack.token;
const web = new WebClient(token); const web = new WebClient(token);
export default function sendSlackMessage(text: string) { export default function sendSlackMessage(text: string) {