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

121 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2017-11-18 17:53:12 +01:00
'use strict'
const shorten = require('vbb-short-station-name')
2017-11-20 01:05:48 +01:00
const {to12Digit, to9Digit} = require('vbb-translate-ids')
2017-12-11 14:51:09 +01:00
const parseLineName = require('vbb-parse-line')
const parseTicket = require('vbb-parse-ticket')
2017-12-12 03:28:54 +01:00
const getStations = require('vbb-stations')
2019-10-20 00:19:52 +02:00
const {parseHook} = require('../../lib/profile-hooks')
2017-11-18 17:53:12 +01:00
2019-10-20 00:19:52 +02:00
const _parseLine = require('../../parse/line')
2017-11-18 17:53:12 +01:00
const _parseLocation = require('../../parse/location')
2019-10-20 00:19:52 +02:00
const _parseJourney = require('../../parse/journey')
const _parseDeparture = require('../../parse/departure')
const _formatStation = require('../../format/station')
2017-11-18 17:53:12 +01:00
const baseProfile = require('./base.json')
2018-03-16 14:23:27 +01:00
const products = require('./products')
2017-11-18 17:53:12 +01:00
2020-03-18 21:35:43 +01:00
// todo: https://m.tagesspiegel.de/berlin/fahrerlebnis-wie-im-regionalexpress-so-faehrt-es-sich-in-der-neuen-express-s-bahn/25338674.html
2019-10-20 00:19:52 +02:00
const parseLineWithMoreDetails = ({parsed}, p) => {
parsed.name = p.name.replace(/^(bus|tram)\s+/i, '')
const details = parseLineName(parsed.name)
parsed.symbol = details.symbol
parsed.nr = details.nr
parsed.metro = details.metro
parsed.express = details.express
parsed.night = details.night
2017-11-18 17:53:12 +01:00
2019-10-20 00:19:52 +02:00
return parsed
2017-11-18 17:53:12 +01:00
}
2019-10-20 00:19:52 +02:00
const parseLocation = ({parsed}, l) => {
if ((parsed.type === 'stop' || parsed.type === 'station') && parsed.id[0] === '9') {
2019-10-20 00:19:52 +02:00
parsed.name = shorten(parsed.name)
parsed.id = to12Digit(parsed.id)
if (!parsed.location.latitude || !parsed.location.longitude) {
const [s] = getStations(parsed.id)
if (s) Object.assign(parsed.location, s.location)
2017-12-12 03:28:54 +01:00
}
2017-11-20 01:05:48 +01:00
}
2019-10-20 00:19:52 +02:00
return parsed
2017-11-18 17:53:12 +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[0] &&
Array.isArray(j.trfRes.fareSetL[0].fareL)
) {
parsed.tickets = []
const sets = j.trfRes.fareSetL[0].fareL
for (let s of sets) {
if (!Array.isArray(s.ticketL) || s.ticketL.length === 0) continue
for (let t of s.ticketL) {
const ticket = parseTicket(t)
ticket.name = s.name + ' ' + ticket.name
parsed.tickets.push(ticket)
}
}
}
2019-10-20 00:19:52 +02:00
return parsed
}
2019-10-20 00:19:52 +02:00
const ringbahnClockwise = /^ringbahn s\s?41$/i
const ringbahnAnticlockwise = /^ringbahn s\s?42$/i
const parseDepartureRenameRingbahn = ({parsed}) => {
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 ⟲'
}
}
2019-10-20 00:19:52 +02:00
return parsed
}
2017-12-16 03:20:11 +01:00
const validIBNR = /^\d+$/
2017-11-18 17:53:12 +01:00
const formatStation = (id) => {
if ('string' !== typeof id) throw new TypeError('station ID must be a string.')
if (!validIBNR.test(id)) {
2017-12-16 03:20:11 +01:00
throw new Error('station ID must be a valid IBNR.')
}
// The VBB has some 7-digit stations. We don't convert them to 12 digits,
// because it only recognizes in the 7-digit format. see derhuerst/vbb-hafas#22
if (id.length !== 7) id = to9Digit(id)
2017-11-18 17:53:12 +01:00
return _formatStation(id)
}
const vbbProfile = {
...baseProfile,
locale: 'de-DE',
2017-11-18 17:53:12 +01:00
timezone: 'Europe/Berlin',
// https://gist.github.com/derhuerst/a8d94a433358abc015ff77df4481070c#file-haf_config_base-properties-L39
// https://runkit.com/derhuerst/hafas-decrypt-encrypted-mac-salt
salt: Buffer.from('RCTJM2fFxFfxxQfI', 'utf8'),
addMicMac: true,
products: products,
2017-12-12 03:28:54 +01:00
2019-10-20 00:19:52 +02:00
parseLine: parseHook(_parseLine, parseLineWithMoreDetails),
parseLocation: parseHook(_parseLocation, parseLocation),
parseStationName: (ctx, name) => shorten(name),
parseJourney: parseHook(_parseJourney, parseJourneyWithTickets),
parseDeparture: parseHook(_parseDeparture, parseDepartureRenameRingbahn),
2017-11-18 17:53:12 +01:00
2017-11-20 01:05:48 +01:00
formatStation,
departuresGetPasslist: false,
departuresStbFltrEquiv: false,
journeysWalkingSpeed: true,
refreshJourneyUseOutReconL: true,
trip: true,
2018-08-26 18:35:27 +02:00
radar: true,
2020-09-16 17:21:59 +02:00
reachableFrom: true,
2017-11-18 17:53:12 +01:00
}
module.exports = vbbProfile