Skip to content

Commit

Permalink
feat: low bandwidth mode (#294)
Browse files Browse the repository at this point in the history
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
  • Loading branch information
joshuahiggins and jef committed Sep 26, 2020
1 parent eda6c85 commit 0aa7ab5
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env-example
Expand Up @@ -10,6 +10,7 @@ EMAIL_PASSWORD=""
HEADLESS=""
IN_STOCK_WAIT_TIME=""
LOG_LEVEL=""
LOW_BANDWIDTH=""
MICROCENTER_LOCATION=""
OPEN_BROWSER=""
PAGE_TIMEOUT=""
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -77,6 +77,7 @@ Here is a list of variables that you can use to customize your newly copied `.en
| `HEADLESS` | Puppeteer to run headless or not | Debugging related, default: `true` |
| `IN_STOCK_WAIT_TIME` | Time to wait between requests to the same link if it has that card in stock | In seconds, default: `0` |
| `LOG_LEVEL` | [Logging levels](https://github.com/winstonjs/winston#logging-levels) | Debugging related, default: `info` |
| `LOW_BANDWIDTH` | Blocks images/fonts to reduce traffic (Note: disabled adblocker) | Default: `false` |
| `MICROCENTER_LOCATION` | Specific MicroCenter location to search | Default : `web` |
| `OPEN_BROWSER` | Toggle for whether or not the browser should open when item is found | Default: `true` |
| `PAGE_TIMEOUT` | Navigation Timeout in milliseconds | `0` for infinite, default: `30000` |
Expand Down
24 changes: 24 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
Expand Up @@ -36,6 +36,7 @@
"puppeteer": "^5.3.1",
"puppeteer-extra": "^3.1.15",
"puppeteer-extra-plugin-adblocker": "^2.11.6",
"puppeteer-extra-plugin-block-resources": "^2.2.7",
"puppeteer-extra-plugin-stealth": "^2.6.1",
"pushbullet": "^2.4.0",
"pushover-notifications": "^1.2.2",
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Expand Up @@ -49,6 +49,7 @@ function envOrNumber(environment: string | undefined, number?: number): number {
const browser = {
isHeadless: envOrBoolean(process.env.HEADLESS),
isTrusted: envOrBoolean(process.env.BROWSER_TRUSTED, false),
lowBandwidth: envOrBoolean(process.env.LOW_BANDWIDTH, false),
maxBackoff: envOrNumber(process.env.PAGE_BACKOFF_MAX, 3600000),
maxSleep: envOrNumber(process.env.PAGE_SLEEP_MAX, 10000),
minBackoff: envOrNumber(process.env.PAGE_BACKOFF_MIN, 10000),
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Expand Up @@ -5,11 +5,18 @@ import {adBlocker} from './adblocker';
import {fetchLinks} from './store/fetch-links';
import {getSleepTime} from './util';
import puppeteer from 'puppeteer-extra';
import resourceBlock from 'puppeteer-extra-plugin-block-resources';
import stealthPlugin from 'puppeteer-extra-plugin-stealth';
import {tryLookupAndLoop} from './store';

puppeteer.use(stealthPlugin());
puppeteer.use(adBlocker);
if (Config.browser.lowBandwidth) {
puppeteer.use(resourceBlock({
blockedTypes: new Set(['image', 'font'])
}));
} else {
puppeteer.use(adBlocker);
}

/**
* Starts the bot.
Expand Down
1 change: 1 addition & 0 deletions src/types/puppeteer-extra-plugin-block-resources.d.ts
@@ -0,0 +1 @@
declare module 'puppeteer-extra-plugin-block-resources';
5 changes: 4 additions & 1 deletion src/util.ts
Expand Up @@ -42,6 +42,9 @@ export async function usingPage<T>(browser: Browser, cb: (page: Page, browser: B
}

export async function closePage(page: Page) {
await disableBlockerInPage(page);
if (!Config.browser.lowBandwidth) {
await disableBlockerInPage(page);
}

await page.close();
}

0 comments on commit 0aa7ab5

Please sign in to comment.