db-vendo-client/p/nahsh/index.js

112 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-03-14 14:54:08 +01:00
'use strict'
const _parseLocation = require('../../parse/location')
2018-03-15 09:33:31 +01:00
const _createParseJourney = require('../../parse/journey')
const _createParseMovement = require('../../parse/movement')
2018-03-14 14:54:08 +01:00
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)
2018-03-14 14:54:08 +01:00
// weird fix for empty lines, e.g. IC/EC at Flensburg Hbf
if (res.lines) {
2018-03-14 14:54:08 +01:00
res.lines = res.lines.filter(x => x.id && x.name)
}
2018-03-18 13:00:16 +01:00
// remove leading zeroes, todo
if (res.id && res.id.length > 0) {
res.id = res.id.replace(/^0+/, '')
2018-03-14 14:54:08 +01:00
}
return res
}
const createParseJourney = (profile, opt, data) => {
const parseJourney = _createParseJourney(profile, opt, data)
2018-03-15 09:33:31 +01:00
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) {
2018-03-15 09:33:31 +01:00
const tariff = t.desc
if (!tariff || !Array.isArray(t.fareL)) continue
for (let v of t.fareL) {
2018-03-15 09:33:31 +01:00
const variant = v.name
if(!variant) continue
const ticket = {
name: [tariff, variant].join(' - '),
tariff,
variant
}
if (v.prc && Number.isInteger(v.prc) && v.cur) {
2018-03-15 09:33:31 +01:00
ticket.amount = v.prc/100
ticket.currency = v.cur
2018-03-18 13:01:07 +01:00
} else {
2018-03-15 09:33:31 +01:00
ticket.amount = null
ticket.hint = 'No pricing information available.'
}
res.tickets.push(ticket)
}
}
}
return res
}
return parseJourneyWithTickets
}
2018-06-26 11:48:30 +02:00
const createParseMovement = (profile, opt, data) => {
const _parseMovement = _createParseMovement(profile, opt, data)
const parseMovement = (m) => {
const res = _parseMovement(m)
// filter out empty nextStops entries
res.nextStops = res.nextStops.filter((f) => {
return f.stop !== null || f.arrival !== null || f.departure !== null
})
return res
2018-03-14 14:54:08 +01:00
}
return parseMovement
2018-03-14 14:54:08 +01:00
}
const nahshProfile = {
locale: 'de-DE',
timezone: 'Europe/Berlin',
2018-05-28 21:48:58 +02:00
endpoint: 'https://nah.sh.hafas.de/bin/mgate.exe',
2018-03-14 14:54:08 +01:00
transformReqBody,
2018-03-21 01:42:13 +01:00
products,
2018-03-14 14:54:08 +01:00
parseLocation,
2018-03-15 09:33:31 +01:00
parseJourney: createParseJourney,
parseMovement: createParseMovement,
2018-03-14 14:54:08 +01:00
trip: true,
2018-08-26 18:35:27 +02:00
radar: true, // todo: see #34
reachableFrom: true
2018-03-14 14:54:08 +01:00
}
module.exports = nahshProfile