-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode-idos.js
123 lines (108 loc) · 3.11 KB
/
node-idos.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
/* Magic Mirror Module
* Module: MMM-idos
* Description: Display estimations for public transport stops in the Czech Republic
*
* By elrubio https://github.com/soyrubio
* MIT Licensed.
*
* This is a web scraper for the MMM-idos module
* The data is scraped from https://idos.idnes.cz/vlakyautobusymhdvse/odjezdy/
*/
const puppeteer = require('puppeteer');
const cheerio = require('cheerio');
function parse_body(body) {
const $ = cheerio.load(body)
var stops = []
$(".dep-row-first").each(function () {
var stop = $(this).find("h3").map(function () {
return $(this).text().trim()
}).toArray();
stops.push(stop);
});
var vehicles = $(".departures-table__cell img").map(function () {
return $(this).attr("alt")
});
var delays = $(".cell-delay").map(function () {
return $(this).find(".delay-bubble").text().replace(/[^0-9]/g, "").replace(/^$/, "0")
});
var output = [];
for (let i = 0; i < stops.length; i++) {
stop = stops[i];
output.push({
"destination": stop[0],
"number": stop[1].replace("Bus", "").replace("Tram", "").trim(),
"departure": stop[2].replace(/\n.*/g, ""),
"delay": delays[i],
"departurewdelay": getDepartureWDelay(stop[2], delays[i]),
"type": vehicles[i].replace("tramvaj", "train").replace("autobus", "bus").replace("trolejbus", "bus")
});
}
output.sort(getSortOrder("departurewdelay"));
return output;
}
function getSortOrder(prop) {
return function (a, b) {
if (a[prop] > b[prop]) {
return 1;
} else if (a[prop] < b[prop]) {
return -1;
}
return 0;
}
}
function getDepartureWDelay(dep, delay) {
var now = new Date();
var time = dep.split(":");
var departure = new Date(now.getFullYear(), now.getMonth(), now.getDate(), time[0], time[1], 0);
var diff = (departure - now) / 60000;
departure.setMinutes(departure.getMinutes() + parseInt(delay));
diff = (departure - now) / 60000;
diff = Math.round(diff);
return diff;
}
async function scrape(options, callback) {
try {
var port_arg;
if (options.ports.length > 0) {
const randomPort = options.ports[Math.floor(Math.random() * options.ports.length)];
port_arg = '--proxy-server=socks5://127.0.0.1:' + randomPort;
}
const browser = await puppeteer.launch({
args: [port_arg],
executablePath: '/usr/bin/chromium-browser'
});
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', (request) => {
if (['image', 'stylesheet', 'font', 'script'].indexOf(request.resourceType()) !== -1) {
request.abort();
} else {
request.continue();
}
});
await page.goto(options.url);
const content = await page.content();
setTimeout(() => {
browser.close();
}, 3000);
callback(null, content);
} catch (err) {
callback(err, null);
}
}
exports.get_livetable = function (stop, ports) {
var url = 'https://idos.idnes.cz/vlakyautobusymhdvse/odjezdy/vysledky/?f=' + stop;
//var stops = scrape(url, props.ports);
return new Promise(function (resolve, reject) {
scrape({
url: url,
ports: ports
}, function (err, body) {
if (err)
return reject(err);
else
return resolve(parse_body(body));
})
});
return stops;
}