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

100 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-03-14 14:54:08 +01:00
'use strict'
2019-10-20 00:19:52 +02:00
const {parseHook} = require('../../lib/profile-hooks')
2018-03-14 14:54:08 +01:00
2019-10-20 00:19:52 +02:00
const _parseLocation = require('../../parse/location')
const _parseJourney = require('../../parse/journey')
const _parseMovement = require('../../parse/movement')
2018-03-14 14:54:08 +01:00
const products = require('./products')
// todo: journey prices
2019-10-20 00:19:52 +02:00
const transformReqBody = (ctx, body) => {
2018-03-14 14:54:08 +01:00
body.client = {
id: 'NAHSH',
name: 'NAHSHPROD',
v: '3000700'
}
body.ver = '1.16'
body.auth = {aid: 'r0Ot9FLFNAFxijLW'}
body.lang = 'de'
return body
}
2019-10-20 00:19:52 +02:00
const fixLocation = ({parsed}, l) => {
2018-03-14 14:54:08 +01:00
// weird fix for empty lines, e.g. IC/EC at Flensburg Hbf
2019-10-20 00:19:52 +02:00
if (parsed.lines) {
parsed.lines = parsed.lines.filter(x => x.id && x.name)
2018-03-14 14:54:08 +01:00
}
2018-03-18 13:00:16 +01:00
// remove leading zeroes, todo
2019-10-20 00:19:52 +02:00
if (parsed.id && parsed.id.length > 0) {
parsed.id = parsed.id.replace(/^0+/, '')
2018-03-14 14:54:08 +01:00
}
2019-10-20 00:19:52 +02:00
return parsed
2018-03-14 14:54:08 +01:00
}
2019-10-20 00:19:52 +02:00
const parseJourneyWithTickets = ({parsed}, j) => {
if (
j.trfRes &&
Array.isArray(j.trfRes.fareSetL) &&
j.trfRes.fareSetL.length > 0
) {
parsed.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.'
2018-03-15 09:33:31 +01:00
}
2019-10-20 00:19:52 +02:00
parsed.tickets.push(ticket)
2018-03-15 09:33:31 +01:00
}
}
}
2019-10-20 00:19:52 +02:00
return parsed
2018-03-15 09:33:31 +01:00
}
2019-10-20 00:19:52 +02:00
const fixMovement = ({parsed}, m) => {
// filter out empty nextStopovers entries
parsed.nextStopovers = parsed.nextStopovers.filter((f) => {
return f.stop !== null || f.arrival !== null || f.departure !== null
})
return parsed
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
2019-10-20 00:19:52 +02:00
parseLocation: parseHook(_parseLocation, fixLocation),
parseJourney: parseHook(_parseJourney, parseJourneyWithTickets),
parseMovement: parseHook(_parseMovement, fixMovement),
2018-03-14 14:54:08 +01:00
trip: true,
2018-08-26 18:35:27 +02:00
radar: true, // todo: see #34
2020-09-16 17:21:59 +02:00
reachableFrom: true,
remarks: false, // seems like ver >= 1.20 is required
2018-03-14 14:54:08 +01:00
}
module.exports = nahshProfile