2016-11-10 13:04:45 +01:00
|
|
|
'use strict'
|
2016-11-07 11:17:48 +01:00
|
|
|
|
2016-11-10 13:04:45 +01:00
|
|
|
let fetch = require('node-fetch');
|
|
|
|
|
let cheerio = require('cheerio');
|
|
|
|
|
let fs = require('fs');
|
2017-04-10 02:50:40 +02:00
|
|
|
let cloudinary = require('cloudinary');
|
2017-04-05 02:02:43 +02:00
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
AD_TYPE_SALE,
|
|
|
|
|
IGNORED_USERNAMES,
|
|
|
|
|
CATEGORY_FLAT,
|
|
|
|
|
CATEGORY_HOUSE,
|
|
|
|
|
CATEGORY_OFFICE,
|
|
|
|
|
CATEGORY_LAND
|
2017-10-31 20:20:09 +01:00
|
|
|
} from '../../common/enums';
|
2016-11-10 13:04:45 +01:00
|
|
|
|
|
|
|
|
export default class OlxCrawler {
|
|
|
|
|
|
|
|
|
|
constructor(fromPage = 0, toPage = 10, maxResults = 1000) {
|
|
|
|
|
this.fromPage = fromPage;
|
|
|
|
|
this.toPage = toPage;
|
|
|
|
|
this.maxResults = maxResults;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async indexSingle(url) {
|
2016-11-07 11:17:48 +01:00
|
|
|
try {
|
2016-11-10 13:04:45 +01:00
|
|
|
const res = await fetch(url);
|
|
|
|
|
const body = await res.text();
|
2016-11-07 11:17:48 +01:00
|
|
|
const $ = cheerio.load(body);
|
|
|
|
|
|
2017-04-04 02:16:22 +02:00
|
|
|
const username = $('#lg > div.desno2.profil > div:nth-child(2) > div.vrsta1.vrsta_desno > a > div.username > span').text();
|
|
|
|
|
|
|
|
|
|
if (IGNORED_USERNAMES.includes((username || '').toLowerCase())) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-07 11:17:48 +01:00
|
|
|
const title = $('#naslovartikla').text();
|
2017-04-05 02:02:43 +02:00
|
|
|
const category = $('#artikal_glavni_div > div.artikal_lijevo > div:nth-child(3) > div > span:nth-child(3) > a > span').text();
|
|
|
|
|
|
2016-11-07 11:17:48 +01:00
|
|
|
const price = $('#pc > p:nth-child(2)').text();
|
|
|
|
|
const size = $('#dodatnapolja1 > div:nth-child(1) > div.df2').text();
|
|
|
|
|
const rooms = $('#dodatnapolja1 > div:nth-child(2) > div.df2').text();
|
|
|
|
|
const address = $('#dodatnapolja1 > div:nth-child(5) > div.df2').text();
|
|
|
|
|
const location = $('#artikal_glavni_div > div.artikal_lijevo > div.op.pop.mobile-lokacija').attr('data-content');
|
|
|
|
|
|
|
|
|
|
const adType = $('#artikal_glavni_div > div.artikal_lijevo > div:nth-child(15) > div:nth-child(2) > div.df2').text();
|
|
|
|
|
const time = $('time').attr('datetime');
|
|
|
|
|
const olxId = $('#artikal_glavni_div > div.artikal_lijevo > div:nth-child(15) > div:nth-child(4) > div.df2').text();
|
|
|
|
|
|
|
|
|
|
const descriptions = $('.artikal_detaljniopis_tekst');
|
2017-04-04 02:16:22 +02:00
|
|
|
const floor = $('#dodatnapolja1').find(':contains(Sprat)').last().nextAll().text();
|
2016-11-07 11:17:48 +01:00
|
|
|
const latLngRe = /LatLng\(([0-9]+\.[0-9]+)\,\s+([0-9]+\.[0-9]+)\)/g;
|
|
|
|
|
const imgRe = /href":("[^"]*")/g;
|
|
|
|
|
const matches = latLngRe.exec(body);
|
|
|
|
|
let lng = '',
|
2016-11-10 13:04:45 +01:00
|
|
|
lat = '';
|
2016-11-07 11:17:48 +01:00
|
|
|
|
|
|
|
|
const images = [];
|
2016-11-10 13:04:45 +01:00
|
|
|
const imgMatches = body.match(imgRe);
|
2016-11-07 11:17:48 +01:00
|
|
|
|
2017-04-04 02:16:22 +02:00
|
|
|
const parseRooms = (rooms) => parseInt([...rooms].filter(c => !isNaN(c)).filter(c => c.trim()).join())
|
|
|
|
|
const parsePrice = (price) => parseFloat(price.replace(".", ""))
|
|
|
|
|
|
|
|
|
|
|
2016-11-07 11:17:48 +01:00
|
|
|
for (let i = 0; imgMatches && i < imgMatches.length; i++) {
|
2016-11-10 13:04:45 +01:00
|
|
|
let img = imgMatches[i].replace("href\":", "")
|
2016-11-07 11:17:48 +01:00
|
|
|
img = img.replace("\"", "");
|
|
|
|
|
img = img.replace("\"", "");
|
|
|
|
|
images.push(img);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-10 02:50:40 +02:00
|
|
|
const uploadPromises = images.map(img => {
|
|
|
|
|
const imgFixed = eval(`'${img}'`);
|
|
|
|
|
return cloudinary.uploader.upload(eval(`'${img}'`));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const uploadResults = await Promise.all(uploadPromises);
|
|
|
|
|
const cloudinaryImages = uploadResults.map(ur => ur.url);
|
|
|
|
|
|
2016-11-07 11:17:48 +01:00
|
|
|
if (matches && matches.length >= 3) {
|
|
|
|
|
lat = matches[1];
|
|
|
|
|
lng = matches[2];
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-05 00:14:30 +02:00
|
|
|
const parsedPrice = parsePrice(price);
|
2017-04-05 02:02:43 +02:00
|
|
|
let parsedRooms;
|
|
|
|
|
|
|
|
|
|
if (rooms === 'Garsonjera') {
|
|
|
|
|
parsedRooms = 0;
|
|
|
|
|
} else {
|
|
|
|
|
parsedRooms = parseRooms(rooms);
|
|
|
|
|
}
|
2017-04-05 00:14:30 +02:00
|
|
|
|
2016-11-07 11:17:48 +01:00
|
|
|
const data = {
|
2017-04-05 02:02:43 +02:00
|
|
|
category: this.getCategoryId(category),
|
2016-11-10 13:04:45 +01:00
|
|
|
url,
|
2016-11-07 11:17:48 +01:00
|
|
|
title,
|
2017-04-05 02:02:43 +02:00
|
|
|
price: isNaN(parsedPrice) ? price : parsedPrice,
|
2017-04-04 02:16:22 +02:00
|
|
|
size: parseFloat(size),
|
2017-04-05 02:02:43 +02:00
|
|
|
rooms: parsedRooms,
|
2017-04-04 02:16:22 +02:00
|
|
|
floor: parseInt(floor),
|
2016-11-07 11:17:48 +01:00
|
|
|
address,
|
|
|
|
|
location,
|
2017-04-04 02:16:22 +02:00
|
|
|
adType: AD_TYPE_SALE,
|
2016-11-07 11:17:48 +01:00
|
|
|
time,
|
|
|
|
|
shortDescription: descriptions.first().text(),
|
|
|
|
|
longDescription: descriptions.last().text(),
|
|
|
|
|
lat,
|
|
|
|
|
lng,
|
2017-03-31 14:31:56 +02:00
|
|
|
loc: [parseFloat(lat), parseFloat(lng)],
|
2017-04-10 02:50:40 +02:00
|
|
|
images: cloudinaryImages
|
2016-11-07 11:17:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
} catch (e) {
|
2017-04-10 02:50:40 +02:00
|
|
|
console.error('Exception caught: ' + e.message);
|
2016-11-07 11:17:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2016-11-10 13:04:45 +01:00
|
|
|
}
|
2016-11-07 11:17:48 +01:00
|
|
|
|
2016-11-10 13:04:45 +01:00
|
|
|
async indexPage(pageNr, maxResults = 1000) {
|
2016-11-07 11:17:48 +01:00
|
|
|
try {
|
|
|
|
|
console.log('Starting to index page: ' + pageNr);
|
2017-04-04 02:16:22 +02:00
|
|
|
const url = `http://www.olx.ba/pretraga?vrsta=samoprodaja&sort_order=desc&kategorija=23&sort_po=datum&kanton=9&stranica=${pageNr}`;
|
2016-11-07 11:17:48 +01:00
|
|
|
|
2017-03-31 14:31:56 +02:00
|
|
|
const res = await fetch(url);
|
|
|
|
|
const body = await res.text();
|
|
|
|
|
const $ = cheerio.load(body);
|
|
|
|
|
const hrefs = [];
|
2016-11-07 11:17:48 +01:00
|
|
|
const results = {};
|
|
|
|
|
|
2016-11-10 13:04:45 +01:00
|
|
|
$('#rezultatipretrage').find('.listitem').each((i, elem) => {
|
2016-11-07 11:17:48 +01:00
|
|
|
const href = $(elem).find('a').first().attr('href');
|
|
|
|
|
hrefs.push(href);
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-10 13:04:45 +01:00
|
|
|
let actualNoOfResults = (hrefs.length <= maxResults) ? hrefs.length : maxResults;
|
2016-11-07 11:17:48 +01:00
|
|
|
|
2017-04-02 03:31:39 +02:00
|
|
|
for (let i = 0; i < hrefs.length; i++) {
|
2016-11-10 13:04:45 +01:00
|
|
|
console.log(`indexing: ${hrefs[i]}`);
|
|
|
|
|
|
|
|
|
|
const singleData = await this.indexSingle(hrefs[i]);
|
2016-11-07 11:17:48 +01:00
|
|
|
|
|
|
|
|
if (singleData) {
|
|
|
|
|
results[hrefs[i]] = singleData;
|
|
|
|
|
}
|
2016-11-10 13:04:45 +01:00
|
|
|
await this.sleep(500);
|
2016-11-07 11:17:48 +01:00
|
|
|
}
|
|
|
|
|
|
2016-11-10 13:04:45 +01:00
|
|
|
return results;
|
2016-11-07 11:17:48 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Exception caught:' + e);
|
|
|
|
|
}
|
2016-11-10 13:04:45 +01:00
|
|
|
}
|
2016-11-07 11:17:48 +01:00
|
|
|
|
2017-04-05 02:02:43 +02:00
|
|
|
getCategoryId (category) {
|
|
|
|
|
if (category === 'Stanovi') {
|
|
|
|
|
return CATEGORY_FLAT;
|
|
|
|
|
} else if (category === 'Zemljišta') {
|
|
|
|
|
return CATEGORY_LAND;
|
|
|
|
|
} else if (category === 'Kuće') {
|
|
|
|
|
return CATEGORY_HOUSE;
|
|
|
|
|
} else if (category === 'Poslovni prostori') {
|
|
|
|
|
return CATEGORY_OFFICE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 13:04:45 +01:00
|
|
|
async sleep(ms) {
|
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
|
}
|
2016-11-07 11:17:48 +01:00
|
|
|
|
2016-11-10 13:04:45 +01:00
|
|
|
async indexPages(start, end, maxResults = 1000) {
|
|
|
|
|
let results = {};
|
2016-11-07 11:17:48 +01:00
|
|
|
for (let i = start; i <= end; i++) {
|
2016-11-10 14:03:58 +01:00
|
|
|
let result = await this.indexPage(i, maxResults);
|
2016-11-10 13:04:45 +01:00
|
|
|
Object.assign(results, result)
|
|
|
|
|
await this.sleep(5000);
|
2016-11-07 11:17:48 +01:00
|
|
|
}
|
2016-11-10 13:04:45 +01:00
|
|
|
return results;
|
|
|
|
|
}
|
2016-11-07 11:17:48 +01:00
|
|
|
|
2016-11-10 13:04:45 +01:00
|
|
|
async crawl() {
|
|
|
|
|
let results = await this.indexPages(this.fromPage, this.toPage, this.maxResults);
|
|
|
|
|
return results;
|
|
|
|
|
}
|
2016-11-07 11:17:48 +01:00
|
|
|
}
|
|
|
|
|
|