mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 05:17:35 +00:00
feat(api): add rudimentary web control panel (#183)
This commit is contained in:
+41
-13
@@ -1,4 +1,5 @@
|
||||
import {Stores} from './store/model';
|
||||
import {startAPIServer, stopAPIServer} from './web';
|
||||
import {Browser} from 'puppeteer';
|
||||
import {adBlocker} from './adblocker';
|
||||
import {config} from './config';
|
||||
import {getSleepTime} from './util';
|
||||
@@ -6,6 +7,7 @@ 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 {storeList} from './store/model';
|
||||
import {tryLookupAndLoop} from './store';
|
||||
|
||||
puppeteer.use(stealthPlugin());
|
||||
@@ -17,15 +19,12 @@ if (config.browser.lowBandwidth) {
|
||||
puppeteer.use(adBlocker);
|
||||
}
|
||||
|
||||
let browser: Browser | undefined;
|
||||
|
||||
/**
|
||||
* Starts the bot.
|
||||
*/
|
||||
async function main() {
|
||||
if (Stores.length === 0) {
|
||||
logger.error('✖ no stores selected', Stores);
|
||||
return;
|
||||
}
|
||||
|
||||
const args: string[] = [];
|
||||
|
||||
// Skip Chromium Linux Sandbox
|
||||
@@ -45,7 +44,9 @@ async function main() {
|
||||
args.push(`--proxy-server=http://${config.proxy.address}:${config.proxy.port}`);
|
||||
}
|
||||
|
||||
const browser = await puppeteer.launch({
|
||||
await stop();
|
||||
|
||||
browser = await puppeteer.launch({
|
||||
args,
|
||||
defaultViewport: {
|
||||
height: config.page.height,
|
||||
@@ -54,7 +55,7 @@ async function main() {
|
||||
headless: config.browser.isHeadless
|
||||
});
|
||||
|
||||
for (const store of Stores) {
|
||||
for (const store of storeList.values()) {
|
||||
logger.debug('store links', {meta: {links: store.links}});
|
||||
if (store.setupAction !== undefined) {
|
||||
store.setupAction(browser);
|
||||
@@ -62,14 +63,41 @@ async function main() {
|
||||
|
||||
setTimeout(tryLookupAndLoop, getSleepTime(store), browser, store);
|
||||
}
|
||||
|
||||
await startAPIServer();
|
||||
}
|
||||
|
||||
async function stop() {
|
||||
await stopAPIServer();
|
||||
|
||||
if (browser) {
|
||||
// Use temporary swap variable to avoid any race condition
|
||||
const browserTemporary = browser;
|
||||
browser = undefined;
|
||||
await browserTemporary.close();
|
||||
}
|
||||
}
|
||||
|
||||
async function stopAndExit() {
|
||||
await stop();
|
||||
// eslint-disable-next-line unicorn/no-process-exit
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will continually run until user interferes.
|
||||
*/
|
||||
try {
|
||||
void main();
|
||||
} catch (error) {
|
||||
logger.error('✖ something bad happened, resetting nvidia-snatcher', error);
|
||||
void main();
|
||||
async function loopMain() {
|
||||
try {
|
||||
await main();
|
||||
} catch (error) {
|
||||
logger.error('✖ something bad happened, resetting nvidia-snatcher in 5 seconds', error);
|
||||
setTimeout(loopMain, 5000);
|
||||
}
|
||||
}
|
||||
|
||||
void loopMain();
|
||||
|
||||
process.on('SIGINT', stopAndExit);
|
||||
process.on('SIGQUIT', stopAndExit);
|
||||
process.on('SIGTERM', stopAndExit);
|
||||
|
||||
Reference in New Issue
Block a user