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
+31 -31
View File
@@ -1,59 +1,59 @@
const {_android: android} = require('playwright');
const ExcelUtil = require("./src/excel/ExcelUtil");
const CommandorPage = require("./src/workers/CommandorPage");
const MongoManager = require("./src/workers/mongo_manager");
function delay(delayInms) {
return new Promise(resolve => {
setTimeout(() => {
resolve(2);
}, delayInms);
});
}
const {MongoManager, formatDate} = require("./src/workers/mongo_manager");
const mongoManager = new MongoManager();
let excelUtil = new ExcelUtil();
let contactList = excelUtil.readContacts();
mongoManager.connect().then(r => console.log("mongo connected"))
// 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) {
console.log(`Model: ${device.model()}`);
console.log(`Serial: ${device.serial()}`);
let commandor = new CommandorPage(contactPojo, device, mongoManager);
//read contacts form excel
return await commandor.loadPage();
if (await needToBook(contactPojo, mongoManager)) {
let commandor = new CommandorPage(contactPojo, device, mongoManager);
//read contacts form excel
return await commandor.loadPage();
} else {
console.log("do not send request --> skip")
}
}
mongoManager.connect().then(r => console.log("mongo connected"))
async function printFiles() {
let devices = await android.devices();
await contactList.reduce(async (promise, contactPojo) => {
async function startWithList(contacts, device) {
await contacts.reduce(async (promise, contactPojo) => {
// This line will wait for the last async function to finish.
// The first iteration uses an already resolved Promise
// so, it will immediately continue.
await promise;
const contents = await startBook(contactPojo, devices[0]);
const contents = await startBook(contactPojo, device);
console.log(contents);
}, Promise.resolve());
}
(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.storeType = storeType;
this.url = url;
this.mail = mail;
this.email = mail;
this.type = type;
this.source_from = ""
}
to_mongo_dict() {
@@ -20,10 +21,11 @@ class ReserveResultPojo {
phone: this.phoneNumber,
firstName: this.firstName,
lastName: this.lastName,
email: this.mail,
email: this.email,
passport: this.passportNumber,
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 ReserveResultPojo = require("../models/ReserveResultPojo");
const PublishType = require("../models/PublishType");
const RDV_URL = "http://192.168.0.44:8000/test_appointment.html"
// const RDV_URL = "https://rendezvousparis.hermes.com/client/register";
// 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"
@@ -59,6 +59,7 @@ class CommandorPage {
console.log("loadPage() called");
await this.device.shell('am force-stop com.android.chrome');
const context = await this.device.launchBrowser({command: '--incognito'});
// await context.clearCookies()
// Use BrowserContext as usual.
this.page = await context.newPage();
@@ -144,6 +145,7 @@ class CommandorPage {
async fillFields(page) {
console.log("fillFields called")
if (!this.isFillingFields) {
this.isFillingFields = true;
await this.chooseStore(page);
@@ -155,7 +157,7 @@ class CommandorPage {
await this.inputPassportId(page)
await this.checkCGU(page)
await this.resolveCaptcha(page)
await delay(100 * 1000)
await delay(10 * 1000)
}
}
@@ -176,7 +178,7 @@ class CommandorPage {
async resolveCaptcha(page) {
if (RDV_URL.includes("192")) {
this.isTerminated = true;
await this.push_message_to_queue(PublishType.SUCCESS)
return
}
this.captchaSolver = new SolveCaptcha(page);
@@ -208,13 +210,17 @@ class CommandorPage {
}
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") {
return
}
// save to mongoDb
let reserve = ReserveResultPojo.create_from_contact(this.contact, this.page.url(), 1, publishType)
await this.mongoManager.saveReserveToDb(reserve)
let reserve = ReserveResultPojo.create_from_contact(this.contact, id, this.page.url(), 1, publishType);
reserve.source_from = this.device.model();
await this.mongoManager.saveReserveToDb(reserve.to_mongo_dict())
this.isTerminated = true
}
}
+3 -3
View File
@@ -9,7 +9,7 @@ const DESTINATION_EMAIL_LIST = "DESTINATION_EMAIL_LIST"
const {MongoClient} = require('mongodb');
const ReserveResultPojo = require("../models/ReserveResultPojo");
const ContactPojo = require("../models/ContactPojo");
require("../models/ContactPojo");
function formatDate(date) {
let d = new Date(date),
@@ -37,7 +37,7 @@ class MongoManager {
this.db = this.client.db("appointment")
}
async getAllSuccessfulItemsForDay(day) {
async getAllSuccessfulItemsForDay(day) {
return await this.db.collection(day).find().toArray()
}
@@ -48,4 +48,4 @@ class MongoManager {
}
module.exports = MongoManager
module.exports = {MongoManager, formatDate}