Skip to content

Commit

Permalink
feat(api): Add rudimentary web control panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Doridian committed Sep 23, 2020
1 parent 8466f9f commit c61ca61
Show file tree
Hide file tree
Showing 32 changed files with 335 additions and 44 deletions.
1 change: 1 addition & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ TWITTER_ACCESS_TOKEN_KEY=
TWITTER_ACCESS_TOKEN_SECRET=
TWITTER_TWEET_TAGS=
USER_AGENT="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
WEB_PORT="1337"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Here is a list of variables that you can use to customize your newly copied `.en
| `TWITTER_ACCESS_TOKEN_KEY` | Twitter Token Key |
| `TWITTER_ACCESS_TOKEN_SECRET` | Twitter Token Secret |
| `TWITTER_TWEET_TAGS` | Optional list of hashtags to append to the tweet message | Eg: "`#NVIDIA` `#NVIDIAINSTOCK`" |
| `WEB_PORT` | Starts a webserver to be able to control the bot while it is running; optional | Default: disabled |

> :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.
Expand Down
2 changes: 1 addition & 1 deletion src/__test__/notification-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Link} from '../store/model';
import {Link} from '../store/model/types';
import {sendNotification} from '../notification';

const link: Link = {
Expand Down
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,10 @@ export const Config = {
page,
store
};

export function setConfig(newConfig: any) {
const writeConfig = Config as any;
for (const key of Object.keys(newConfig)) {
writeConfig[key] = newConfig[key];
}
}
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Config} from './config';
import {Logger} from './logger';
import {Stores} from './store/model';
import {adBlocker} from './adblocker';
import {getSleepTime} from './util';
import puppeteer from 'puppeteer-extra';
import {startAPIServer} from './web';
import stealthPlugin from 'puppeteer-extra-plugin-stealth';
import {storeList} from './store/model';
import {tryLookupAndLoop} from './store';

puppeteer.use(stealthPlugin());
Expand Down Expand Up @@ -37,14 +38,16 @@ async function main() {
headless: Config.browser.isHeadless
});

for (const store of Stores) {
for (const store of storeList.values()) {
Logger.debug(store.links);
if (store.setupAction !== undefined) {
store.setupAction(browser);
}

setTimeout(tryLookupAndLoop, getSleepTime(), browser, store);
}

startAPIServer();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/notification/desktop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Link} from '../store/model';
import {Link} from '../store/model/types';
import notifier from 'node-notifier';

export function sendDesktopNotification(cartUrl: string, link: Link) {
Expand Down
2 changes: 1 addition & 1 deletion src/notification/discord.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {MessageBuilder, Webhook} from 'discord-webhook-node';
import {Config} from '../config';
import {Link} from '../store/model';
import {Link} from '../store/model/types';
import {Logger} from '../logger';

const hook = new Webhook(Config.notifications.discord.webHookUrl);
Expand Down
2 changes: 1 addition & 1 deletion src/notification/email.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Config} from '../config';
import {Link} from '../store/model';
import {Link} from '../store/model/types';
import {Logger} from '../logger';
import Mail from 'nodemailer/lib/mailer';
import nodemailer from 'nodemailer';
Expand Down
2 changes: 1 addition & 1 deletion src/notification/notification.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Config} from '../config';
import {Link} from '../store/model';
import {Link} from '../store/model/types';
import {playSound} from './sound';
import {sendDesktopNotification} from './desktop';
import {sendDiscordMessage} from './discord';
Expand Down
2 changes: 1 addition & 1 deletion src/notification/sms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Config} from '../config';
import {Link} from '../store/model';
import {Link} from '../store/model/types';
import {Logger} from '../logger';
import Mail from 'nodemailer/lib/mailer';
import nodemailer from 'nodemailer';
Expand Down
2 changes: 1 addition & 1 deletion src/notification/twitter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Config} from '../config';
import {Link} from '../store/model';
import {Link} from '../store/model/types';
import {Logger} from '../logger';
import Twitter from 'twitter';

Expand Down
6 changes: 5 additions & 1 deletion src/store/lookup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Browser, Page, Response} from 'puppeteer';
import {Link, Store} from './model';
import {Link, Store} from './model/types';
import {closePage, delay, getSleepTime} from '../util';
import {Config} from '../config';
import {Logger} from '../logger';
Expand Down Expand Up @@ -43,6 +43,10 @@ function filterSeries(series: string) {
* @param store Vendor of graphics cards.
*/
async function lookup(browser: Browser, store: Store) {
if (Config.store.stores.length > 0 && !Config.store.stores.includes(store.name)) {
return;
}

/* eslint-disable no-await-in-loop */
for (const link of store.links) {
if (!filterSeries(link.series)) {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/adorama.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const Adorama: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/amazon-ca.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const AmazonCa: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/amazon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const Amazon: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/asus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const Asus: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/bandh.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const BAndH: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/bestbuy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const BestBuy: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/evga-eu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const EvgaEu: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/evga.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const Evga: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/helpers/nvidia.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Browser, Response} from 'puppeteer';
import {NvidiaRegionInfo, regionInfos} from '../nvidia';
import {Config} from '../../../config';
import {Link} from '../store';
import {Link} from '../types';
import {Logger} from '../../../logger';
import open from 'open';
import {timestampUrlParameter} from '../../timestamp-url-parameter';
Expand Down
30 changes: 13 additions & 17 deletions src/store/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ import {Asus} from './asus';
import {BAndH} from './bandh';
import {BestBuy} from './bestbuy';
import {BestBuyCa} from './bestbuy-ca';
import {Config} from '../../config';
import {Evga} from './evga';
import {EvgaEu} from './evga-eu';
import {Logger} from '../../logger';
import {MicroCenter} from './microcenter';
import {NewEgg} from './newegg';
import {NewEggCa} from './newegg-ca';
import {Nvidia} from './nvidia';
import {OfficeDepot} from './officedepot';
import {Store} from './store';
import {Zotac} from './zotac';

const masterList = new Map([
export const storeList = new Map([
[Adorama.name, Adorama],
[Amazon.name, Amazon],
[AmazonCa.name, AmazonCa],
Expand All @@ -36,20 +33,19 @@ const masterList = new Map([
[Zotac.name, Zotac]
]);

const list = new Map();

for (const name of Config.store.stores) {
if (masterList.has(name)) {
list.set(name, masterList.get(name));
} else {
const logString = `No store named ${name}, skipping.`;
Logger.warn(logString);
const brands = new Set();
const series = new Set();
for (const store of storeList.values()) {
for (const link of store.links) {
brands.add(link.brand);
series.add(link.series);
}
}

const logString = `Selected stores: ${Array.from(list.keys()).join(', ')}`;
Logger.info(logString);

export const Stores = Array.from(list.values()) as Store[];
export function getAllBrands() {
return Array.from(brands);
}

export * from './store';
export function getAllSeries() {
return Array.from(series);
}
2 changes: 1 addition & 1 deletion src/store/model/microcenter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const MicroCenter: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/newegg-ca.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const NewEggCa: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/newegg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const NewEgg: Store = {
labels: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/nvidia.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {generateLinks, generateSetupAction} from './helpers/nvidia';
import {Store} from './store';
import {Store} from './types';

// Region/country set by config file, silently ignores null / missing values and defaults to usa

Expand Down
2 changes: 1 addition & 1 deletion src/store/model/officedepot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const OfficeDepot: Store = {
labels: {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/store/model/zotac.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Store} from './store';
import {Store} from './types';

export const Zotac: Store = {
labels: {
Expand Down

0 comments on commit c61ca61

Please sign in to comment.