need to use openCv to identifiy text

This commit is contained in:
2023-05-22 00:57:12 +02:00
parent 0c99c6b36f
commit 48a7338928
7 changed files with 222 additions and 96 deletions
+97 -90
View File
@@ -11,7 +11,6 @@ const {
} = require('electron')
const GeoCaptchaSolver = require("./GeoCaptchaSolver");
const SlidingCaptchaSolver = require("./SlidingCaptchaSolver");
const {de} = require("yarn/lib/cli");
const OCRChecker = require("./OCRChecker");
// const RDV_URL = "http://192.168.0.44:8000/test_appointment.html"
const RDV_URL = "https://rendezvousparis.hermes.com/client/register";
@@ -34,12 +33,6 @@ const TIME_OUT = 60 * 1000 * 4//4 mins
const EMPTY_RESPONSE_ERROR = "ERR_EMPTY_RESPONSE"
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 DOUBLE_REQUEST_ERROR_MESSAGE = "A request with the same data has already been validated today."
const DOUBLE_REQUEST_ERROR_MESSAGE_FR = "Une demande avec les données saisies a déjà été validée aujourdhui."
const TOO_MANY_REQUEST_ERROR_MESSAGE = "Due to a large number of requests"
const TOO_MANY_REQUEST_ERROR_MESSAGE_FR = "Suite à un trop grand nombre de demandes"
const CAPTCHA_ERROR_MESSAGE = "Error verifying captcha, please try again"
const CAPTCHA_ERROR_MESSAGE_FR = "La vérification du captcha a échoué"
const REGEX_RDV_URL = "https:\/\/rendezvousparis\.hermes\.com\/client\/register\/[A-Z0-9]+"
const DEFAULT_STORE = 'faubourg';
@@ -90,12 +83,16 @@ class CommandorPage {
async connect_to_browser(ocrResult) {
this.browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser",
headless: false, defaultViewport: null
})
console.log("connect_to_browser() called");
if (!this.browser.isConnected()) {
this.browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser",
headless: false, defaultViewport: null
})
}
let pages = await this.browser.pages();
this.page = pages[pages.length - 1]
// 0 is the last page
this.page = pages[0];
switch (ocrResult) {
case OCRResult.SUCCESS:
// get url and push to server
@@ -105,10 +102,9 @@ class CommandorPage {
case OCRResult.TO_REFRESH:
logWithDevice("will reload page", this.device)
await this.page.reload();
logWithDevice("will disconnect browser", this.device)
await this.browser.disconnect()
await delay(1000);
await this.checkResultWithOcr();
break;
}
let content = await this.page.content();
console.log(content);
@@ -116,7 +112,7 @@ class CommandorPage {
async loadPage() {
// Connect to the device.
log("loadPage() called, with port:" + this.port);
logWithDevice("loadPage() called, with port:" + this.port, this.device);
try {
this.browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser",
@@ -145,14 +141,14 @@ class CommandorPage {
log(e)
this.isTerminated = true
}
try {
if (this.page.url().includes("google")) {
this.page.focus('button >> nth=3')
this.page.click()
}
} catch (e) {
log(e)
}
// try {
// if (this.page.url().includes("google")) {
// this.page.focus('button >> nth=3')
// this.page.click()
// }
// } catch (e) {
// log(e)
// }
try {
const [button] = await this.page.$x("//a[contains(., 'rendezvousparis')]");
console.log("button is " + button)
@@ -165,9 +161,9 @@ class CommandorPage {
// });
} catch (e) {
log(e)
if (!this.page.url().includes(RDV_URL)) {
this.isTerminated = true;
}
// if (!this.page.url().includes(RDV_URL)) {
// this.isTerminated = true;
// }
}
let cancel
@@ -182,10 +178,10 @@ class CommandorPage {
cancel()
return this.browser
} else {
if (this.page.url() === RDV_URL) {
if (!this.isFillingFields)
this.fillFields(this.page);
}
// if (this.page.url() === RDV_URL) {
// if (!this.isFillingFields)
// this.fillFields(this.page);
// }
}
}, 10 * 1000)//interval of 10 seconds
@@ -331,7 +327,8 @@ class CommandorPage {
async fillFields(page) {
log("fillFields called")
log("fillFields called for contact:" + this.contact.mail)
await this.enableDisableAirPlanMode()
if (!this.isFillingFields) {
this.isFillingFields = true;
await this.chooseStore(page);
@@ -364,24 +361,12 @@ class CommandorPage {
// remove debug flag
// const validElement = await page.$('.btn');
console.log("will click on valid button");
console.log("will click on valid button");
console.log("will click on valid button");
await this.page.evaluate(() => {
document.getElementsByClassName("btn")[0].click();
})
// this.browser.disconnect();
// await delay(2000);
// let ocrChecker = new OCRChecker(this.device, this.contact);
// let checkResult = await ocrChecker.get_result();
// switch (checkResult) {
// case OCRResult.SUCCESS:
// // reconnect to page and get url
// await this.connect_to_browser(OCRResult.SUCCESS);
// break;
// case OCRResult.BLOCKED:
// await this.resetBrowser();
// break;
// }
// await this.checkResultWithOcr();
} catch (e) {
log(e);
}
@@ -440,24 +425,17 @@ class CommandorPage {
}
async onPageLoad(currentPage) {
logWithDevice("onPageLoad called with url " + this.page.url(), this.device)
try {
let content = await currentPage.content();
let content = await this.page.content();
let captcha_url = "geo.captcha-delivery.com/captcha";
if (content.toString().includes(captcha_url)) {
if (this.audioAnalyse) {
await this.checkAudioBtn();
}
if (this.alertBeep) {
for (let i = 0; i < 15; i++) {
await delay(1000)
shell.beep()
}
}
await this.checkResultWithOcr()
logWithDevice("发现datadome", this.device);
} else if (content.includes("502 Bad Gateway")) {
logWithDevice("502 Bad Gateway found", this.device)
await this.page.reload()
} else if (currentPage.url().includes("sorry")) {
} else if (this.page.url().includes("sorry")) {
await this.resetBrowser()
} else if (content.includes("PROXY_CONNECTION_FAILED")) {
logWithDevice("PROXY_CONNECTION_FAILED, will reload page", this.device);
@@ -467,12 +445,14 @@ class CommandorPage {
logWithDevice("ERR_NETWORK_CHANGED, will reload page", this.device);
await delay(2000)
await this.page.reload()
} else if (content.includes("CACHE_MISS")) {
logWithDevice("CACHE_MISS, will reload page", this.device);
await delay(2000)
// await this.setUpCookies()
await this.page.reload()
} else if (content.includes("ERR_TIMED_OUT")) {
}
// else if (content.includes("CACHE_MISS")) {
// logWithDevice("CACHE_MISS, will reload page", this.device);
// await delay(2000)
// // await this.setUpCookies()
// await this.page.reload()
// }
else if (content.includes("ERR_TIMED_OUT")) {
logWithDevice("ERR_TIMED_OUT, will reload page", this.device);
await delay(2000)
await this.page.reload()
@@ -481,7 +461,7 @@ class CommandorPage {
await delay(2000)
await this.page.reload()
} else {
if (currentPage.url() === RDV_URL) {
if (this.page.url() === RDV_URL) {
await this.fillFields(this.page);
await this.saveCookies()
// if (this.isFillingFields)
@@ -539,6 +519,14 @@ class CommandorPage {
// }
}
async slidingCaptcha(onResult) {
let slidingCaptchaSolver = new SlidingCaptchaSolver(this.device);
await slidingCaptchaSolver.solve(this.page, async (isSuccessful) => {
console.log("check isAlwaysBlocked")
onResult(isSuccessful)
})
}
async checkAudioBtn() {
let isBlocked = await this.isBlocked()
if (!isBlocked) {
@@ -589,6 +577,7 @@ class CommandorPage {
async push_message_to_queue(publishType) {
let url = this.page.url();
logWithDevice("successful url is " + url, this.device)
await this.push_message_to_db(publishType, url)
}
@@ -640,36 +629,11 @@ class CommandorPage {
let elementHandle = await this.page.$('[title="reCAPTCHA"]')
const iframe = await elementHandle.contentFrame()
await iframe.click("#recaptcha-anchor > div.recaptcha-checkbox-border")
// // .getByRole('checkbox', {name: 'I\'m not a robot'})
// if (enCheckbox) {
// enCheckbox.click()
// } else {
// let frCheckbox = await this.page.$('[title="reCAPTCHA"]').getByRole('checkbox', {name: 'Je ne suis pas un robot'})
// if (frCheckbox) {
// frCheckbox.click()
// } else {
// console.log("recaptcha checkbox not found")
// }
// }
} catch (e) {
log(e);
}
}
async handleError(errorContent) {
log("handle error:" + errorContent);
if (errorContent.includes(DOUBLE_REQUEST_ERROR_MESSAGE) || errorContent.includes(DOUBLE_REQUEST_ERROR_MESSAGE_FR)) {
this.isTerminated = true;
} else if (errorContent.includes(TOO_MANY_REQUEST_ERROR_MESSAGE) || errorContent.includes(TOO_MANY_REQUEST_ERROR_MESSAGE_FR)) {
//add contact to black list and set terminated the task
log("handle error: will save to black list db");
await this.saveToBlackList()
} else if (errorContent.includes(CAPTCHA_ERROR_MESSAGE) || errorContent.includes(CAPTCHA_ERROR_MESSAGE_FR)) {
this.isTerminated = true;
}
}
async removeDebugFlag() {
await delay(1000)
await this.device.shell("am clear-debug-app --persistent com.android.chrome")
@@ -680,12 +644,55 @@ class CommandorPage {
console.log("will reset browser")
await this.device.shell("pm clear com.android.chrome")
await delay(1000)
await this.device.shell("pm am start -n com.android.chrome/com.google.android.apps.chrome.Main")
await this.device.shell("am start -n com.android.chrome/com.google.android.apps.chrome.Main")
await delay(1000)
// await this.skipWelcomePage()
this.isTerminated = true
}
async checkResultWithOcr() {
console.log("checkResultWithOcr() called.")
await delay(2000);
let ocrChecker = new OCRChecker(this.device, this.contact);
let checkResult = await ocrChecker.get_result();
while (checkResult === OCRResult.RECHECK) {
await delay(4000)
logWithDevice("will recheck OCR", this.device)
checkResult = await ocrChecker.get_result();
}
while (checkResult === OCRResult.SLIDING_CAPTCHA) {
await this.slidingCaptcha(async () => {
checkResult = await ocrChecker.get_result();
await this.checkResultWithOcr()
})
await delay(5000)
}
switch (checkResult) {
case OCRResult.SUCCESS:
// reconnect to page and get url
await this.connect_to_browser(OCRResult.SUCCESS);
break;
case OCRResult.RECAPTCHA_ERROR:
this.isTerminated = true;
break;
case OCRResult.TO_REFRESH:
await this.connect_to_browser(OCRResult.TO_REFRESH)
break;
case OCRResult.BLOCKED:
await this.resetBrowser();
break;
}
}
async enableDisableAirPlanMode() {
logWithDevice("will enable aireplan mode", this.device)
await this.device.shell("cmd connectivity airplane-mode enable")
await this.device.shell("adb shell settings put global airplane_mode_on 1")
await delay(1000)
await this.device.shell("adb shell settings put global airplane_mode_on 0")
await this.device.shell("cmd connectivity airplane-mode disable")
}
}
module.exports = CommandorPage
+8
View File
@@ -23,6 +23,8 @@ const CAPTCHA_ERROR_MESSAGE_FR = "La vérification du captcha a échoué"
const BLOCKED_MSG_EN = "have been blocked"
const BLOCKED_MSG_FR = "avez été bloqué"
const CHECKING_MSG_FR = "Verifying"
const ERR_CACHE_MISS = "ERR_CACHE_MISS"
const SLIDING_CAPTCHA_FR = "Glissez vers la droite"
class OCRChecker {
@@ -47,6 +49,12 @@ class OCRChecker {
return OCRResult.RECAPTCHA_ERROR
} else if (result.includes(BLOCKED_MSG_EN) || result.includes(BLOCKED_MSG_FR)) {
return OCRResult.BLOCKED
} else if (result.includes(ERR_CACHE_MISS)) {
return OCRResult.TO_REFRESH
} else if (result.includes(CHECKING_MSG_FR)) {
return OCRResult.RECHECK
} else if (result.includes(SLIDING_CAPTCHA_FR)) {
return OCRResult.SLIDING_CAPTCHA
}
}