From e98f740f1a654c80b0c547f2943ed6c65462cf5b Mon Sep 17 00:00:00 2001 From: Holegots Date: Sun, 1 Jan 2023 01:50:37 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Improve=20robustness?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/openai-auth.ts | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/openai-auth.ts b/src/openai-auth.ts index 4c3f1dd..e125646 100644 --- a/src/openai-auth.ts +++ b/src/openai-auth.ts @@ -126,14 +126,16 @@ export async function getOpenAIAuth({ await delay(500) // click login button and wait for navigation to finish - await Promise.all([ - page.waitForNavigation({ - waitUntil: 'networkidle2', - timeout: timeoutMs - }), - - page.click('#__next .btn-primary') - ]) + while (page.url().endsWith('/auth/login')) { + await Promise.all([ + page.waitForNavigation({ + waitUntil: 'networkidle2', + timeout: timeoutMs + }), + page.click('#__next .btn-primary') + ]) + delay(500) + } await checkForChatGPTAtCapacity(page, { timeoutMs }) @@ -179,12 +181,22 @@ export async function getOpenAIAuth({ if (hasNopechaExtension) { await waitForRecaptcha(page, { timeoutMs }) } else if (hasRecaptchaPlugin) { + // Add retry for network unstable console.log('solving captchas using 2captcha...') - const res = await page.solveRecaptchas() - if (res.captchas?.length) { - console.log('captchas result', res) - } else { - console.log('no captchas found') + const retries = 3 + for (let i = 0; i < retries; i++) { + try { + const res = await page.solveRecaptchas() + if (res.captchas?.length) { + console.log('captchas result', res) + break + } else { + console.log('no captchas found') + delay(500) + } + } catch (e) { + console.log('captcha error', e) + } } }