-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbahn.js
More file actions
66 lines (54 loc) · 1.74 KB
/
bahn.js
File metadata and controls
66 lines (54 loc) · 1.74 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
const cheerio = require('cheerio');
const URI = require('urijs');
const request = require('request-promise');
if (process.argv.length < 4) {
usage()
}
(async () => {
const from = process.argv[2], to = process.argv[3]
const url = URI('https://reiseauskunft.bahn.de/bin/query.exe/dn')
.query({
'S': from,
'Z': to,
'start': '1'
}).toString()
const $ = await request(url, {
transform: body => cheerio.load(body)
})
let firstrows = []
$('.result .firstrow').each((i, el) => {
parse = attr => parseQuery($, el, attr)
firstrows.push({
station: {
departure: parse('td.station.first')
},
time: {
departure: parse('td.time').substr(0,5)
},
live: {
departure: parse('span.delayOnTime').substr(0,5)
},
duration: parse('td.duration'),
changes: parse('td.changes'),
products: parse('td.products'),
})
})
$('.result .last').each((i, el) => {
parse = attr => parseQuery($, el, attr)
firstrows[i].station.destination = parse('td.station.stationDest')
firstrows[i].time.arrival = parse('td.time').substr(0,5)
firstrows[i].live.arrival = parse('span.delayOnTime').substr(0,5)
})
$('.result .buttonLine').each((i, el) => {
firstrows[i].details = $('.open',el).attr('href')
firstrows[i].book = $('.buttonbold',el).attr('href')
})
console.log(firstrows)
})()
function parseQuery($, el, attr) {
return $(attr, el).text().replace(/\n/g, '').trim()
}
function usage() {
console.log(`usage: ${process.argv[1]} [from] [to]`)
process.exit(2)
}