Files
streetmerchant/src/index.ts
T
2020-10-06 21:51:32 -04:00

76 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {Stores} from './store/model';
import {adBlocker} from './adblocker';
import {config} from './config';
import {getSleepTime} from './util';
import {logger} from './logger';
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());
if (config.browser.lowBandwidth) {
puppeteer.use(resourceBlock({
blockedTypes: new Set(['image', 'font'])
}));
} else {
puppeteer.use(adBlocker);
}
/**
* Starts the bot.
*/
async function main() {
if (Stores.length === 0) {
logger.error('✖ no stores selected', Stores);
return;
}
const args: string[] = [];
// Skip Chromium Linux Sandbox
// https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#setting-up-chrome-linux-sandbox
if (config.browser.isTrusted) {
args.push('--no-sandbox');
args.push('--disable-setuid-sandbox');
}
// Add the address of the proxy server if defined
if (config.proxy.address) {
args.push(`--proxy-server=http://${config.proxy.address}:${config.proxy.port}`);
}
// Check for deprecated configuration values
if (process.env.MAX_PRICE) {
logger.warn(' MAX_PRICE is deprecated, please use MAX_PRICE_$[series]');
}
const browser = await puppeteer.launch({
args,
defaultViewport: {
height: config.page.height,
width: config.page.width
},
headless: config.browser.isHeadless
});
for (const store of Stores) {
logger.debug('store links', {meta: {links: store.links}});
if (store.setupAction !== undefined) {
store.setupAction(browser);
}
setTimeout(tryLookupAndLoop, getSleepTime(), browser, store);
}
}
/**
* Will continually run until user interferes.
*/
try {
void main();
} catch (error) {
logger.error('✖ something bad happened, resetting nvidia-snatcher', error);
void main();
}