2019-06-13 15:49:31 +02:00
|
|
|
|
2019-06-21 16:48:19 +02:00
|
|
|
const Promise = require("bluebird");
|
2019-06-19 17:12:22 +02:00
|
|
|
const OlxCrawler = require("../helpers/crawlers/olxClawler");
|
2019-06-20 14:51:14 +02:00
|
|
|
const db = require("../models/index");
|
2019-06-25 17:06:07 +02:00
|
|
|
const { allMarketAlerts } = require('../helpers/db/dbHelper');
|
2019-06-13 15:49:31 +02:00
|
|
|
|
2019-06-21 15:14:43 +02:00
|
|
|
const olxCrawler = new OlxCrawler(1, 2, 3);
|
|
|
|
|
|
2019-06-13 15:49:31 +02:00
|
|
|
const crawlers = [
|
2019-06-21 16:48:19 +02:00
|
|
|
olxCrawler,
|
2019-06-20 21:27:51 +02:00
|
|
|
];
|
2019-06-13 15:49:31 +02:00
|
|
|
|
2019-06-20 21:27:51 +02:00
|
|
|
async function crawlAll() {
|
2019-06-25 17:06:07 +02:00
|
|
|
console.log("CRAWLER SERVICE: crawlAll");
|
2019-06-20 21:27:51 +02:00
|
|
|
|
2019-07-10 15:21:46 +02:00
|
|
|
try {
|
|
|
|
|
const marketAlertsFromDb = await allMarketAlerts(true);
|
|
|
|
|
const hrefs = [];
|
|
|
|
|
|
|
|
|
|
marketAlertsFromDb.map(marketAlert => {
|
|
|
|
|
if (hrefs[marketAlert.request] === undefined) {
|
|
|
|
|
hrefs[marketAlert.request] = []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hrefs[marketAlert.request].push(marketAlert.url);
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
global.hrefs = hrefs;
|
|
|
|
|
console.log("CRAWLER SERVICE: GLOBAL HREFS");
|
|
|
|
|
console.log(global.hrefs);
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("CRAWLER SERVICE:could not fetch marketalerts ", e);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 16:33:00 +02:00
|
|
|
return Promise.map(crawlers, function (crawler) {
|
2019-06-21 16:48:19 +02:00
|
|
|
return crawler.crawl();
|
|
|
|
|
}).then(async (results) => {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
2019-07-10 15:21:46 +02:00
|
|
|
const marketAlertsFromDb = await allMarketAlerts(false, true);
|
2019-07-10 12:27:30 +02:00
|
|
|
|
2019-07-10 15:21:46 +02:00
|
|
|
console.log("CRAWLER SERVICE: number of existing MarketAlerts from db: " + marketAlertsFromDb.length);
|
2019-06-21 16:48:19 +02:00
|
|
|
|
|
|
|
|
const marketAlerts = [];
|
|
|
|
|
const mergedResults = [].concat.apply([], results);
|
|
|
|
|
|
|
|
|
|
for (const result of mergedResults) {
|
|
|
|
|
marketAlerts.push({
|
|
|
|
|
url: result.url,
|
|
|
|
|
realestateOrigin: "OLX",
|
2019-06-24 11:49:13 +02:00
|
|
|
originId: 1,
|
2019-06-21 16:48:19 +02:00
|
|
|
size: result.size,
|
|
|
|
|
price: result.price,
|
|
|
|
|
email: result.email,
|
2019-07-02 21:49:56 +02:00
|
|
|
request: result.uuid,
|
2019-06-21 16:48:19 +02:00
|
|
|
// lastDate: DataTypes.STRING,
|
|
|
|
|
municipality: result.municipality,
|
|
|
|
|
region: result.region,
|
2019-06-24 11:49:13 +02:00
|
|
|
gardenSize: isNaN(result.gardenSize) ? 0 : result.gardenSize,
|
2019-06-28 18:06:19 +02:00
|
|
|
realEstateType: result.realEstateType,
|
|
|
|
|
title: result.title,
|
2019-07-10 15:21:46 +02:00
|
|
|
notified: false,
|
|
|
|
|
hasLocation: result.hasLocation
|
2019-06-21 16:48:19 +02:00
|
|
|
})
|
|
|
|
|
}
|
2019-06-25 17:06:07 +02:00
|
|
|
console.log("CRAWLER SERVICE: Number of crawler results: " + marketAlerts.length);
|
|
|
|
|
|
2019-06-21 16:48:19 +02:00
|
|
|
try {
|
2019-06-25 17:06:07 +02:00
|
|
|
|
2019-07-10 15:21:46 +02:00
|
|
|
const filteredMarketAlerts = marketAlerts.filter((elem) => !marketAlertsFromDb.find(({ url }) => elem.url === url));
|
2019-06-25 17:06:07 +02:00
|
|
|
console.log("CRAWLER SERVICE: Number of new crawler results: " + filteredMarketAlerts.length);
|
|
|
|
|
|
2019-06-21 16:48:19 +02:00
|
|
|
await db.MarketAlert.bulkCreate(filteredMarketAlerts);
|
2019-06-25 17:06:07 +02:00
|
|
|
|
2019-06-21 16:48:19 +02:00
|
|
|
} catch (e) {
|
2019-06-25 17:06:07 +02:00
|
|
|
console.log("CRAWLER SERVICE: Could not bulkCreate marketalers reason: ", e);
|
2019-06-21 16:48:19 +02:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
2019-06-25 17:06:07 +02:00
|
|
|
console.log("CRAWLER SERVICE: Error crawling. Trying next crawler! ", e);
|
2019-06-21 16:48:19 +02:00
|
|
|
}
|
2019-06-21 15:14:43 +02:00
|
|
|
})
|
2019-06-21 16:48:19 +02:00
|
|
|
};
|
2019-07-09 16:33:00 +02:00
|
|
|
module.exports = crawlAll;
|
|
|
|
|
// crawlAll();
|