123 lines
5.0 KiB
JavaScript
123 lines
5.0 KiB
JavaScript
const {v4: uuidv4} = require("uuid");
|
|
const tesseract = require("node-tesseract-ocr");
|
|
const OCRResult = require("../models/OCRResult");
|
|
const Jimp = require('jimp');
|
|
|
|
function delay(delayInMs) {
|
|
return new Promise(resolve => {
|
|
setTimeout(() => {
|
|
resolve(2);
|
|
}, delayInMs);
|
|
});
|
|
}
|
|
|
|
const config = {
|
|
lang: "eng",
|
|
oem: 1,
|
|
psm: 3,
|
|
}
|
|
|
|
const MESSAGE_URL_VALIDATION_FR = "envoyé un lien par e-mail."
|
|
const MESSAGE_URL_VALIDATION_FR_2 = "un lien par e-mail"
|
|
const MESSAGE_URL_VALIDATION_EN = "Please click on the link we sent by email"
|
|
const CAPTCHA_ERROR_MESSAGE = "Error verifying captcha, please try again"
|
|
const CAPTCHA_ERROR_MESSAGE_FR = "La vérification du captcha a échoué"
|
|
const BLOCKED_MSG_EN = "have been blocked"
|
|
const BLOCKED_MSG_FR = "avez été bloqué"
|
|
const CHECKING_MSG_FR = "Verifying"
|
|
const ERR_CACHE_MISS = "ERR_CACHE_MISS"
|
|
const SLIDING_CAPTCHA_FR = "Pourquoi cette vérification"
|
|
const MESSAGE_FILL_FIELD_FR = "Demande de rendez-vous pour"
|
|
const WELCOME_MESSAGE_FR = "Bienvenue dans Chrome"
|
|
const PAGE_OPTIMIZATION_CHROME_FR = "Vous pouvez changer d'avis a tout moment dans"
|
|
const PAGE_OPTIMIZATION_CHROME_FR_2 = "Vous pouvez modifier vos options a tout moment"
|
|
const PAGE_OPTIMIZATION_CHROME_FR_3 = "Vous pouvez effectuer des modifications"
|
|
const PAGE_OPTIMIZATION_CHROME_FR_4 = "de nouvelles fonctionnalités"
|
|
const PAGE_OPTIMIZATION_CHROME_FR_5 = "Avec la mesure des performance"
|
|
const ONLINE_APPOINTMENT = "Online Appointment"
|
|
const CONFIRM_RESEND_FORM_FR = "Confirmer le nouvel envoi"
|
|
const CLOSED_MESSAGE_FR = "Depuis plus de 130 ans"
|
|
const DIALOG_TO_SKIP = "facilement les commandes"
|
|
const ABOUT_BLANK = "about:blank"
|
|
const GOOGLE_DISCONNECT_FR = "Rester déconnecté"
|
|
const RECAPTCHA_FAILED_FR = "captcha a échoué"
|
|
|
|
async function convertImageToWhiteBlack(image_path) {
|
|
const image = await Jimp.read(image_path);
|
|
image.grayscale().write(image_path + ".wb");
|
|
return image_path + ".wb";
|
|
}
|
|
|
|
class OCRChecker {
|
|
|
|
constructor(device, contact) {
|
|
this.device = device;
|
|
this.contact = contact;
|
|
}
|
|
|
|
get_file_name() {
|
|
let uuid = uuidv4();
|
|
return this.contact.passportNumber + "_" + uuid + ".png"
|
|
}
|
|
|
|
|
|
async get_result() {
|
|
let fileName = await this.take_screen_shot()
|
|
let screenShot = await convertImageToWhiteBlack(fileName);
|
|
try {
|
|
let result = await tesseract
|
|
.recognize(screenShot, config)
|
|
console.log(result)
|
|
if (result.includes(MESSAGE_URL_VALIDATION_EN) || result.includes(MESSAGE_URL_VALIDATION_FR) || result.includes(MESSAGE_URL_VALIDATION_FR_2)) {
|
|
return OCRResult.SUCCESS
|
|
} else if (result.includes(CAPTCHA_ERROR_MESSAGE) || result.includes(CAPTCHA_ERROR_MESSAGE_FR)) {
|
|
return OCRResult.RECAPTCHA_ERROR
|
|
} else if (result.includes(BLOCKED_MSG_EN) || result.includes(BLOCKED_MSG_FR)) {
|
|
return OCRResult.BLOCKED
|
|
} else if (result.includes(ERR_CACHE_MISS)) {
|
|
return OCRResult.TO_REFRESH
|
|
} else if (result.includes(CHECKING_MSG_FR)) {
|
|
return OCRResult.RECHECK
|
|
} else if (result.includes(SLIDING_CAPTCHA_FR)) {
|
|
return OCRResult.SLIDING_CAPTCHA
|
|
} else if (result.includes(MESSAGE_FILL_FIELD_FR)) {
|
|
return OCRResult.FILL_FIELD
|
|
} else if (result.includes(WELCOME_MESSAGE_FR)) {
|
|
return OCRResult.NEED_TO_CLICK_LATE_BTN
|
|
} else if (result.toLowerCase().includes(ONLINE_APPOINTMENT.toLowerCase())) {
|
|
return OCRResult.ONLINE_APPOINTMENT
|
|
} else if (result.includes(PAGE_OPTIMIZATION_CHROME_FR) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_2) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_3) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_4) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_5)) {
|
|
return OCRResult.PAGE_OPTIMIZATION
|
|
} else if (result.includes(CONFIRM_RESEND_FORM_FR)) {
|
|
return OCRResult.CONFIRM_RESEND_FORM
|
|
} else if (result.includes(CLOSED_MESSAGE_FR)) {
|
|
return OCRResult.CLOSED
|
|
} else if (result.includes(ABOUT_BLANK)) {
|
|
return OCRResult.TERMINAED
|
|
} else if (result.includes(DIALOG_TO_SKIP)) {
|
|
return OCRResult.TO_SKIP
|
|
} else if (result.includes(GOOGLE_DISCONNECT_FR)) {
|
|
return OCRResult.GOOGLE_DISCONNECT
|
|
} else if (result.includes(RECAPTCHA_FAILED_FR)) {
|
|
return OCRResult.TERMINAED
|
|
} else {
|
|
return OCRResult.TERMINAED
|
|
}
|
|
} catch (e) {
|
|
console.log(e)
|
|
return OCRResult.RECHECK
|
|
}
|
|
|
|
}
|
|
|
|
async take_screen_shot() {
|
|
let name = this.get_file_name()
|
|
await this.device.screenshot({path: name});
|
|
await delay(1000);
|
|
return name
|
|
// console.log(`stdout: ${stdout1}`);
|
|
}
|
|
}
|
|
|
|
module.exports = OCRChecker
|