Files
old-kivi/crawler/specific/prostor.js

263 lines
7.3 KiB
JavaScript
Raw Normal View History

2017-10-31 20:20:09 +01:00
'use strict';
2017-04-10 05:28:37 +02:00
2017-10-31 20:20:09 +01:00
let fetch = require ('node-fetch');
let cheerio = require ('cheerio');
let fs = require ('fs');
let cloudinary = require ('cloudinary');
let FormData = require ('form-data');
2017-04-10 05:28:37 +02:00
import {
AD_TYPE_SALE,
IGNORED_USERNAMES,
CATEGORY_FLAT,
CATEGORY_HOUSE,
CATEGORY_OFFICE,
2017-10-30 22:54:56 +01:00
CATEGORY_LAND,
STATUS_NORMAL,
STATUS_RESERVED,
2017-10-31 20:20:09 +01:00
STATUS_SOLD,
} from '../../common/enums';
2017-04-10 05:28:37 +02:00
export default class ProstorCrawler {
2017-10-31 20:20:09 +01:00
constructor (fromPage = 0, toPage = 10, maxResults = 1000) {
2017-04-10 05:28:37 +02:00
this.fromPage = fromPage;
this.toPage = toPage;
this.maxResults = maxResults;
}
2017-10-31 20:20:09 +01:00
async indexSingle (url) {
2017-04-10 05:28:37 +02:00
try {
2017-10-31 20:20:09 +01:00
const res = await fetch (url);
const body = await res.text ();
const $ = cheerio.load (body);
2017-04-10 05:28:37 +02:00
2017-10-31 20:20:09 +01:00
const title = $ (
'#nav_center_sub > div.content_area_1_left > div:nth-child(1) > h1'
).text ();
2017-04-10 05:28:37 +02:00
2017-10-31 20:20:09 +01:00
const category = $ (
'#nav_center_sub > div.content_area_1_left > div.bottom10 > div.content_lr_in_show > div:nth-child(3) > div:nth-child(4) > div.size_rs > span'
).text ();
2017-04-10 05:28:37 +02:00
2017-10-31 20:20:09 +01:00
const price = $ (
'#nav_center_sub > div.content_area_1_left > div.bottom10 > div.content_lr_in_show > div:nth-child(1) > div.size_rs > strong'
).text ();
2017-04-10 05:28:37 +02:00
2017-10-31 20:20:09 +01:00
const size = $ (
'#nav_center_sub > div.content_area_1_left > div.bottom10 > div.content_lr_in_show > div:nth-child(4) > div:nth-child(7) > div.size_rs > span'
).text ();
const rooms = $ (
'#nav_center_sub > div.content_area_1_left > div.bottom10 > div.content_lr_in_show > div:nth-child(4) > div:nth-child(2) > div.size_rs > span'
).text ();
2017-04-10 05:28:37 +02:00
2017-10-31 20:20:09 +01:00
const address = $ (
'#nav_center_sub > div.content_area_1_left > div.bottom10 > div.content_lr_in_show > div:nth-child(3) > div:nth-child(3) > div.size_rs > span'
).text ();
2017-04-10 05:28:37 +02:00
//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();
2017-10-31 20:20:09 +01:00
const time = $ (
'#nav_center_sub > div.content_area_1_right > div.bottom_d > div > strong:nth-child(1)'
).text ();
2017-04-10 05:28:37 +02:00
//const olxId = $('#artikal_glavni_div > div.artikal_lijevo > div:nth-child(15) > div:nth-child(4) > div.df2').text();
2017-10-31 20:20:09 +01:00
const descriptions = $ (
'#nav_center_sub > div.content_area_1_left > div.bottom10 > div.content_ll_in_show > div:nth-child(1)'
).text ();
2017-04-10 05:28:37 +02:00
2017-10-31 20:20:09 +01:00
const floor = $ (
'#nav_center_sub > div.content_area_1_left > div.bottom10 > div.content_lr_in_show > div:nth-child(4) > div:nth-child(6) > div.size_rs'
).text ();
2017-04-10 05:28:37 +02:00
const latLngRe = /marker=([0-9]+\.[0-9]+)\,\s*([0-9]+\.[0-9]+)/g;
2017-10-31 20:20:09 +01:00
var hasMap = false;
var tmpTitle = title.toUpperCase ();
2017-10-30 22:54:56 +01:00
var status = STATUS_NORMAL;
2017-10-31 20:20:09 +01:00
if (tmpTitle.indexOf ('PRODANO') !== -1) status = STATUS_SOLD;
if (tmpTitle.indexOf ('REZERVISANO') !== -1) status = STATUS_RESERVED;
2017-10-30 22:54:56 +01:00
2017-04-10 05:28:37 +02:00
//const latLngRe = /LatLng\(([0-9]+\.[0-9]+)\,\s+([0-9]+\.[0-9]+)\)/g;
2017-10-31 20:20:09 +01:00
const matches = latLngRe.exec (body);
let lng = '', lat = '';
hasMap = false;
2017-04-10 05:28:37 +02:00
if (matches && matches.length >= 3) {
lat = matches[1];
lng = matches[2];
2017-10-31 20:20:09 +01:00
hasMap = true;
2017-04-10 05:28:37 +02:00
}
//console.log({
2017-10-31 20:20:09 +01:00
//lat,
//lng,
//floor,
//descriptions,
//time,
//price,
//size,
//category,
//title
2017-04-10 05:28:37 +02:00
//});
//const imgRe = /href":("[^"]*")/g;
const images = [];
//const imgMatches = body.match(imgRe);
2017-10-31 20:20:09 +01:00
const parseRooms = rooms =>
parseInt (
[...rooms].filter (c => !isNaN (c)).filter (c => c.trim ()).join ()
);
const parsePrice = price => parseFloat (price.replace ('.', ''));
2017-04-10 05:28:37 +02:00
2017-10-31 20:20:09 +01:00
$ ('.fancybox').each ((i, elem) => {
const img = $ (elem).attr ('href');
images.push (img);
2017-04-10 05:28:37 +02:00
});
//for (let i = 0; imgMatches && i < imgMatches.length; i++) {
2017-10-31 20:20:09 +01:00
//let img = imgMatches[i].replace("href\":", "")
//img = img.replace("\"", "");
//img = img.replace("\"", "");
//images.push(img);
2017-04-10 05:28:37 +02:00
//}
2017-04-12 15:33:30 +02:00
//const uploadPromises = images.map(img => {
2017-10-31 20:20:09 +01:00
//return cloudinary.uploader.upload(img);
2017-04-12 15:33:30 +02:00
//});
2017-04-10 05:28:37 +02:00
2017-04-12 15:33:30 +02:00
//const uploadResults = await Promise.all(uploadPromises);
//const cloudinaryImages = uploadResults.map(ur => ur.url);
2017-04-10 05:28:37 +02:00
2017-10-31 20:20:09 +01:00
const parsedPrice = parsePrice (price);
2017-04-10 05:28:37 +02:00
let parsedRooms;
if (rooms === 'Garsonjera') {
parsedRooms = 0;
} else {
2017-10-31 20:20:09 +01:00
parsedRooms = parseRooms (rooms);
2017-04-10 05:28:37 +02:00
}
const data = {
2017-10-31 20:20:09 +01:00
category: this.getCategoryId (category),
2017-04-10 05:28:37 +02:00
url,
title,
2017-10-31 20:20:09 +01:00
price: isNaN (parsedPrice) ? price : parsedPrice,
size: parseFloat (size),
2017-04-10 05:28:37 +02:00
rooms: parsedRooms,
2017-10-31 20:20:09 +01:00
floor: parseInt (floor),
2017-04-10 05:28:37 +02:00
address,
adType: AD_TYPE_SALE,
time,
shortDescription: title,
longDescription: descriptions,
lat,
lng,
2017-10-31 20:20:09 +01:00
loc: [parseFloat (lat), parseFloat (lng)],
hasMap,
2017-10-30 22:54:56 +01:00
status,
2017-04-12 15:33:30 +02:00
//images: cloudinaryImages
2017-10-31 20:20:09 +01:00
images,
2017-04-10 05:28:37 +02:00
};
2017-10-31 20:20:09 +01:00
console.log (data);
2017-04-10 05:28:37 +02:00
return data;
} catch (e) {
2017-10-31 20:20:09 +01:00
console.error ('Exception caught: ' + e.message);
2017-04-10 05:28:37 +02:00
}
return null;
}
2017-10-31 20:20:09 +01:00
async indexPage (pageNr, maxResults = 1000) {
2017-04-10 05:28:37 +02:00
try {
2017-10-31 20:20:09 +01:00
console.log ('Starting to index page: ' + pageNr);
2017-04-10 05:28:37 +02:00
const url = `http://prostor.ba/index.php`;
2017-10-31 20:20:09 +01:00
const data = new FormData ();
data.append ('sortCombo', 'e.date_create DESC');
data.append ('command', '');
data.append ('action', 'show');
data.append ('page', pageNr);
data.append ('param', 'ponuda.inc.php');
data.append ('checkNO', 0);
data.append ('order', 'e.date_create DESC');
data.append ('reset', 0);
data.append ('estate_action', 1);
data.append ('Itemid', 785);
const res = await fetch (url, {
2017-04-10 05:28:37 +02:00
method: 'POST',
2017-10-31 20:20:09 +01:00
body: data,
2017-04-10 05:28:37 +02:00
});
2017-10-31 20:20:09 +01:00
const body = await res.text ();
const $ = cheerio.load (body);
2017-04-10 05:28:37 +02:00
const hrefs = [];
2017-10-31 20:20:09 +01:00
$ ('.nekret_box').each ((i, elem) => {
const href = $ (elem).find ('a').first ().attr ('href');
hrefs.push (`http://prostor.ba/${href}`);
2017-04-10 05:28:37 +02:00
});
const results = {};
for (const href of hrefs) {
2017-10-31 20:20:09 +01:00
console.log (`indexing: ${href}`);
2017-04-10 05:28:37 +02:00
2017-10-31 20:20:09 +01:00
const singleData = await this.indexSingle (href);
2017-04-10 05:28:37 +02:00
if (singleData) {
results[href] = singleData;
}
2017-10-31 20:20:09 +01:00
await this.sleep (500);
2017-04-10 05:28:37 +02:00
}
return results;
} catch (e) {
2017-10-31 20:20:09 +01:00
console.error ('Exception caught:' + e);
2017-04-10 05:28:37 +02:00
}
}
getCategoryId (category) {
if (category === 'Stan') {
return CATEGORY_FLAT;
} else if (category === 'Zemljište') {
return CATEGORY_LAND;
} else if (category === 'Kuća') {
return CATEGORY_HOUSE;
} else if (category === 'Poslovni prostor') {
return CATEGORY_OFFICE;
}
}
2017-10-31 20:20:09 +01:00
async sleep (ms) {
return new Promise (resolve => setTimeout (resolve, ms));
2017-04-10 05:28:37 +02:00
}
2017-10-31 20:20:09 +01:00
async indexPages (start, end, maxResults = 1000) {
2017-04-10 05:28:37 +02:00
let results = {};
for (let i = start; i <= end; i++) {
2017-10-31 20:20:09 +01:00
let result = await this.indexPage (i, maxResults);
Object.assign (results, result);
await this.sleep (5000);
2017-04-10 05:28:37 +02:00
}
return results;
}
2017-10-31 20:20:09 +01:00
async crawl () {
let results = await this.indexPages (
this.fromPage,
this.toPage,
this.maxResults
);
2017-04-10 05:28:37 +02:00
return results;
}
}