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

169 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-05-07 16:17:37 +02:00
// todo: use import assertions once they're supported by Node.js & ESLint
// https://github.com/tc39/proposal-import-assertions
import {createRequire} from 'module'
const require = createRequire(import.meta.url)
2018-07-28 13:43:15 +02:00
2022-05-07 16:17:37 +02:00
import {parseHook} from '../../lib/profile-hooks.js'
2018-07-28 13:43:15 +02:00
2022-05-07 16:17:37 +02:00
import {parseAndAddLocationDHID} from '../vbb/parse-loc-dhid.js'
2022-03-07 01:34:33 +01:00
2022-05-07 16:17:37 +02:00
import {parseLocation as _parseLocation} from '../../parse/location.js'
import {parseArrival as _parseArrival} from '../../parse/arrival.js'
import {parseDeparture as _parseDeparture} from '../../parse/departure.js'
import {parseStopover as _parseStopover} from '../../parse/stopover.js'
import {parseJourneyLeg as _parseJourneyLeg} from '../../parse/journey-leg.js'
2018-07-28 13:43:15 +02:00
const baseProfile = require('./base.json')
2022-05-07 16:17:37 +02:00
import {products} from './products.js'
2018-07-28 13:43:15 +02:00
2021-10-29 13:03:58 +02:00
// todo: there's also a referenced icon `{"res":"occup_fig_{low,mid}"}`
2021-10-28 12:41:36 +02:00
const addOccupancy = (item, occupancyCodes) => {
const remIdx = (item.remarks || [])
.findIndex(r => r.code && occupancyCodes.has(r.code))
if (remIdx < 0) return;
const rem = item.remarks[remIdx]
item.occupancy = occupancyCodes.get(rem.code)
item.remarks = [
...item.remarks.slice(0, remIdx),
...item.remarks.slice(remIdx + 1),
]
}
const stopoverOccupancyCodes = new Map([
['text.occup.loc.max.11', 'low'],
['text.occup.loc.max.12', 'medium'],
2021-10-29 13:03:58 +02:00
['text.occup.loc.max.13', 'high'],
2021-10-28 12:41:36 +02:00
])
const journeyLegOccupancyCodes = new Map([
['text.occup.jny.max.11', 'low'],
['text.occup.jny.max.12', 'medium'],
2021-10-29 13:03:58 +02:00
['text.occup.jny.max.13', 'high'],
2021-10-28 12:41:36 +02:00
])
2019-10-20 00:19:52 +02:00
const parseLocation = ({parsed}, l) => {
2022-03-07 01:34:33 +01:00
parseAndAddLocationDHID(parsed, l)
2019-10-20 00:19:52 +02:00
return parsed
2018-07-28 13:43:15 +02:00
}
2021-10-28 12:37:32 +02:00
// todo: S45, S46?
2019-10-20 00:19:52 +02:00
const ringbahnClockwise = /^ringbahn s\s?41$/i
const ringbahnAnticlockwise = /^ringbahn s\s?42$/i
2021-10-28 12:37:32 +02:00
const parseDepartureRenameRingbahn = ({parsed}, dep) => {
2019-10-20 00:19:52 +02:00
if (parsed.line && parsed.line.product === 'suburban') {
const d = parsed.direction && parsed.direction.trim()
if (ringbahnClockwise.test(d)) {
parsed.direction = 'Ringbahn S41 ⟳'
} else if (ringbahnAnticlockwise.test(d)) {
parsed.direction = 'Ringbahn S42 ⟲'
2018-07-28 13:43:15 +02:00
}
}
2019-10-20 00:19:52 +02:00
return parsed
2018-07-28 13:43:15 +02:00
}
2021-10-28 12:41:36 +02:00
const parseArrivalRenameRingbahn = ({parsed}, arr) => {
if (parsed.line && parsed.line.product === 'suburban') {
const p = parsed.provenance && parsed.provenance.trim()
if (ringbahnClockwise.test(p)) {
parsed.provenance = 'Ringbahn S41 ⟳'
} else if (ringbahnAnticlockwise.test(p)) {
parsed.provenance = 'Ringbahn S42 ⟲'
}
}
return parsed
}
const parseArrDepWithOccupancy = ({parsed}, d) => {
addOccupancy(parsed, stopoverOccupancyCodes)
return parsed
}
const parseStopoverWithOccupancy = ({parsed}, st, date) => {
addOccupancy(parsed, stopoverOccupancyCodes)
return parsed
}
2018-07-28 13:43:15 +02:00
2019-10-20 00:19:52 +02:00
const parseJourneyLegWithBerlkönig = (ctx, leg, date) => {
if (leg.type === 'KISS') {
const icon = ctx.common.icons[leg.icoX]
if (icon && icon.type === 'prod_berl') {
const res = _parseJourneyLeg(ctx, {
...leg, type: 'WALK'
}, date)
delete res.walking
const mcp = leg.dep.mcp || {}
const mcpData = mcp.mcpData || {}
// todo: mcp.lid
// todo: mcpData.occupancy, mcpData.type
// todo: journey.trfRes.bkgData
res.line = {
type: 'line',
id: null, // todo
// todo: fahrtNr?
name: mcpData.providerName,
public: true,
mode: 'taxi',
product: 'berlkoenig'
// todo: operator
2018-10-28 16:37:19 +01:00
}
2019-10-20 00:19:52 +02:00
return res
2018-10-28 16:37:19 +01:00
}
}
2019-10-20 00:19:52 +02:00
return _parseJourneyLeg(ctx, leg, date)
2018-10-28 16:37:19 +01:00
}
2021-10-28 12:41:36 +02:00
const parseJourneyLegWithOccupancy = ({parsed}, leg, date) => {
if (leg.type === 'JNY') {
addOccupancy(parsed, journeyLegOccupancyCodes)
}
return parsed
}
2018-10-28 16:37:19 +01:00
// use the Berlkönig ride sharing service?
2021-12-29 13:51:37 +01:00
// todo: https://github.com/alexander-albers/tripkit/issues/26#issuecomment-825437320
2019-10-20 00:19:52 +02:00
const requestJourneysWithBerlkoenig = ({opt}, query) => {
2018-10-28 16:37:19 +01:00
if (('numF' in query) && opt.berlkoenig) {
// todo: check if this is still true
throw new Error('The `berlkoenig` and `results` options are mutually exclusive.')
}
query.jnyFltrL.push({type: 'GROUP', mode: 'INC', value: 'OEV'})
if (opt.berlkoenig) query.jnyFltrL.push({type: 'GROUP', mode: 'INC', value: 'BERLKOENIG'})
query.gisFltrL = [{meta: 'foot_speed_normal', type: 'M', mode: 'FB'}]
return query
}
2018-07-28 13:43:15 +02:00
// todo: adapt/extend `vbb-parse-ticket` to support the BVG markup
2022-05-07 16:17:37 +02:00
const profile = {
...baseProfile,
2018-07-28 13:43:15 +02:00
locale: 'de-DE',
timezone: 'Europe/Berlin',
2018-10-28 16:37:19 +01:00
transformJourneysQuery: requestJourneysWithBerlkoenig,
2018-07-28 13:43:15 +02:00
products,
2019-10-20 00:19:52 +02:00
parseLocation: parseHook(_parseLocation, parseLocation),
2021-10-28 12:41:36 +02:00
parseArrival: parseHook(
parseHook(_parseArrival, parseArrivalRenameRingbahn),
parseArrDepWithOccupancy,
),
parseDeparture: parseHook(
parseHook(_parseDeparture, parseDepartureRenameRingbahn),
parseArrDepWithOccupancy,
),
parseStopover: parseHook(_parseStopover, parseStopoverWithOccupancy),
parseJourneyLeg: parseHook(
parseJourneyLegWithBerlkönig,
parseJourneyLegWithOccupancy,
),
2018-07-28 13:43:15 +02:00
refreshJourneyUseOutReconL: true,
2018-07-28 13:43:15 +02:00
trip: true,
2018-08-26 18:35:27 +02:00
radar: true,
refreshJourney: true,
reachableFrom: true
2018-07-28 13:43:15 +02:00
}
2022-05-07 16:17:37 +02:00
export {
profile,
}