repo: init
This commit is contained in:
28
src/website/twitch.ts
Normal file
28
src/website/twitch.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import type { Page } from "patchright";
|
||||
import logger from "../logger";
|
||||
|
||||
async function keepAlive(page: Page, spawnId: string = "unknown") {
|
||||
try {
|
||||
let waitTime = 0;
|
||||
while (true) {
|
||||
// Wait for a random time between 1 and 11 seconds
|
||||
const timeout = 1000 + Math.floor(Math.random() * 60 * 1000) % 10000;
|
||||
logger.debug(`[${spawnId}] Waiting for ${timeout / 1000} seconds...`);
|
||||
await page.waitForTimeout(timeout);
|
||||
waitTime += timeout;
|
||||
if ((await page.locator(".ScCoreButton-sc-ocjdkq-0.ggPgVz").all()).length > 0) {
|
||||
logger.debug(`[${spawnId}] Player encountered an error, refreshing the page...`);
|
||||
await page.reload({timeout: 0, waitUntil: "domcontentloaded"});
|
||||
}
|
||||
if (waitTime > 5 * 60 * 1000) {
|
||||
logger.debug(`[${spawnId}] Waited for more than 5 minutes, refreshing the page...`);
|
||||
await page.reload({timeout: 0, waitUntil: "domcontentloaded"});
|
||||
waitTime = 0;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error(`[${spawnId}] Error while keeping the page alive: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
export { keepAlive };
|
||||
Reference in New Issue
Block a user