add support for LinkValidator

This commit is contained in:
2022-12-09 09:59:11 +01:00
parent 82b297fdd3
commit 7fea859e29
3 changed files with 60 additions and 37 deletions
+1
View File
@@ -16,6 +16,7 @@
"electron-squirrel-startup": "^1.0.0", "electron-squirrel-startup": "^1.0.0",
"mongodb": "^4.9.1", "mongodb": "^4.9.1",
"mongoose": "^6.5.4", "mongoose": "^6.5.4",
"node-schedule": "^2.1.0",
"node-wget": "^0.4.3", "node-wget": "^0.4.3",
"node-xlsx": "^0.21.0", "node-xlsx": "^0.21.0",
"playwright": "1.23.0", "playwright": "1.23.0",
+19 -3
View File
@@ -3,8 +3,11 @@ const {_android: android} = require("playwright");
const alert = require("alert"); const alert = require("alert");
const LinkValidator = require("./workers/LinkValidator"); const LinkValidator = require("./workers/LinkValidator");
const mongoManager = new MongoManager(); const mongoManager = new MongoManager();
const schedule = require('node-schedule');
let canContinue = true;
function validateLinks() {
mongoManager.connect().then(r => {
android.devices().then((devices) => { android.devices().then((devices) => {
if (devices.length === 0) { if (devices.length === 0) {
alert("未找到连接的设备"); alert("未找到连接的设备");
@@ -14,15 +17,18 @@ mongoManager.connect().then(r => {
let segmentNumber = list.length / devices.length; let segmentNumber = list.length / devices.length;
console.log("connected device number:" + devices.length) console.log("connected device number:" + devices.length)
console.log("segmentNumber:" + segmentNumber) console.log("segmentNumber:" + segmentNumber)
canContinue = false;
for (let i = 0; i < devices.length; i++) { for (let i = 0; i < devices.length; i++) {
startWithList(list.slice(i * segmentNumber, segmentNumber * (i + 1)), devices[i]).then(result => { startWithList(list.slice(i * segmentNumber, segmentNumber * (i + 1)), devices[i]).then(result => {
console.log("stop") console.log("stop")
canContinue = true;
}) })
} }
}) })
}) })
}); }
async function startWithList(listOfUrls, device) { async function startWithList(listOfUrls, device) {
await listOfUrls.reduce(async (promise, urlToValidate) => { await listOfUrls.reduce(async (promise, urlToValidate) => {
@@ -34,4 +40,14 @@ async function startWithList(listOfUrls, device) {
await commandor.loadPage() await commandor.loadPage()
console.log("next"); console.log("next");
}, Promise.resolve()); }, Promise.resolve());
} }
mongoManager.connect().then(r => {
const job = schedule.scheduleJob('*/1 * * * *', function () {
if (canContinue) {
validateLinks()
} else {
console.log("job is executing, ignored")
}
})
});
+40 -34
View File
@@ -30,6 +30,7 @@ REGEX_RDV_URL = "https:\/\/rendezvousparis\.hermes\.com\/client\/register\/[A-Z0
const PLAY_AUDIO_BTN_ID = ".audio-captcha-play-button" const PLAY_AUDIO_BTN_ID = ".audio-captcha-play-button"
const homedir = require('os').homedir(); const homedir = require('os').homedir();
const CAPTCHA_CONTAINER = "#captcha-container"; const CAPTCHA_CONTAINER = "#captcha-container";
function delay(delayInMs) { function delay(delayInMs) {
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(() => { setTimeout(() => {
@@ -62,44 +63,49 @@ class LinkValidator {
// Connect to the device. // Connect to the device.
log("loadPage() called for url " + this.urlToValidate); log("loadPage() called for url " + this.urlToValidate);
// 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();
// await context.clearCookies()
// Use BrowserContext as usual.
this.page = await context.newPage();
this.page.on("load", (loadedPage) => {
this.onPageLoad(loadedPage)
})
this.page.on("response", (response) => {
this.onResponse(response)
})
try { try {
await this.page.goto(this.urlToValidate, {timeout: 90 * 1000}); const context = await this.device.launchBrowser();
// await context.clearCookies()
// Use BrowserContext as usual.
this.page = await context.newPage();
this.page.on("load", (loadedPage) => {
this.onPageLoad(loadedPage)
})
this.page.on("response", (response) => {
this.onResponse(response)
})
try {
await this.page.goto(this.urlToValidate, {timeout: 90 * 1000});
} catch (e) {
log(e)
this.isTerminated = true
}
let cancel
const intervalTask = setInterval(() => {
if (this.isTerminated) {
log("request terminated, will close device")
context.close()
// this.page.close()
this.device.close()
clearInterval(intervalTask)
cancel()
return context
}
}, 10 * 1000)//interval of 10 seconds
await new Promise(function (fulfill, reject) {
cancel = function () {
fulfill(Promise.resolve())
}
setTimeout(fulfill, TIME_OUT, 5)
}).then(log)
} catch (e) { } catch (e) {
log(e) console.log(e)
this.isTerminated = true
} }
let cancel
const intervalTask = setInterval(() => {
if (this.isTerminated) {
log("request terminated, will close device")
context.close()
// this.page.close()
this.device.close()
clearInterval(intervalTask)
cancel()
return context
}
}, 10 * 1000)//interval of 10 seconds
await new Promise(function (fulfill, reject) {
cancel = function () {
fulfill(Promise.resolve())
}
setTimeout(fulfill, TIME_OUT, 5)
}).then(log)
} }
async onPageLoad(currentPage) { async onPageLoad(currentPage) {