Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bahn.de boards #12

Merged
merged 19 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const createClient = (profile, userAgent, opt = {}) => {
const {res} = await profile.request({profile, opt}, userAgent, req);

const ctx = {profile, opt, common, res};
const results = (res[resultsField] || res.items || res.bahnhofstafelAbfahrtPositionen || res.bahnhofstafelAnkunftPositionen)
const results = (res[resultsField] || res.items || res.bahnhofstafelAbfahrtPositionen || res.bahnhofstafelAnkunftPositionen || res.entries)
.map(res => parse(ctx, res)); // TODO sort?, slice

return {
Expand Down
2 changes: 1 addition & 1 deletion p/db/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"nearbyEndpoint": "https://int.bahn.de/web/api/reiseloesung/orte/nearby",
"tripEndpoint": "https://int.bahn.de/web/api/reiseloesung/fahrt",
"regioGuideTripEndpoint": "https://regio-guide.de/@prd/zupo-travel-information/api/public/ri/journey/",
"boardEndpoint": "https://regio-guide.de/@prd/zupo-travel-information/api/public/ri/board/",
"boardEndpoint": "https://int.bahn.de/web/api/reiseloesung/",
"defaultLanguage": "en"
}
2 changes: 2 additions & 0 deletions p/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {formatJourneysReq, formatRefreshJourneyReq} from './journeys-req.js';
import {formatTripReq} from './trip-req.js';
import {formatLocationFilter} from './location-filter.js';
import {formatLocationsReq} from './locations-req.js';
import {formatStationBoardReq} from './station-board-req.js';

const profile = {
...baseProfile,
Expand All @@ -19,6 +20,7 @@ const profile = {
formatTripReq,
formatLocationsReq,
formatLocationFilter,
formatStationBoardReq,
};

export {
Expand Down
23 changes: 23 additions & 0 deletions p/db/station-board-req.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const formatStationBoardReq = (ctx, station, type) => {
const {profile, opt} = ctx;

const maxVias = opt.vias ?? 0;

return {
endpoint: profile.boardEndpoint,
path: type === 'departures' ? 'abfahrten' : 'ankuenfte',
query: {
ortExtId: station,
zeit: profile.formatTimeOfDay(profile, opt.when),
datum: profile.formatDate(profile, opt.when),
mitVias: maxVias !== 0 ? true : undefined,
maxVias: maxVias === -1 ? undefined : maxVias,
verkehrsmittel: profile.formatProductsFilter(ctx, opt.products || {}),
},
method: 'GET',
};
};

export {
formatStationBoardReq,
};
15 changes: 8 additions & 7 deletions parse/arrival-or-departure.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ const createParseArrOrDep = (prefix) => {
const {profile, opt} = ctx;
const cancelled = profile.parseCancelled(d);
const res = {
tripId: d.journeyID || d.train?.journeyId || d.zuglaufId,
stop: profile.parseLocation(ctx, d.station || d.abfrageOrt),
tripId: d.journeyID || d.journeyId || d.train?.journeyId || d.zuglaufId,
stop: profile.parseLocation(ctx, d.station || d.abfrageOrt || d.bahnhofsId),
...profile.parseWhen(
ctx,
null,
d.timeSchedule || d.time || d.abgangsDatum || d.ankunftsDatum,
d.timeType != 'SCHEDULE' ? d.timePredicted || d.time || d.ezAbgangsDatum || d.ezAnkunftsDatum : null,
d.timeSchedule || d.time || d.zeit || d.abgangsDatum || d.ankunftsDatum,
d.timeType != 'SCHEDULE' ? d.timePredicted || d.time || d.ezZeit || d.ezAbgangsDatum || d.ezAnkunftsDatum : null,
cancelled),
...profile.parsePlatform(ctx, d.platformSchedule || d.platform || d.gleis, d.platformPredicted || d.platform || d.ezGleis, cancelled),
...profile.parsePlatform(ctx, d.platformSchedule || d.gleis || d.platform || d.ezGleis, d.platformPredicted || d.platform || d.ezGleis, cancelled),
traines-source marked this conversation as resolved.
Show resolved Hide resolved
// prognosisType: TODO
direction: d.transport?.direction?.stopPlaces?.length > 0 && profile.parseStationName(ctx, d.transport?.direction?.stopPlaces[0].name) || profile.parseStationName(ctx, d.destination?.name || d.richtung) || null,
direction: d.transport?.direction?.stopPlaces?.length > 0 && profile.parseStationName(ctx, d.transport?.direction?.stopPlaces[0].name) || profile.parseStationName(ctx, d.destination?.name || d.richtung || d.terminus) || null,
provenance: profile.parseStationName(ctx, d.transport?.origin?.name || d.origin?.name || d.abgangsOrt?.name) || null,
line: profile.parseLine(ctx, d) || null,
remarks: [],
remarks: profile.parseRemarks(ctx, d),
traines-source marked this conversation as resolved.
Show resolved Hide resolved
origin: profile.parseLocation(ctx, d.transport?.origin || d.origin) || null,
destination: profile.parseLocation(ctx, d.transport?.destination || d.destination) || null,
vias: d.ueber,
// loadFactor: profile.parseArrOrDepWithLoadFactor(ctx, d)
};

Expand Down
10 changes: 5 additions & 5 deletions parse/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import slugg from 'slugg';

const parseLine = (ctx, p) => {
const profile = ctx.profile;
const fahrtNr = p.verkehrsmittel?.nummer || p.transport?.number || p.train?.no || p.no || ((p.risZuglaufId || '') + '_').split('_')[1] || p.verkehrsmittelNummer || ((p.mitteltext || '') + ' ').split(' ')[1] || ((p.zugName || '') + ' ').split(' ')[1];
const fahrtNr = p.verkehrsmittel?.nummer || p.transport?.number || p.train?.no || p.no || ((p.risZuglaufId || '') + '_').split('_')[1] || p.verkehrsmittelNummer || ((p.verkehrmittel?.langText || '') + ' ').split(' ')[1] || ((p.mitteltext || '') + ' ').split(' ')[1] || ((p.zugName || '') + ' ').split(' ')[1];
traines-source marked this conversation as resolved.
Show resolved Hide resolved
const res = {
type: 'line',
id: slugg(p.verkehrsmittel?.langText || p.transport?.journeyDescription || p.risZuglaufId || p.train && p.train.category + ' ' + p.train.lineName + ' ' + p.train.no || p.no && p.name + ' ' + p.no || p.langtext || p.mitteltext || p.zugName), // TODO terrible
id: slugg(p.verkehrsmittel?.langText || p.verkehrmittel?.langText || p.transport?.journeyDescription || p.risZuglaufId || p.train && p.train.category + ' ' + p.train.lineName + ' ' + p.train.no || p.no && p.name + ' ' + p.no || p.langtext || p.mitteltext || p.zugName), // TODO terrible
fahrtNr: String(fahrtNr),
name: p.verkehrsmittel?.langText || p.verkehrsmittel?.name || p.zugName || p.transport && p.transport.category + ' ' + p.transport.line || p.train && p.train.category + ' ' + p.train.lineName || p.name || p.mitteltext || p.langtext,
name: p.verkehrsmittel?.langText || p.verkehrsmittel?.name || p.verkehrmittel?.name || p.verkehrmittel?.langText || p.zugName || p.transport && p.transport.category + ' ' + p.transport.line || p.train && p.train.category + ' ' + p.train.lineName || p.name || p.mitteltext || p.langtext,
public: true,
};

const adminCode = p.administrationId || p.administration?.id || p.administration?.administrationID;
if (adminCode) {
res.adminCode = adminCode;
}
res.productName = p.verkehrsmittel?.kurzText || p.transport?.category || p.train?.category || p.category || p.kurztext;
const foundProduct = profile.products.find(pp => pp.vendo == p.verkehrsmittel?.produktGattung || pp.ris == p.transport?.type || pp.ris == p.train?.type || pp.ris == p.type || pp.ris_alt == p.train?.type || pp.ris_alt == p.type || pp.dbnav_short == p.produktGattung);
res.productName = p.verkehrsmittel?.kurzText || p.verkehrmittel?.kurzText || p.transport?.category || p.train?.category || p.category || p.kurztext;
const foundProduct = profile.products.find(pp => pp.vendo == p.verkehrsmittel?.produktGattung || pp.vendo == p.verkehrmittel?.produktGattung || pp.ris == p.transport?.type || pp.ris == p.train?.type || pp.ris == p.type || pp.ris_alt == p.train?.type || pp.ris_alt == p.type || pp.dbnav_short == p.produktGattung);
res.mode = foundProduct?.mode;
res.product = foundProduct?.id;

Expand Down
4 changes: 4 additions & 0 deletions parse/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const parseLocation = (ctx, l) => {
return null;
}

if (typeof l === 'string') {
traines-source marked this conversation as resolved.
Show resolved Hide resolved
return {type: 'station', id: l};
}

const lid = parse(l.id || l.locationId, {delimiter: '@'});
const res = {
type: 'location',
Expand Down
16 changes: 11 additions & 5 deletions parse/remarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const parseRemarks = (ctx, ref) => {
ref.hims || [],
ref.serviceNotiz && [ref.serviceNotiz] || [],
ref.messages || [],
ref.meldungen || [],
ref.meldungenAsObject || [],
ref.attributNotizen || [],
ref.attributes || [],
Expand Down Expand Up @@ -202,11 +203,16 @@ const parseRemarks = (ctx, ref) => {
*/

const parseCancelled = (ref) => {
return ref.canceled || ref.cancelled || ref.journeyCancelled || (ref.risNotizen || ref.echtzeitNotizen) && Boolean((ref.risNotizen || ref.echtzeitNotizen).find(r => r.key == 'text.realtime.stop.cancelled'
|| r.type == 'HALT_AUSFALL'
|| r.text == 'Halt entfällt'
|| r.text == 'Stop cancelled',
));
return ref.canceled
|| ref.cancelled
|| ref.journeyCancelled
|| (ref.risNotizen || ref.echtzeitNotizen || ref.meldungen) && Boolean(
(ref.risNotizen || ref.echtzeitNotizen || ref.meldungen).find(r => r.key == 'text.realtime.stop.cancelled'
|| r.type == 'HALT_AUSFALL'
|| r.text == 'Halt entfällt'
|| r.text == 'Stop cancelled',
),
);
};

export {
Expand Down
11 changes: 6 additions & 5 deletions test/db-departures.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import tap from 'tap';

import {createClient} from '../index.js';
import {profile as rawProfile} from '../p/db/index.js';
const res = require('./fixtures/db-departures-regio-guide.json');
import {dbDepartures as expected} from './fixtures/db-departures-regio-guide.js';
const res = require('./fixtures/db-departures.json');
import {dbDepartures as expected} from './fixtures/db-departures.js';

const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false});
const {profile} = client;
Expand All @@ -20,13 +20,14 @@ const opt = {
remarks: true,
stopovers: true,
includeRelatedStations: true,
when: '2019-08-19T20:30:00+02:00',
when: '2025-02-05T15:00:00',
products: {},
vias: 5,
};

tap.test('parses a regio-guide departure correctly', (t) => {
tap.test('parses a db departure correctly', (t) => {
const ctx = {profile, opt, common: null, res};
const departures = res.items.map(d => profile.parseDeparture(ctx, d));
const departures = res.entries.map(d => profile.parseDeparture(ctx, d));

t.same(departures, expected);
t.end();
Expand Down
1,236 changes: 1,236 additions & 0 deletions test/e2e/fixtures/requests_1722637011/recording.har

Large diffs are not rendered by default.

Loading
Loading