mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 02:57:34 +00:00
feat: optional per store min and max page sleep time (#576)
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
@@ -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` |
|
||||
|
||||
+8
-1
@@ -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 = {
|
||||
|
||||
+1
-1
@@ -60,7 +60,7 @@ async function main() {
|
||||
store.setupAction(browser);
|
||||
}
|
||||
|
||||
setTimeout(tryLookupAndLoop, getSleepTime(), browser, store);
|
||||
setTimeout(tryLookupAndLoop, getSleepTime(store), browser, store);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,4 +60,6 @@ export type Store = {
|
||||
*/
|
||||
successStatusCodes?: StatusCodeRangeArray;
|
||||
waitUntil?: LoadEvent;
|
||||
minPageSleep?: number;
|
||||
maxPageSleep?: number;
|
||||
};
|
||||
|
||||
+4
-3
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user