-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
91 lines (83 loc) · 2.44 KB
/
functions.js
File metadata and controls
91 lines (83 loc) · 2.44 KB
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
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const getPrices = (body, name) => {
const dom = new JSDOM(body);
dom.window.document.querySelectorAll("table.zebra tr table").forEach(table => table.deleteRow(0));
const trs = dom.window.document.querySelectorAll("table.zebra tr");
let parsePrice = false;
let wog = null, okko = null, shell = null, socar = null, glusco = null
for(const {cells} of trs) {
if (!parsePrice) {
if (cells.length === 1) {
if (cells[0].textContent.toLowerCase().includes(name)) {
parsePrice = true;
}
}
} else {
if (cells.length === 1) {
break;
}
if (cells.length !== 7) {
continue;
}
const brand = cells[0].textContent.toLowerCase();
const premium = parseFloat(cells[2].textContent.replace(',','.'))
const regular = parseFloat(cells[3].textContent.replace(',','.'))
switch(brand) {
case 'wog':
wog = {premium, regular};
break;
case 'shell':
shell = {premium};
break;
case 'окко':
okko = {premium, regular};
break;
case 'glusco':
glusco = {premium, regular};
break;
case 'socar':
socar = {premium, regular};
break;
}
}
}
return {wog, okko, shell, socar, glusco};
}
const distance = (lat1, lon1, lat2, lon2) => {
const p = 0.017453292519943295; // Math.PI / 180
const c = Math.cos;
const a = 0.5 - c((lat2 - lat1) * p)/2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p))/2;
return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
}
const mapDistance = (response, location) => {
return response.map(result => ({
...result,
distance: Math.round(distance(
location.lat,
location.lng,
result.geometry.location.lat,
result.geometry.location.lng,
) * 10) / 10,
})).sort((a, b) => a.distance - b.distance);
}
const chainLocations = (ctx, locations) => new Promise(resolve => {
if (locations.length) {
const {geometry: {location: {lat, lng}}} = locations.pop();
return ctx.replyWithLocation(lat, lng).then(() => chainLocations(ctx, locations).then(resolve));
} else {
return resolve();
}
})
const filterSameLocations = locations => {
const lastLatLng = {lat: 0, lng: 0};
return locations.filter(({geometry: {location: {lat, lng}}}) => {
const distances = distance(lastLatLng.lat, lastLatLng.lng, lat, lng);
lastLatLng.lat = lat;
lastLatLng.lng = lng;
return distances > 0.01;
})
}
module.exports = {getPrices, mapDistance, chainLocations, filterSameLocations};