Merge branch 'master' of bitbucket.org:panleicim/appointment_tool_js

This commit is contained in:
2023-05-17 14:25:51 +02:00
9 changed files with 274 additions and 42 deletions
+3 -1
View File
@@ -10,7 +10,8 @@ const SEVEN_DAYS_IN_S = 3600 * 24 * 7;
const NINETY_DAYS_IN_S = 3600 * 24 * 30 * 3;
let excelUtil = new ExcelUtil();
let collectionName = formatDate(new Date())
device_to_excludes = ["47e7e36b", "e30eb015","774687ff"]
// device_to_excludes = ["47e7e36b", "e30eb015"]
device_to_excludes = ["47e7e36b"]
async function filterAlreadyBookedContacts(contactList) {
let alreadyBookedContacts = await mongoManager.getAllSuccessfulItemsForDay(collectionName);
@@ -124,6 +125,7 @@ async function startBook(contactPojo, device, selectedStore, audioAnalyse, alert
async function startWithList(contacts, device, selectedStore, audioAnalyse, alertBeep) {
let duplicatedList = [].concat(contacts).concat(contacts).concat(contacts)
// let duplicatedList = [].concat(contacts)
await duplicatedList.reduce(async (promise, contactPojo) => {
// This line will wait for the last async function to finish.
// The first iteration uses an already resolved Promise
+4 -1
View File
@@ -10,7 +10,10 @@ class ExcelUtil {
(info, index, list) => {
if (index > 0 && info.length > 0) {
// console.log(info)
let name = info[0].split(" ")
if (info[0].includes("Aijing")){
console.log("found");
}
let name = info[0].trim().split(" ")
let firstName = name[1];
let lastName = name[0];
let phoneNumber = info[1];
+7
View File
@@ -0,0 +1,7 @@
class Position {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
module.exports = Position
+113 -32
View File
@@ -7,7 +7,8 @@ const {
shell
} = require('electron')
const GeoCaptchaSolver = require("./GeoCaptchaSolver");
// const RDV_URL = "http://192.168.0.13:8000/test_appointment.html";
const SlidingCaptchaSolver = require("./SlidingCaptchaSolver");
// const RDV_URL = "http://192.168.0.41:8000/test_appointment.html"
const RDV_URL = "https://rendezvousparis.hermes.com/client/register";
const BLANK_URL = "about:blank"
const ERROR_CAPTCHA_UNSOLVABLE = "ERROR_CAPTCHA_UNSOLVABLE";
@@ -36,6 +37,7 @@ 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';
const searchTexts = ['hermes+rdv+online+paris', 'hermes+rdv+enligne+paris', 'hermes+rdv+en+ligne+paris', 'hermes+rendezvous+en+ligne+paris', 'hermes+appointment+online+paris', 'hermes+appointment+online+paris', 'appointment+hermes+paris+on+line', 'hermes+rendez+vous+online+paris', 'hermes+rendez+vous+paris+en+ligne', 'hermes+rendez+vous+paris+enligne', 'hermes+rendez+vous+paris+online', 'online+appointment+hermes+paris', 'hermes+online+appointment+paris', 'paris+hermes+online+appointment']
function delay(delayInMs) {
@@ -58,6 +60,10 @@ function log(message) {
appointmentLogger.log({level: "info", message: message})
}
function logWithDevice(message, device) {
appointmentLogger.log({level: "info", message: device.model() + ":" + message})
}
class CommandorPage {
constructor(contact, device, mongoManager, selectedStore = DEFAULT_STORE, audioAnalyse = false, alertBeep = false) {
this.contact = contact;
@@ -101,7 +107,6 @@ class CommandorPage {
try {
const item = searchTexts[Math.floor(Math.random() * searchTexts.length)];
await this.page.goto("https://www.google.com/search?q=" + item + "&lr=lang_en", {timeout: 90 * 1000});
// await this.page.goto(RDV_URL, {timeout: 90 * 1000});
} catch (e) {
log(e)
this.isTerminated = true
@@ -113,10 +118,13 @@ class CommandorPage {
log(e)
}
try {
// await this.setUpCookies()
this.page.locator(':nth-match(:text("rendezvousparis"), 1)').click()
} catch (e) {
log(e)
this.isTerminated = true
if (!this.page.url().includes(RDV_URL)) {
this.isTerminated = true;
}
}
let cancel
@@ -185,7 +193,7 @@ class CommandorPage {
if (!page.isClosed()) {
if (!this.isPhoneInput) {
await page.locator(PHONE_NUMBER).focus();
await page.locator(PHONE_NUMBER).fill("+33" + this.contact.phoneNumber);
await page.locator(PHONE_NUMBER).fill("+330" + this.contact.phoneNumber);
this.isPhoneInput = true;
}
}
@@ -288,6 +296,7 @@ class CommandorPage {
}
async clickValid(page) {
await this.saveCookies()
await delay(getRandomWaitTime())
try {
if (!this.page.isClosed()) {
@@ -357,7 +366,7 @@ class CommandorPage {
let captcha_container = await iframeHandler.locator(CAPTCHA_CONTAINER)
let html = await captcha_container.innerHTML()
console.log("audio_tag: " + html);
return html.includes("You have been blocked")
return html.includes("You have been blocked") || html.includes("Vous avez été bloqué")
}
async onPageLoad(currentPage) {
@@ -374,29 +383,42 @@ class CommandorPage {
shell.beep()
}
}
log("发现datadome");
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")) {
await this.resetBrowser()
} else if (content.includes("PROXY_CONNECTION_FAILED")) {
logWithDevice("PROXY_CONNECTION_FAILED, will reload page", this.device);
await delay(2000)
await this.page.reload()
} else if (content.includes("ERR_NETWORK_CHANGED")) {
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")) {
logWithDevice("ERR_TIMED_OUT, will reload page", this.device);
await delay(2000)
await this.page.reload()
} else if (content.includes("408 Request Time-out")) {
logWithDevice("Request Time-out, will reload page", this.device);
await delay(2000)
await this.page.reload()
} else {
if (currentPage.url() === RDV_URL) {
await this.fillFields(this.page);
await this.saveCookies()
// if (this.isFillingFields)
// await this.getErrors()
} else {
if (content.includes(MESSAGE_URL_VALIDATION_FR) || content.includes(MESSAGE_URL_VALIDATION_EN)) {
log("successful");
// save cookies
try {
let cookies = await this.context.cookies();
let cookiesInJson = [];
cookies.forEach((cookie) => {
cookiesInJson.push(JSON.stringify(cookie))
})
await require("fs").writeFileSync(this.contact.mail + '.txt', cookiesInJson.join('\n'));
} catch (e) {
console.log(e)
}
await this.push_message_to_queue(PublishType.SUCCESS);
} else if (content.includes(EMPTY_RESPONSE_ERROR)) {
log("EMPTY_RESPONSE_ERROR error received, will quit")
@@ -412,45 +434,102 @@ class CommandorPage {
}
}
async saveCookies() {
log("saveCookies() called.")
try {
let cookies = await this.page.context().cookies();
let cookiesInJson = [];
cookies.forEach((cookie) => {
cookiesInJson.push(JSON.stringify(cookie))
})
await require("fs").writeFileSync(this.contact.mail + '.txt', cookiesInJson.join('\n'));
} catch (e) {
console.log(e)
}
}
async setUpCookies() {
// load cookies from file
try {
logWithDevice("will add cookies", this.device)
const fs = require('fs');
// let cookiesFile = fs.readFileSync('vampuka_fisherleyba@aol.com.txt', 'utf8');
let cookiesFile = fs.readFileSync(this.contact.mail + '.txt', 'utf8');
let cookiesList = cookiesFile.split("\n")
let cookiesListToAdd = []
cookiesList.forEach((cookie) => {
let cookieToAdd = JSON.parse(cookie)
// if (cookieToAdd.name === "datadome") {
cookiesListToAdd.push(cookieToAdd)
// }
})
await this.context.addCookies(cookiesListToAdd)
} catch (e) {
console.log(e)
}
}
async checkAudioBtn() {
let isBlocked = await this.isBlocked()
if (!isBlocked) {
let audioBtn = await this.page.frameLocator("iframe").locator("#captcha__audio__button");
log("audioBtn found")
audioBtn.click()
let captchaSolver = new GeoCaptchaSolver(this.page, this.device, this.isTerminated)
await captchaSolver.solve((isSuccessful) => {
if (!isSuccessful) {
this.isTerminated = true
//try to sliding capthca at first
let slidingCaptchaSolver = new SlidingCaptchaSolver(this.device);
await slidingCaptchaSolver.solve(this.page, async (isSuccessful) => {
console.log("check isAlwaysBlocked")
let isAlwaysBlocked = await this.isBlocked();
if (isAlwaysBlocked) {
await this.resolveByAudio();
}
})
} else {
log("audioBtn not found")
console.log("audioBtn not found")
console.log("we are blocked")
//
await this.resetBrowser()
}
}
async resolveByAudio() {
let audioBtn = await this.page.frameLocator("iframe").locator("#captcha__audio__button");
log("audioBtn found")
audioBtn.click()
let captchaSolver = new GeoCaptchaSolver(this.page, this.device, this.isTerminated)
await captchaSolver.solve((isSuccessful) => {
if (!isSuccessful) {
this.isTerminated = true
}
})
}
async onResponse(response) {
// let rex = new RegExp(REGEX_RDV_URL)
// log("onResponse with url:" + response.url())
// if (rex.test(response.url())) {
// log("rdv url found:" + response.url())
// await this.push_message_to_db(PublishType.SUCCESS, response.url())
let rex = new RegExp(REGEX_RDV_URL)
log("onResponse with url:" + response.url())
// log("onResponse with url:" + response.body())
if (rex.test(response.url())) {
log("rdv url found:" + response.url())
// save cookies
await this.saveCookies();
await this.push_message_to_db(PublishType.SUCCESS, response.url())
}
// if (response.url().includes("geo.captcha-delivery.com/interstitial")) {
// logWithDevice("interstitial: response.status is " + response.status())
// await this.setUpCookies()
// }
}
async push_message_to_queue(publishType) {
let url = this.page.url();
await this.push_message_to_db(publishType, url)
}
async push_message_to_db(publishType, url) {
let splitedUrl = url.split("/");
let id = splitedUrl[splitedUrl.length - 1];
if (url === "https://rendezvousparis.hermes.com/client/welcome") {
return
}
// save to mongoDb
let reserve = ReserveResultPojo.create_from_contact(this.contact, id, this.page.url(), this.choosedStore, publishType);
let reserve = ReserveResultPojo.create_from_contact(this.contact, id, url, this.choosedStore, publishType);
reserve.source_from = this.device.model();
await this.mongoManager.saveReserveToDb(reserve.to_mongo_dict())
await this.deleteFromBlackList()
@@ -523,6 +602,8 @@ class CommandorPage {
await delay(1000)
this.isTerminated = true
}
}
module.exports = CommandorPage
+2 -2
View File
@@ -60,7 +60,7 @@ async function sendRequest(fileName, callback) {
}
}
class GeoCaptchaSolver {
class AudioCaptchaSolver {
constructor(page, device, isTerminated) {
this.page = page;
this.isTerminated = isTerminated
@@ -170,4 +170,4 @@ class GeoCaptchaSolver {
}
module.exports = GeoCaptchaSolver
module.exports = AudioCaptchaSolver
+128
View File
@@ -0,0 +1,128 @@
const fs = require("fs");
const {exec} = require("child_process");
const {promisify} = require('util')
const Position = require("../models/Position");
const sizeOf = promisify(require('image-size'));
const axios = require("axios");
const {v4: uuidv4} = require('uuid');
const DEEPLEARNING_CAPTCHA_HOST = "http://appointment.lpaconsulting.fr:9000"
function delay(delayInMs) {
return new Promise(resolve => {
setTimeout(() => {
resolve(2);
}, delayInMs);
});
}
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
const positionInfo = new Map();
positionInfo.set("2109119DG", new Position(211, 1710))
positionInfo.set("ONEPLUS A6000", new Position(211, 1706))
positionInfo.set("SM-C5000", new Position(180, 1794))
class SlidingCaptchaSolver {
constructor(device) {
this.device = device;
}
async solve(page, onResult) {
await this.sliding_captcha(this.device, onResult)
}
async take_screen_shot(name, device) {
// let stdout1 = await exec("adb -s " + device.serial() + " shell screencap -p /sdcard/" + name)
console.log("take screenshot " + name);
await device.screenshot({path: name});
// let stdout1 = await exec("adb -s " + device.serial() + " exec-out screencap -p > " + name)
await delay(1000);
// console.log(`stdout: ${stdout1}`);
}
async sliding_captcha(device, onResult) {
// take screenshot
console.log("take screenshot")
let blockedFileName = uuidv4();
let blockedImageFileName = "blocked_" + blockedFileName + ".png"
await this.take_screen_shot(blockedImageFileName, device)
//get resolution of screen
await this.sendRequest(blockedImageFileName, async (detectedPositionList) => {
console.log("detectedPosition is " + detectedPositionList);
if (detectedPositionList.length === 2) {
// #xiaomi
let startPosition = detectedPositionList.filter((positionInfo) => {
return positionInfo.label === "origin"
})[0]
let targetPosition = detectedPositionList.filter((positionInfo) => {
return positionInfo.label === "target"
})[0]
if (startPosition !== undefined) {
let y0 = (startPosition.y2 + startPosition.y1) / 2.0;
let x0 = (startPosition.x2 + startPosition.x1) / 2.0;
let y1 = (targetPosition.y2 + targetPosition.y1) / 2.0;
let x1 = (targetPosition.x2 + targetPosition.x1) / 2.0;
let width = targetPosition.x2 - targetPosition.x1;
let randomTime = randomIntFromInterval(100, 500)
let cmd = `adb -s ${device.serial()} shell input swipe ${x0} ${y0} ${x1 + width * 0.5} ${y0} ${1000 + randomTime}`
await delay(2000);
console.log("cmd is " + cmd);
console.log("will slide captcha");
await exec(cmd);
await delay(5000);
onResult(true)
} else {
console.log("startPosition not found for " + device.model())
onResult(false)
}
} else {
console.log("startPosition not found for " + device.model())
onResult(false)
}
})
}
async sendRequest(fileName, callback) {
const fileStream = fs.createReadStream(fileName);
let response = await axios.post(DEEPLEARNING_CAPTCHA_HOST, fileStream, {
headers: {
'Content-Type': 'wav',
'Authorization': 'Bearer 97e36f7e-340e-4c02-b329-9415faee38c3'
},
timeout: 30 * 1000
}).catch(err => {
console.log(err.code);
console.log(err.stack);
console.log(err.message);
callback([]);
})
if (response === undefined) {
await callback({});
return
}
if (response.status === 200) {
let result = response.data;
console.log(result);
await callback(result);
// console.log("will delete png file: " + fileName);
// await fs.unlink(fileName, (err) => {
// if (err) throw err;
// console.log(fileName + ' was deleted');
// })
} else {
console.log("error");
await callback([]);
}
}
}
module.exports = SlidingCaptchaSolver