optimzation

This commit is contained in:
2022-09-07 22:23:59 +02:00
parent cdd47327b1
commit b264e5830a
4 changed files with 53 additions and 45 deletions
+28 -28
View File
@@ -1,59 +1,59 @@
const {_android: android} = require('playwright'); const {_android: android} = require('playwright');
const ExcelUtil = require("./src/excel/ExcelUtil"); const ExcelUtil = require("./src/excel/ExcelUtil");
const CommandorPage = require("./src/workers/CommandorPage"); const CommandorPage = require("./src/workers/CommandorPage");
const MongoManager = require("./src/workers/mongo_manager"); const {MongoManager, formatDate} = require("./src/workers/mongo_manager");
function delay(delayInms) {
return new Promise(resolve => {
setTimeout(() => {
resolve(2);
}, delayInms);
});
}
const mongoManager = new MongoManager(); const mongoManager = new MongoManager();
let excelUtil = new ExcelUtil(); let excelUtil = new ExcelUtil();
let contactList = excelUtil.readContacts(); let contactList = excelUtil.readContacts();
mongoManager.connect().then(r => console.log("mongo connected"))
// Connect to the device. // Connect to the device.
async function needToBook(contact, mongoManager) {
let collectionName = formatDate(new Date())
let alreadBooked = await mongoManager.getAllSuccessfulItemsForDay(collectionName)
let toReturn = true;
await alreadBooked.forEach((bookedItem) => {
if (bookedItem.email === contact.mail) {
toReturn = false;
}
}
)
return toReturn
}
async function startBook(contactPojo, device) { async function startBook(contactPojo, device) {
console.log(`Model: ${device.model()}`); console.log(`Model: ${device.model()}`);
console.log(`Serial: ${device.serial()}`); console.log(`Serial: ${device.serial()}`);
if (await needToBook(contactPojo, mongoManager)) {
let commandor = new CommandorPage(contactPojo, device, mongoManager); let commandor = new CommandorPage(contactPojo, device, mongoManager);
//read contacts form excel //read contacts form excel
return await commandor.loadPage(); return await commandor.loadPage();
} else {
console.log("do not send request --> skip")
}
} }
mongoManager.connect().then(r => console.log("mongo connected"))
async function printFiles() { async function startWithList(contacts, device) {
let devices = await android.devices(); await contacts.reduce(async (promise, contactPojo) => {
await contactList.reduce(async (promise, contactPojo) => {
// This line will wait for the last async function to finish. // This line will wait for the last async function to finish.
// The first iteration uses an already resolved Promise // The first iteration uses an already resolved Promise
// so, it will immediately continue. // so, it will immediately continue.
await promise; await promise;
const contents = await startBook(contactPojo, devices[0]); const contents = await startBook(contactPojo, device);
console.log(contents); console.log(contents);
}, Promise.resolve()); }, Promise.resolve());
} }
(async () => { (async () => {
printFiles() android.devices().then((devices) => {
let segmentNumber = contactList.length / devices.length;
for (let i = 0; i < devices.length; i++) {
startWithList(contactList.slice(i * segmentNumber, segmentNumber * (i + 1)), devices[i]);
}
})
})() })()
// android.devices().then((device_list) => {
// contactList.forEach((contactPojo) => {
// device_list.forEach((device) => {
// (async () => {
// await startBook(contactPojo, device)
// })()
// }
// )
// }
// )
// })
+5 -3
View File
@@ -10,8 +10,9 @@ class ReserveResultPojo {
this.lastName = lastName; this.lastName = lastName;
this.storeType = storeType; this.storeType = storeType;
this.url = url; this.url = url;
this.mail = mail; this.email = mail;
this.type = type; this.type = type;
this.source_from = ""
} }
to_mongo_dict() { to_mongo_dict() {
@@ -20,10 +21,11 @@ class ReserveResultPojo {
phone: this.phoneNumber, phone: this.phoneNumber,
firstName: this.firstName, firstName: this.firstName,
lastName: this.lastName, lastName: this.lastName,
email: this.mail, email: this.email,
passport: this.passportNumber, passport: this.passportNumber,
url: this.url, url: this.url,
store_type: this.storeType store_type: this.storeType,
source_from: this.source_from
} }
} }
+14 -8
View File
@@ -1,11 +1,11 @@
const {_android: android} = require("playwright"); const {_android: android, devices} = require("playwright");
const SolveCaptcha = require("./SolveCaptcha"); const SolveCaptcha = require("./SolveCaptcha");
const ReserveResultPojo = require("../models/ReserveResultPojo"); const ReserveResultPojo = require("../models/ReserveResultPojo");
const PublishType = require("../models/PublishType"); const PublishType = require("../models/PublishType");
const RDV_URL = "http://192.168.0.44:8000/test_appointment.html" // const RDV_URL = "http://192.168.0.44:8000/test_appointment.html"
// const RDV_URL = "https://rendezvousparis.hermes.com/client/register"; const RDV_URL = "https://rendezvousparis.hermes.com/client/register";
const COUNTRY_ID = "#phone_country" const COUNTRY_ID = "#phone_country"
const PHONE_NUMBER = "#phone_number" const PHONE_NUMBER = "#phone_number"
@@ -59,6 +59,7 @@ class CommandorPage {
console.log("loadPage() called"); console.log("loadPage() called");
await this.device.shell('am force-stop com.android.chrome'); await this.device.shell('am force-stop com.android.chrome');
const context = await this.device.launchBrowser({command: '--incognito'}); const context = await this.device.launchBrowser({command: '--incognito'});
// await context.clearCookies()
// Use BrowserContext as usual. // Use BrowserContext as usual.
this.page = await context.newPage(); this.page = await context.newPage();
@@ -144,6 +145,7 @@ class CommandorPage {
async fillFields(page) { async fillFields(page) {
console.log("fillFields called")
if (!this.isFillingFields) { if (!this.isFillingFields) {
this.isFillingFields = true; this.isFillingFields = true;
await this.chooseStore(page); await this.chooseStore(page);
@@ -155,7 +157,7 @@ class CommandorPage {
await this.inputPassportId(page) await this.inputPassportId(page)
await this.checkCGU(page) await this.checkCGU(page)
await this.resolveCaptcha(page) await this.resolveCaptcha(page)
await delay(100 * 1000) await delay(10 * 1000)
} }
} }
@@ -176,7 +178,7 @@ class CommandorPage {
async resolveCaptcha(page) { async resolveCaptcha(page) {
if (RDV_URL.includes("192")) { if (RDV_URL.includes("192")) {
this.isTerminated = true; await this.push_message_to_queue(PublishType.SUCCESS)
return return
} }
this.captchaSolver = new SolveCaptcha(page); this.captchaSolver = new SolveCaptcha(page);
@@ -208,13 +210,17 @@ class CommandorPage {
} }
async push_message_to_queue(publishType) { async push_message_to_queue(publishType) {
let url = this.page.url() let url = this.page.url();
let splitedUrl = url.split("/");
let id = splitedUrl[splitedUrl.length - 1];
if (url === "https://rendezvousparis.hermes.com/client/welcome") { if (url === "https://rendezvousparis.hermes.com/client/welcome") {
return return
} }
// save to mongoDb // save to mongoDb
let reserve = ReserveResultPojo.create_from_contact(this.contact, this.page.url(), 1, publishType) let reserve = ReserveResultPojo.create_from_contact(this.contact, id, this.page.url(), 1, publishType);
await this.mongoManager.saveReserveToDb(reserve) reserve.source_from = this.device.model();
await this.mongoManager.saveReserveToDb(reserve.to_mongo_dict())
this.isTerminated = true
} }
} }
+2 -2
View File
@@ -9,7 +9,7 @@ const DESTINATION_EMAIL_LIST = "DESTINATION_EMAIL_LIST"
const {MongoClient} = require('mongodb'); const {MongoClient} = require('mongodb');
const ReserveResultPojo = require("../models/ReserveResultPojo"); const ReserveResultPojo = require("../models/ReserveResultPojo");
const ContactPojo = require("../models/ContactPojo"); require("../models/ContactPojo");
function formatDate(date) { function formatDate(date) {
let d = new Date(date), let d = new Date(date),
@@ -48,4 +48,4 @@ class MongoManager {
} }
module.exports = MongoManager module.exports = {MongoManager, formatDate}