mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
DB departures/arrivals: parse load factor
See https://www.bahn.de/p/view/service/buchung/auslastungsinformation.shtml for reference.
This commit is contained in:
parent
4f8bc17015
commit
f92e9336c0
2 changed files with 55 additions and 1 deletions
4
index.js
4
index.js
|
@ -89,6 +89,7 @@ const createClient = (profile, userAgent, request = _request) => {
|
|||
.then((d) => {
|
||||
if (!Array.isArray(d.jnyL)) return []
|
||||
const parse = parser(profile, opt, {
|
||||
raw: d,
|
||||
locations: d.locations,
|
||||
lines: d.lines,
|
||||
hints: d.hints,
|
||||
|
@ -218,6 +219,7 @@ const createClient = (profile, userAgent, request = _request) => {
|
|||
if (!Array.isArray(d.outConL)) return []
|
||||
|
||||
const parse = profile.parseJourney(profile, opt, {
|
||||
raw: d,
|
||||
locations: d.locations,
|
||||
lines: d.lines,
|
||||
hints: d.hints,
|
||||
|
@ -277,6 +279,7 @@ const createClient = (profile, userAgent, request = _request) => {
|
|||
}
|
||||
|
||||
const parse = profile.parseJourney(profile, opt, {
|
||||
raw: d,
|
||||
locations: d.locations,
|
||||
lines: d.lines,
|
||||
hints: d.hints,
|
||||
|
@ -406,6 +409,7 @@ const createClient = (profile, userAgent, request = _request) => {
|
|||
})
|
||||
.then((d) => {
|
||||
const parse = profile.parseJourneyLeg(profile, opt, {
|
||||
raw: d,
|
||||
locations: d.locations,
|
||||
lines: d.lines,
|
||||
hints: d.hints,
|
||||
|
|
|
@ -2,7 +2,10 @@
|
|||
|
||||
const trim = require('lodash/trim')
|
||||
|
||||
const _createParseArrival = require('../../parse/arrival')
|
||||
const _createParseDeparture = require('../../parse/departure')
|
||||
const _createParseJourney = require('../../parse/journey')
|
||||
const _createParseJourneyLeg = require('../../parse/journey-leg')
|
||||
const _createParseLine = require('../../parse/line')
|
||||
const _parseHint = require('../../parse/hint')
|
||||
const _formatStation = require('../../format/station')
|
||||
|
@ -13,13 +16,43 @@ const formatLoyaltyCard = require('./loyalty-cards').format
|
|||
|
||||
const transformReqBody = (body) => {
|
||||
body.client = {id: 'DB', v: '16040000', type: 'IPH', name: 'DB Navigator'}
|
||||
body.ext = 'DB.R18.06.a'
|
||||
body.ext = 'DB.R19.04.a'
|
||||
body.ver = '1.16'
|
||||
body.auth = {type: 'AID', aid: 'n91dB8Z77MLdoR0K'}
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
// https://www.bahn.de/p/view/service/buchung/auslastungsinformation.shtml
|
||||
const loadFactors = []
|
||||
loadFactors[1] = 'low-to-medium'
|
||||
loadFactors[2] = 'high'
|
||||
loadFactors[3] = 'very-high'
|
||||
loadFactors[4] = 'exceptionally-high'
|
||||
|
||||
const parseLoadFactor = (opt, tcocL, tcocX) => {
|
||||
const cls = opt.firstClass ? 'FIRST' : 'SECOND'
|
||||
const load = tcocX.map(i => tcocL[i]).find(lf => lf.c === cls)
|
||||
return load && loadFactors[load.r] || null
|
||||
}
|
||||
|
||||
const createParseArrOrDep = (createParse) => (profile, opt, data) => {
|
||||
const parse = createParse(profile, opt, data)
|
||||
const parseWithLoadFactor = (d) => {
|
||||
const result = parse(d)
|
||||
if (d.stbStop.dTrnCmpSX && Array.isArray(d.stbStop.dTrnCmpSX.tcocX)) {
|
||||
const {tcocL} = data.raw.common
|
||||
const load = parseLoadFactor(opt, tcocL, d.stbStop.dTrnCmpSX.tcocX)
|
||||
if (load) result.loadFactor = load
|
||||
}
|
||||
return result
|
||||
}
|
||||
return parseWithLoadFactor
|
||||
}
|
||||
|
||||
const createParseArrival = createParseArrOrDep(_createParseArrival)
|
||||
const createParseDeparture = createParseArrOrDep(_createParseDeparture)
|
||||
|
||||
const transformJourneysQuery = (query, opt) => {
|
||||
const filters = query.jnyFltrL
|
||||
if (opt.bike) filters.push(bike)
|
||||
|
@ -96,6 +129,20 @@ const createParseJourney = (profile, opt, data) => {
|
|||
return parseJourneyWithPrice
|
||||
}
|
||||
|
||||
const createParseJourneyLeg = (profile, opt, data) => {
|
||||
const parseJourneyLeg = _createParseJourneyLeg(profile, opt, data)
|
||||
const parseJourneyLegWithLoadFactor = (j, pt, parseStopovers) => {
|
||||
const result = parseJourneyLeg(j, pt, parseStopovers)
|
||||
if (pt.jny && pt.jny.dTrnCmpSX && Array.isArray(pt.jny.dTrnCmpSX.tcocX)) {
|
||||
const {tcocL} = data.raw.common
|
||||
const load = parseLoadFactor(opt, tcocL, pt.jny.dTrnCmpSX.tcocX)
|
||||
if (load) result.loadFactor = load
|
||||
}
|
||||
return result
|
||||
}
|
||||
return parseJourneyLegWithLoadFactor
|
||||
}
|
||||
|
||||
const hintsByCode = Object.assign(Object.create(null), {
|
||||
fb: {
|
||||
type: 'hint',
|
||||
|
@ -332,7 +379,10 @@ const dbProfile = {
|
|||
|
||||
// todo: parseLocation
|
||||
parseJourney: createParseJourney,
|
||||
parseJourneyLeg: createParseJourneyLeg,
|
||||
parseLine: createParseLine,
|
||||
parseArrival: createParseArrival,
|
||||
parseDeparture: createParseDeparture,
|
||||
parseHint,
|
||||
|
||||
formatStation,
|
||||
|
|
Loading…
Add table
Reference in a new issue