WIP Changed login to crawler.

This commit is contained in:
Naida Vatric
2020-01-13 09:12:03 +01:00
parent 8505282670
commit e70901d369
3 changed files with 47 additions and 30 deletions

View File

@@ -3,6 +3,7 @@
const fetch = require("node-fetch");
const cheerio = require("cheerio");
const moment = require("moment-timezone");
const FormData = require("form-data");
const {
AD_TYPE,
@@ -586,44 +587,37 @@ class ProstorCrawler {
}
async loginForScraping(PROSTOR_LOGIN) {
console.log("PROSTOR_LOGIN", PROSTOR_LOGIN);
let logedin = false;
fetch("https://prostor.ba/moj-prostor/prijava", {
var formData = new FormData();
formData.append("email", PROSTOR_LOGIN.EMAIL);
formData.append("password", PROSTOR_LOGIN.PASSWORD);
//When once loged in it stays loged in with same credentials.
//Do we need to log out ??
return fetch("https://prostor.ba/moj-prostor/prijava", {
method: "POST",
body: JSON.stringify({
email: PROSTOR_LOGIN.EMAIL,
password: PROSTOR_LOGIN.PASSWORD
})
body: formData,
headers: { Cookie: "ci_session=3a47b6e18b3b9bc146bcde1f95126cbad0f58bf7" }
})
.then(page => {
/* console.log("page", page.text());
const $ = cheerio.load(page);
console.log("$ ", $);
if (
$(".icons .d-none.d-xl-inline-block.mr-2")
.text()
.indexOf("Dobrodošli") != -1
) {
console.log("[PROSTOR]: Crawler loged in!");
logedin = true;
} else {
console.log("[PROSTOR]: Crawler login failed - wrong credentials!");
} */
return page.text();
})
.then(resp => {
// console.log(resp);
const $ = cheerio.load(resp);
console.log("$ ", $("h1").text());
if (
$("h1")
.text()
.indexOf("Dobrodošli") !== -1
) {
console.log("[PROSTOR]: Crawler loged in!");
return true;
} else {
console.log("[PROSTOR]: Crawler login failed - wrong credentials!");
return false;
}
})
.catch(err => {
console.log("[PROSTOR]: Crawler login error ", err);
});
//
console.log("login in function:", logedin);
return logedin;
}
}