Skip to content

Commit

Permalink
feat: optional per store min and max page sleep time (#576)
Browse files Browse the repository at this point in the history
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
  • Loading branch information
SesioN and jef committed Oct 23, 2020
1 parent 11ee0bf commit 503d76f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -130,7 +130,7 @@ Here is a list of variables that you can use to customize your newly copied `.en
| `SLACK_TOKEN` | Slack API token | |
| `SMTP_ADDRESS` | IP Address or fqdn of smtp server |
| `SMTP_PORT` | TCP Port number on which the smtp server is listening for connections | Default: `25` |
| `STORES` | [Supported stores](#supported-stores) you want to be scraped | Comma separated, default: `nvidia` |
| `STORES` | [Supported stores](#supported-stores) you want to be scraped | Both supported formats are comma separated <br/><br/>1. Standard E.g.: `"nvidia"` <br/><br/> 2. Advanced E.g: `STORE:PAGE_SLEEP_MIN:PAGE_SLEEP_MAX`, E.g: `nvidia:10000:30000` <br/><br/>Default: `nvidia` |
| `SCREENSHOT` | Capture screenshot of page if a card is found | Default: `true` |
| `TELEGRAM_ACCESS_TOKEN` | Telegram access token | |
| `TELEGRAM_CHAT_ID` | Telegram chat ID | Comma seperated, e.g.: `123456789`, `123456789,987654321` |
Expand Down
9 changes: 8 additions & 1 deletion src/config.ts
Expand Up @@ -245,7 +245,14 @@ const store = {
showOnlyBrands: envOrArray(process.env.SHOW_ONLY_BRANDS),
showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS),
showOnlySeries: envOrArray(process.env.SHOW_ONLY_SERIES, ['3070', '3080', '3090']),
stores: envOrArray(process.env.STORES, ['nvidia'])
stores: envOrArray(process.env.STORES, ['nvidia']).map(entry => {
const [name, minPageSleep, maxPageSleep] = entry.match(/[^:]+/g) ?? [];
return {
maxPageSleep: envOrNumberMax(minPageSleep, maxPageSleep, browser.maxSleep),
minPageSleep: envOrNumberMin(minPageSleep, maxPageSleep, browser.minSleep),
name: envOrString(name)
};
})
};

export const config = {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -60,7 +60,7 @@ async function main() {
store.setupAction(browser);
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/store/lookup.ts
Expand Up @@ -183,7 +183,7 @@ async function lookupCardInStock(store: Store, page: Page, link: Link) {
if (store.labels.captcha) {
if (await pageIncludesLabels(page, store.labels.captcha, baseOptions)) {
logger.warn(Print.captcha(link, store, true));
await delay(getSleepTime());
await delay(getSleepTime(store));
return false;
}
}
Expand Down Expand Up @@ -212,7 +212,7 @@ export async function tryLookupAndLoop(browser: Browser, store: Store) {
logger.error(error);
}

const sleepTime = getSleepTime();
const sleepTime = getSleepTime(store);
logger.debug(`[${store.name}] Lookup done, next one in ${sleepTime} ms`);
setTimeout(tryLookupAndLoop, sleepTime, browser, store);
}
8 changes: 4 additions & 4 deletions src/store/model/index.ts
Expand Up @@ -99,11 +99,11 @@ const masterList = new Map([

const list = new Map();

for (const name of config.store.stores) {
if (masterList.has(name)) {
list.set(name, masterList.get(name));
for (const storeData of config.store.stores) {
if (masterList.has(storeData.name)) {
list.set(storeData.name, {...masterList.get(storeData.name), storeData});
} else {
const logString = `No store named ${name}, skipping.`;
const logString = `No store named ${storeData.name}, skipping.`;
logger.warn(logString);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/store/model/store.ts
Expand Up @@ -60,4 +60,6 @@ export type Store = {
*/
successStatusCodes?: StatusCodeRangeArray;
waitUntil?: LoadEvent;
minPageSleep?: number;
maxPageSleep?: number;
};
7 changes: 4 additions & 3 deletions src/util.ts
@@ -1,11 +1,12 @@
import {Browser, Page, Response} from 'puppeteer';
import {StatusCodeRangeArray} from './store/model';
import {StatusCodeRangeArray, Store} from './store/model';
import {config} from './config';
import {disableBlockerInPage} from './adblocker';
import {logger} from './logger';

export function getSleepTime() {
return config.browser.minSleep + (Math.random() * (config.browser.maxSleep - config.browser.minSleep));
export function getSleepTime(store: Store) {
const minSleep = store.minPageSleep as number;
return minSleep + (Math.random() * ((store.maxPageSleep as number) - minSleep));
}

export async function delay(ms: number) {
Expand Down

0 comments on commit 503d76f

Please sign in to comment.