This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
forked from public-transport/hafas-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (91 loc) · 2.43 KB
/
index.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
'use strict'
const _parseLocation = require('../../parse/location')
const _createParseJourney = require('../../parse/journey')
const _createParseMovement = require('../../parse/movement')
const products = require('./products')
// todo: journey prices
const transformReqBody = (body) => {
body.client = {
id: 'NAHSH',
name: 'NAHSHPROD',
v: '3000700'
}
body.ver = '1.16'
body.auth = {aid: 'r0Ot9FLFNAFxijLW'}
body.lang = 'de'
return body
}
const parseLocation = (profile, opt, data, l) => {
const res = _parseLocation(profile, opt, data, l)
// weird fix for empty lines, e.g. IC/EC at Flensburg Hbf
if (res.lines) {
res.lines = res.lines.filter(x => x.id && x.name)
}
// remove leading zeroes, todo
if (res.id && res.id.length > 0) {
res.id = res.id.replace(/^0+/, '')
}
return res
}
const createParseJourney = (profile, opt, data) => {
const parseJourney = _createParseJourney(profile, opt, data)
const parseJourneyWithTickets = (j) => {
const res = parseJourney(j)
if (
j.trfRes &&
Array.isArray(j.trfRes.fareSetL) &&
j.trfRes.fareSetL.length > 0
) {
res.tickets = []
for (let t of j.trfRes.fareSetL) {
const tariff = t.desc
if (!tariff || !Array.isArray(t.fareL)) continue
for (let v of t.fareL) {
const variant = v.name
if(!variant) continue
const ticket = {
name: [tariff, variant].join(' - '),
tariff,
variant
}
if (v.prc && Number.isInteger(v.prc) && v.cur) {
ticket.amount = v.prc/100
ticket.currency = v.cur
} else {
ticket.amount = null
ticket.hint = 'No pricing information available.'
}
res.tickets.push(ticket)
}
}
}
return res
}
return parseJourneyWithTickets
}
const createParseMovement = (profile, opt, data) => {
const _parseMovement = _createParseMovement(profile, opt, data)
const parseMovement = (m) => {
const res = _parseMovement(m)
// filter out empty nextStopovers entries
res.nextStopovers = res.nextStopovers.filter((f) => {
return f.stop !== null || f.arrival !== null || f.departure !== null
})
return res
}
return parseMovement
}
const nahshProfile = {
locale: 'de-DE',
timezone: 'Europe/Berlin',
endpoint: 'https://nah.sh.hafas.de/bin/mgate.exe',
transformReqBody,
products,
parseLocation,
parseJourney: createParseJourney,
parseMovement: createParseMovement,
trip: true,
radar: true, // todo: see #34
reachableFrom: true
}
module.exports = nahshProfile