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: optional per store min and max page sleep time #576

Merged
merged 2 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,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` |
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| `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` |
| `STORES` | [Supported stores](#supported-stores) you want to be scraped | Both supported formats are comma separated<br/>1. Standard E.g.: `"nvidia"`<br/>2. Advanced E.g: `STORE:PAGE_SLEEP_MIN:PAGE_SLEEP_MAX`, E.g: `nvidia:10000:30000`<br/>Default: `nvidia` |

I like the <br/>s! Maybe just one though per?

| `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
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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