From 8ec0443a01214fa616bf755206f0c189fc533f15 Mon Sep 17 00:00:00 2001 From: Lei PAN Date: Thu, 30 Nov 2023 16:19:48 +0100 Subject: [PATCH] add timeout for pages() --- src/workers/CommandorPage.js | 100 +++++++++++++++------------- src/workers/OCRChecker.js | 1 + src/workers/SlidingCaptchaSolver.js | 7 +- 3 files changed, 58 insertions(+), 50 deletions(-) diff --git a/src/workers/CommandorPage.js b/src/workers/CommandorPage.js index 3966fa6..aca8cf1 100644 --- a/src/workers/CommandorPage.js +++ b/src/workers/CommandorPage.js @@ -32,6 +32,8 @@ const MESSAGE_URL_VALIDATION_FR = "Nous avons envoyé un lien par e-mail." const MESSAGE_URL_VALIDATION_EN = "Please click on the link we sent by email" const REGEX_RDV_URL = "https:\/\/rendezvousparis\.hermes\.com\/client\/register\/[A-Z0-9]+" const DEFAULT_STORE = 'faubourg'; +const timeout = (prom, time) => + Promise.race([prom, new Promise((_r, rej) => setTimeout(rej, time))]); function delay(delayInMs) { return new Promise(resolve => { @@ -178,8 +180,9 @@ class CommandorPage { if (this.isTerminated) { log(this.device.model() + ":request terminated, send cancel()"); try { - if (this.page !== undefined) + if (this.page !== undefined) { await this.page.close() + } } catch (e) { console.log(e) } @@ -786,7 +789,6 @@ class CommandorPage { } } - async checkResultWithOcr() { logWithDevice("checkResultWithOcr() called.", this.device) if (this.device.model() === "M2006C3LG") @@ -806,7 +808,9 @@ class CommandorPage { await this.slidingCaptcha(async (isSuccessful) => { logWithDevice("SLIDING_CAPTCHA result is " + isSuccessful, this.device) if (isSuccessful) { + await delay(5 * 1000) checkResult = await this.ocrChecker.get_result(); + console.log(checkResult); } else { checkResult = OCRResult.TERMINAED this.isTerminated = true @@ -836,41 +840,61 @@ class CommandorPage { browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser", headless: false, defaultViewport: null }) - logWithDevice("get pages", this.device) - let pages = await this.browser.pages(); + try { - for (const currentPage of pages) { + logWithDevice("get pages", this.device) + // let pages = await this.browser.pages(); + let pages = await timeout(this.browser.pages(), 5 * 1000); + pages.forEach((currentPage) => { if (currentPage.url() === RDV_URL) { - logWithDevice("get content", this.device) - let pageContent = await currentPage.content() - logWithDevice("content:" + pageContent, this.device) - if (!pageContent.includes("geo.captcha-delivery.com")) { - this.page = currentPage; - break - } else { - try { - await currentPage.close() - } catch (e) { - console.log(e) - } - } + this.page = currentPage; } else { try { - await currentPage.close() + currentPage.close() } catch (e) { console.log(e) } } - } - } catch (e) { - this.isTerminated = true; - console.log(e) - } - logWithDevice("this.page.bringToFront();", this.device) + }) + logWithDevice("this.page.bringToFront();", this.device) + + await this.page.bringToFront(); + await this.fillFields(this.page) + await delay(2 * 1000); + } catch (e) { + console.log(e) + await this.resetBrowser() + // this.isTerminated = true; + } + // try { + // for (const currentPage of pages) { + // if (currentPage.url() === RDV_URL) { + // logWithDevice("get content", this.device) + // let pageContent = await currentPage.content() + // logWithDevice("content:" + pageContent, this.device) + // if (!pageContent.includes("geo.captcha-delivery.com")) { + // this.page = currentPage; + // break + // } else { + // try { + // await currentPage.close() + // } catch (e) { + // console.log(e) + // } + // } + // } else { + // try { + // await currentPage.close() + // } catch (e) { + // console.log(e) + // } + // } + // } + // } catch (e) { + // this.isTerminated = true; + // console.log(e) + // } - await this.page.bringToFront(); - await this.fillFields(this.page) - await delay(2 * 1000); } catch (e) { console.log(e) this.isTerminated = true @@ -879,27 +903,7 @@ class CommandorPage { logWithDevice("this.browser.isConnected() is " + this.browser.isConnected(), this.device) try { if (this.browser.isConnected()) { - // logWithDevice("get pages", this.device) - // let pages = await this.browser.pages(); logWithDevice("will use old page", this.device) - // try { - // pages.forEach((currentPage) => { - // if (currentPage.url() === RDV_URL) { - // this.page = currentPage; - // } else { - // // try { - // // if (!currentPage.isClosed()) { - // // currentPage.close() - // // } - // // } catch (e) { - // // console.log(e) - // // } - // } - // }) - // } catch (e) { - // this.isTerminated = true - // console.log(e) - // } logWithDevice("this.page.bringToFront();", this.device) await this.page.bringToFront(); // this.page = pages; diff --git a/src/workers/OCRChecker.js b/src/workers/OCRChecker.js index 12019e0..df5b847 100644 --- a/src/workers/OCRChecker.js +++ b/src/workers/OCRChecker.js @@ -194,6 +194,7 @@ class OCRChecker { async take_screen_shot() { let name = this.get_file_name() console.log("will take screenshot for " + this.device.model() + ":" + this.device.serial()) + console.log("name is " + name) let stdout1 = await exec("adb -s " + this.device.serial() + " exec-out screencap -p > " + name) await delay(5000); console.log(`stdout: ${stdout1}`); diff --git a/src/workers/SlidingCaptchaSolver.js b/src/workers/SlidingCaptchaSolver.js index c5798d4..66dc51f 100644 --- a/src/workers/SlidingCaptchaSolver.js +++ b/src/workers/SlidingCaptchaSolver.js @@ -34,17 +34,18 @@ class SlidingCaptchaSolver { let stdout1 = await exec("adb -s " + device.serial() + " exec-out screencap -p > " + name) // await delay(5000); console.log(`stdout: ${stdout1}`); - await delay(1000); + await delay(5000); } async sliding_captcha(device, onResult) { // take screenshot - console.log("take screenshot") + console.log("sliding_captcha.take screenshot") let blockedFileName = uuidv4(); let blockedImageFileName = "blocked_" + blockedFileName + ".png" await this.take_screen_shot(blockedImageFileName, device) //get resolution of screen + console.log("sliding_captcha.sendRequest:" + blockedImageFileName) await this.sendRequest(blockedImageFileName, async (detectedPositionList) => { console.log("detectedPosition: " + device.model() + ":" + detectedPositionList); if (detectedPositionList.length >= 2) { @@ -81,6 +82,7 @@ class SlidingCaptchaSolver { }) } + async deleteFile(filename) { try { fs.unlinkSync(filename) @@ -89,6 +91,7 @@ class SlidingCaptchaSolver { return OCRResult.TERMINAED } } + async sendRequest(fileName, callback) { const fileStream = fs.createReadStream(fileName); let response = await axios.post(DEEPLEARNING_CAPTCHA_HOST, fileStream, {