-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.js
124 lines (106 loc) · 3.88 KB
/
controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const Feed = require('feed').Feed;
const e = require('express');
const scrapePazar3 = require('./scrapers/pazar3.js');
const scrapeReklama5 = require('./scrapers/reklama5.js');
var moment = require('moment'); // require
moment().format();
/**
* Get scraps from Pazar3 and Reklama5 based on parameters provided
*
* @param type - Determine the type of listings to get
* @param query - Optional search query
* @param city - Cities array
* @param price - Price range
* @param rss - Return RSS feed data if true
* @return {returnData}
*/
async function getScraps(query = '', city = 'ohrid,struga,tetovo,skopje', price = '10000,30000', sortby = 'date', type = 'apartments', rss = false) {
scrapPromises = [];
returnData = [];
let cities = [];
if(city) {
cities = city.split(',');
} else {
return 0;
}
let priceRange = [];
if(price) {
prices = price.split(',');
priceRange = {from: prices[0], to: prices[1]};
} else {
priceRange = {from: '', to: ''};
}
// Get the scrap promises for each city
cities.forEach(function(city) {
// Scrape Pazar3
scrapPromises.push(scrapePazar3(type, query, city, priceRange)
.then(function data(e) {
return e;
})
);
// Scrape Reklmata5
scrapPromises.push(scrapeReklama5(type, query, city, priceRange)
.then(function data(e) {
return e;
})
);
});
// Run the promises
promisesData = await Promise.all(scrapPromises).catch(error => console.log(`Error: ${error}`));
// Fill up the array with data
promisesData.forEach(function(oglasi) {
oglasi.forEach(function(oglas) {
moment.locale('mk');
// Get date from scraps
if(oglas.date.startsWith("Денес")) {
let time = moment(oglas.date, 'H:mm');
let momentDate = moment(time);
oglas.date = momentDate;
} else if(oglas.date.startsWith("Вчера")) {
let time = moment(oglas.date, 'H:mm');
let momentDate = moment(time).add(-1, 'days');
oglas.date = momentDate;
} else {
let momentDate = moment(oglas.date, 'DD MMMM HH:mm');
let razlika = momentDate.diff(moment(), 'months', true);
if(razlika) {
momentDate.set('year', 2020);
}
oglas.date = momentDate;
}
returnData.push(oglas);
});
});
// Sort the array using sortby parameter (date or price)
returnData.sort((a, b) => b[sortby] - a[sortby]);
// If RSS data is needed
if(rss) {
const feed = new Feed({
title: "Bojans AH Feed",
description: "Searching for real estate via Reklama5 & Pazar3",
link: "https://bojanstojanovski.com/playground/ah-feed/",
image: "https://bojanstojanovski.com/dist/img/portret.jpg",
favicon: "https://bojanstojanovski.com/favicon.ico",
copyright: "Content belongs to Reklama5 & Pazar3",
author: {
name: "Bojan Stojanovski",
email: "[email protected]",
link: "https://bojanstojanovski.com/blog/projects/apartment-hunting-rss-feed/"
}
});
returnData.forEach(function(oglas) {
let titleFormatted = (oglas.title + " - " + oglas.price + "€");
let date = oglas.date;
feed.addItem({
title: titleFormatted,
id: oglas.url,
link: oglas.url,
content: date.format('llll') + ' - ' + oglas.price.toString(),
pubDate: date.format('YYYY-MM-DD H:mm:ss')
});
});
return feed;
}
return returnData;
}
module.exports = getScraps;