BVG: parse occupancy

This commit is contained in:
Jannis R 2021-10-28 12:41:36 +02:00
parent a8d4d328b8
commit 69ddf5fb8d
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
7 changed files with 9460 additions and 2 deletions

View file

@ -9,7 +9,9 @@ const {parseHook} = require('../../lib/profile-hooks')
const {
parseLine: _parseLine,
parseLocation: _parseLocation,
parseArrival: _parseArrival,
parseDeparture: _parseDeparture,
parseStopover: _parseStopover,
parseJourneyLeg: _parseJourneyLeg,
formatStation: _formatStation,
} = require('../../lib/default-profile')
@ -17,6 +19,29 @@ const {
const baseProfile = require('./base.json')
const products = require('./products')
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'],
// todo: high
])
const journeyLegOccupancyCodes = new Map([
['text.occup.jny.max.11', 'low'],
['text.occup.jny.max.12', 'medium'],
// todo: high
])
// todo: https://m.tagesspiegel.de/berlin/fahrerlebnis-wie-im-regionalexpress-so-faehrt-es-sich-in-der-neuen-express-s-bahn/25338674.html
const parseLineWithMoreDetails = ({parsed}, p) => {
parsed.name = p.name.replace(/^(bus|tram)\s+/i, '')
@ -56,6 +81,27 @@ const parseDepartureRenameRingbahn = ({parsed}, dep) => {
}
return parsed
}
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
}
const parseJourneyLegWithBerlkönig = (ctx, leg, date) => {
if (leg.type === 'KISS') {
@ -86,6 +132,12 @@ const parseJourneyLegWithBerlkönig = (ctx, leg, date) => {
}
return _parseJourneyLeg(ctx, leg, date)
}
const parseJourneyLegWithOccupancy = ({parsed}, leg, date) => {
if (leg.type === 'JNY') {
addOccupancy(parsed, journeyLegOccupancyCodes)
}
return parsed
}
const validIBNR = /^\d+$/
const formatStation = (id) => {
@ -126,8 +178,19 @@ const bvgProfile = {
parseLine: parseHook(_parseLine, parseLineWithMoreDetails),
parseLocation: parseHook(_parseLocation, parseLocation),
parseStationName: (ctx, name) => shorten(name),
parseDeparture: parseHook(_parseDeparture, parseDepartureRenameRingbahn),
parseJourneyLeg: parseJourneyLegWithBerlkönig,
parseArrival: parseHook(
parseHook(_parseArrival, parseArrivalRenameRingbahn),
parseArrDepWithOccupancy,
),
parseDeparture: parseHook(
parseHook(_parseDeparture, parseDepartureRenameRingbahn),
parseArrDepWithOccupancy,
),
parseStopover: parseHook(_parseStopover, parseStopoverWithOccupancy),
parseJourneyLeg: parseHook(
parseJourneyLegWithBerlkönig,
parseJourneyLegWithOccupancy,
),
formatStation,

31
test/bvg-arrivals.js Normal file
View file

@ -0,0 +1,31 @@
'use strict'
const tap = require('tap')
const createClient = require('..')
const rawProfile = require('../p/bvg')
const res = require('./fixtures/bvg-arrivals.json')
const expected = require('./fixtures/bvg-arrivals.js')
const client = createClient(rawProfile, 'public-transport/hafas-client:test')
const {profile} = client
const opt = {
direction: null,
duration: 10,
linesOfStops: true,
remarks: true,
stopovers: true,
includeRelatedStations: true,
when: '2021-10-28T10:35:00+02:00',
products: {}
}
tap.test('parses an arrival correctly (BVG)', (t) => {
const common = profile.parseCommon({profile, opt, res})
const ctx = {profile, opt, common, res}
const arrivals = res.jnyL.map(d => profile.parseArrival(ctx, d))
t.same(arrivals, expected)
t.end()
})

View file

@ -0,0 +1,29 @@
'use strict'
const tap = require('tap')
const createClient = require('..')
const rawProfile = require('../p/bvg')
const res = require('./fixtures/bvg-trip-with-occupancy.json')
const expected = require('./fixtures/bvg-trip-with-occupancy.js')
const client = createClient(rawProfile, 'public-transport/hafas-client:test')
const {profile} = client
const opt = {
stopovers: true,
polyline: true,
subStops: false,
entrances: true,
remarks: true,
when: '2021-10-28T09:28:00+02:00',
}
tap.test('parses an trip with occupancy correctly (BVG)', (t) => {
const common = profile.parseCommon({profile, opt, res})
const ctx = {profile, opt, common, res}
const trip = profile.parseTrip(ctx, res.journey)
t.same(trip, expected)
t.end()
})

1367
test/fixtures/bvg-arrivals.js vendored Normal file

File diff suppressed because it is too large Load diff

4332
test/fixtures/bvg-arrivals.json vendored Normal file

File diff suppressed because it is too large Load diff

1937
test/fixtures/bvg-trip-with-occupancy.js vendored Normal file

File diff suppressed because it is too large Load diff

1699
test/fixtures/bvg-trip-with-occupancy.json vendored Normal file

File diff suppressed because it is too large Load diff