From 52ad4dea000d88caeac205fb12ab0ce8c9f23244 Mon Sep 17 00:00:00 2001 From: gohoski Date: Wed, 8 Jan 2025 03:10:49 +0300 Subject: [PATCH] properly catch hCaptcha window closing after timeout. please note that you can't increase the timeout in any way, even by clicking,so the only option we have is to just reinstate the solving process --- package-lock.json | 1 - src/lib/SunoApi.ts | 25 ++++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index c15f1c8..4cfc7d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,6 @@ "next-swagger-doc": "^0.4.0", "pino": "^8.19.0", "pino-pretty": "^11.0.0", - "playwright-core": "^1.49.1", "react": "^18", "react-dom": "^18", "react-markdown": "^9.0.1", diff --git a/src/lib/SunoApi.ts b/src/lib/SunoApi.ts index a6fb8de..959fff4 100644 --- a/src/lib/SunoApi.ts +++ b/src/lib/SunoApi.ts @@ -300,8 +300,8 @@ class SunoApi { new Promise(async (resolve, reject) => { const frame = page.frameLocator('iframe[title*="hCaptcha"]'); const challenge = frame.locator('.challenge-container'); - while (true) { - try { + try { + while (true) { await page.waitForResponse('https://img**.hcaptcha.com/**', { timeout: 60000 }); // wait for hCaptcha image to load while (true) { // wait for all requests to finish try { @@ -354,15 +354,18 @@ class SunoApi { await this.click(challenge, { x: +data.x, y: +data.y }); }; } - /*await*/ this.click(frame.locator('.button-submit')); // await is commented because we need to call waitForResponse at the same time - } catch(e: any) { - if (e.message.includes('viewport') || e.message.includes('timeout')) // when hCaptcha window has been closed due to inactivity, - this.click(button); // click the Create button again to trigger the CAPTCHA - else if (e.message.includes('been closed')) // catch error when closing the browser - resolve(); - else - reject(e); + this.click(frame.locator('.button-submit')).catch(e => { + if (e.message.includes('viewport')) // when hCaptcha window has been closed due to inactivity, + this.click(button); // click the Create button again to trigger the CAPTCHA + else + throw e; + }); } + } catch(e: any) { + if (e.message.includes('been closed')) // catch error when closing the browser + resolve(); + else + reject(e); } }).catch(e => { browser.browser()?.close(); @@ -409,7 +412,7 @@ class SunoApi { ): Promise { await this.keepAlive(false); const startTime = Date.now(); - const audios = this.generateSongs( + const audios = await this.generateSongs( prompt, false, undefined,