first commit

This commit is contained in:
2022-09-07 15:12:11 +02:00
parent 1689094851
commit 6266083ab8
13 changed files with 760 additions and 32 deletions
+193
View File
@@ -0,0 +1,193 @@
const {_android: android} = require("playwright");
const SolveCaptcha = require("./SolveCaptcha");
// const RDV_URL = "http://192.168.0.44:8000/test_appointment.html"
const RDV_URL = "https://rendezvousparis.hermes.com/client/register";
const COUNTRY_ID = "#phone_country"
const PHONE_NUMBER = "#phone_number"
const EMAIL_ID = "#email"
const PREFER_STORE = "#prefer"
const LAST_NAME = "#surname"
const FIRST_NAME = "#name"
const CGU_ID = "#cgu"
const PROCESSING_ID = "#processing"
const PASSPORT_ID = "#passport_id"
const CONFIRMED_MESSAGE = "Your request for a Leather Goods appointment has been registered"
const CONFIRMED_MESSAGE_FR = "Votre demande de rendez-vous Maroquinerie a bien été enregistrée et nous vous en remercions."
const MESSAGE_URL_VALIDATION_FR = "Nous avons envoyé un lien par e-mail."
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é"
function delay(delayInms) {
return new Promise(resolve => {
setTimeout(() => {
resolve(2);
}, delayInms);
});
}
function getRandom() {
return Math.floor(Math.random() * 5);
}
function getRandomWaitTime() {
return getRandom() * 1000
}
class CommandorPage {
constructor(contact) {
this.contact = contact;
}
async loadPage() {
// Connect to the device.
const [device] = await android.devices();
console.log(`Model: ${device.model()}`);
console.log(`Serial: ${device.serial()}`);
{
// --------------------- Browser -----------------------
// Launch Chrome browser.
await device.shell('am force-stop com.android.chrome');
const context = await device.launchBrowser({command: '--incognito'});
// Use BrowserContext as usual.
const page = await context.newPage();
this.page = page
page.on("load", this.onPageLoad)
await page.goto(RDV_URL);
// await this.fillFields(page)
console.log(await page.evaluate(() => window.location.href));
//wait 10 mins
await delay(10 * 60 * 1000);
}
// Close the device.
await device.close();
}
async chooseCountry(page) {
await page.locator(COUNTRY_ID).focus();
await delay(getRandomWaitTime())
await page.click(COUNTRY_ID);
await delay(getRandomWaitTime())
await page.selectOption(COUNTRY_ID, 'FR');
await delay(getRandomWaitTime())
await page.click(COUNTRY_ID);
}
async fillEmail(page) {
await page.locator(EMAIL_ID).focus();
await delay(getRandomWaitTime())
await page.locator(EMAIL_ID).fill(this.contact.mail);
}
async inputPhoneNumber(page) {
await page.locator(PHONE_NUMBER).focus()
await page.locator(PHONE_NUMBER).fill("0" + this.contact.phoneNumber)
}
async inputName(page) {
await page.locator(LAST_NAME).focus()
await delay(getRandomWaitTime())
await page.locator(LAST_NAME).fill(this.contact.lastName)
await page.locator(FIRST_NAME).focus()
await delay(getRandomWaitTime())
await page.locator(FIRST_NAME).fill(this.contact.firstName)
}
async inputPassportId(page) {
await page.locator(PASSPORT_ID).focus()
await delay(getRandomWaitTime())
await page.locator(PASSPORT_ID).fill(this.contact.passportNumber.toString())
}
async checkCGU(page) {
await page.locator(CGU_ID).focus()
await page.locator(CGU_ID).click()
await delay(getRandomWaitTime())
await page.locator(PROCESSING_ID).focus()
await page.locator(PROCESSING_ID).click()
}
async chooseStore(page) {
await page.locator(PREFER_STORE).focus()
await delay(1000)
await page.click(PREFER_STORE);
await page.selectOption(PREFER_STORE, "faubourg");
await page.click(PREFER_STORE);
}
async fillFields(page) {
await this.chooseStore(page)
await this.inputName(page)
await this.chooseCountry(page);
await this.inputPhoneNumber(page)
await this.fillEmail(page)
await this.inputPhoneNumber(page)
await this.inputPassportId(page)
await this.checkCGU(page)
await this.resolveCaptcha(page)
await delay(100 * 1000)
}
async clickValid(page) {
await delay(getRandomWaitTime())
try {
page.evaluate(() => {
document.getElementsByClassName("btn")[0].focus();
})
await delay(getRandomWaitTime())
page.evaluate(() => {
document.getElementsByClassName("btn")[0].click();
})
} catch (e) {
console.log(e)
}
}
async resolveCaptcha(page) {
this.captchaSolver = new SolveCaptcha(page);
await this.captchaSolver.start((solution) => {
console.log("solution is: " + solution);
page.evaluate((solution) => {
document.getElementById("g-recaptcha-response").innerHTML = solution;
}, solution)
this.clickValid()
})
}
async onPageLoad() {
let content = await this.page.content();
let captcha_url = "geo.captcha-delivery.com/captcha"
if (content.toString().includes(captcha_url)) {
console.log("we are blockeds")
} else {
if (this.page.url() === RDV_URL) {
await this.fillFields()
} else {
if (content.includes(MESSAGE_URL_VALIDATION_FR)) {
console.log("successful")
this.push_message_to_queue(PublishType.SUCCESS)
}
}
}
}
push_message_to_queue(publishType) {
let url = this.page.url()
if (url === "https://rendezvousparis.hermes.com/client/welcome") {
return
}
}
}
module.exports = CommandorPage