Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notification): add pushbullet, add url with notifications #226

Merged
merged 7 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PAGE_TIMEOUT="30000"
PHONE_NUMBER="1234567890"
PHONE_CARRIER="tmobile"
PLAY_SOUND="notification.mp3"
PUSHBULLET="o.pushbulletTokendbjVjvJHVhcg"
PUSHOVER_TOKEN="123pushover-token456"
PUSHOVER_USER="123pushover-user-key"
PAGE_SLEEP_MIN="5000"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Here is a list of variables that you can use to customize your newly copied `.en
| `PHONE_NUMBER` | 10 digit phone number | E.g.: `1234567890`, email configuration required |
| `PHONE_CARRIER` | [Supported carriers](#supported-carriers) for SMS | Email configuration required |
| `PLAY_SOUND` | Play this sound notification if a card is found | E.g.: `path/to/notification.wav`, relative path accepted, valid formats: wav, mp3, flac, [free sounds available](https://notificationsounds.com/) |
| `PUSHBULLET` | PushBullet API key | Get at https://www.pushbullet.com/#settings/account |
| `PUSHOVER_TOKEN` | Pushover access token | Generate at https://pushover.net/apps/build |
| `PUSHOVER_USER` | Pushover username |
| `PAGE_SLEEP_MIN` | Minimum sleep time between queries of the same store | Default: `5000` |
Expand Down
61 changes: 61 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"puppeteer-extra": "^3.1.15",
"puppeteer-extra-plugin-adblocker": "^2.11.6",
"puppeteer-extra-plugin-stealth": "^2.6.1",
"pushbullet": "^2.4.0",
"pushover-notifications": "^1.2.2",
"twitter": "^1.7.1",
"winston": "^3.3.3"
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const notifications = {
number: process.env.PHONE_NUMBER ?? ''
},
playSound: process.env.PLAY_SOUND ?? '',
pushBulletApiKey: process.env.PUSHBULLET ?? '',
pushover: {
token: process.env.PUSHOVER_TOKEN ?? '',
username: process.env.PUSHOVER_USER ?? ''
Expand Down
5 changes: 5 additions & 0 deletions src/notification/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {playSound} from './sound';
import {sendDesktopNotification} from './desktop';
import {sendDiscordMessage} from './discord';
import {sendEmail} from './email';
import {sendPushBulletNotification} from './pushbullet';
import {sendPushoverNotification} from './pushover';
import {sendSMS} from './sms';
import {sendSlackMessage} from './slack';
Expand Down Expand Up @@ -36,6 +37,10 @@ export function sendNotification(cartUrl: string, link: Link) {
}
}

if (notifications.pushBulletApiKey) {
sendPushBulletNotification(cartUrl, link);
}

if (notifications.pushover.token && notifications.pushover.username) {
sendPushoverNotification(cartUrl);
}
Expand Down
19 changes: 19 additions & 0 deletions src/notification/pushbullet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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) {
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}`);
}
});
}
4 changes: 2 additions & 2 deletions src/store/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ async function lookupCard(browser: Browser, store: Store, page: Page, link: Link
await page.screenshot({path: link.screenshot});
}

const givenUrl = link.cartUrl ? link.cartUrl : link.url;
let givenUrl = link.cartUrl ? link.cartUrl : link.url;

if (Config.browser.open) {
if (link.openCartAction === undefined) {
await open(givenUrl);
} else {
link.openCartAction(browser);
givenUrl = await link.openCartAction(browser);
}
}

Expand Down
18 changes: 13 additions & 5 deletions src/store/model/helpers/nvidia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ export function generateSetupAction() {
const accessToken = data.access_token;

Logger.info('[nvidia] you can log into your cart now...');
Logger.info(checkoutUrl(drLocale, accessToken));
await open(checkoutUrl(drLocale, accessToken));
const cartUrl = checkoutUrl(drLocale, accessToken);
Logger.info(cartUrl);
if (Config.browser.open) {
await open(cartUrl);
}
} catch (error) {
Logger.debug(error);
Logger.error('✖ [nvidia] cannot generate cart/session token, continuing without, auto-"add to cart" may not work...');
Expand All @@ -83,6 +86,7 @@ export function generateOpenCartAction(id: number, nvidiaLocale: string, drLocal
const page = await browser.newPage();
Logger.info(`🚀🚀🚀 [nvidia] ${cardName}, starting auto add to cart... 🚀🚀🚀`);
let response: Response | null;
let cartUrl: string;
try {
Logger.info(`🚀🚀🚀 [nvidia] ${cardName}, getting access token... 🚀🚀🚀`);
response = await page.goto(nvidiaSessionUrl(nvidiaLocale), {waitUntil: 'networkidle0'});
Expand All @@ -97,15 +101,19 @@ export function generateOpenCartAction(id: number, nvidiaLocale: string, drLocal
response = await page.goto(addToCartUrl(id, drLocale, accessToken), {waitUntil: 'networkidle0'});

Logger.info(`🚀🚀🚀 [nvidia] ${cardName}, opening checkout page... 🚀🚀🚀`);
Logger.info(checkoutUrl(drLocale, accessToken));
await open(checkoutUrl(drLocale, accessToken));
cartUrl = checkoutUrl(drLocale, accessToken);
Logger.info(cartUrl);
await open(cartUrl);
} catch (error) {
Logger.debug(error);
Logger.error(`✖ [nvidia] ${cardName} could not automatically add to cart, opening page`);
await open(fallbackCartUrl(nvidiaLocale));
cartUrl = fallbackCartUrl(nvidiaLocale);
await open(cartUrl);
}

await page.close();

return cartUrl;
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/store/model/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Link {
model: string;
url: string;
cartUrl?: string;
openCartAction?: (browser: Browser) => void;
openCartAction?: (browser: Browser) => Promise<string>;
screenshot?: string;
}

Expand Down
1 change: 1 addition & 0 deletions src/types/pushbullet.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'pushbullet';