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 all 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
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const loadEnrichedStationData = (profile) => new Promise((resolve, reject) => {
readStations.full()
.on('data', (station) => {
items[station.id] = station;
items[station.name] = station;
})
.once('end', () => {
if (profile.DEBUG) {
Expand Down Expand Up @@ -105,7 +106,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
57 changes: 49 additions & 8 deletions p/db/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,67 @@
import {createRequire} from 'module';
const require = createRequire(import.meta.url);

const baseProfile = require('./base.json');
const dbnavBase = require('../dbnav/base.json');
dabund24 marked this conversation as resolved.
Show resolved Hide resolved
const dbregioguideBase = require('../dbregioguide/base.json');
import {products} from '../../lib/products.js';
import {formatJourneysReq, formatRefreshJourneyReq} from './journeys-req.js';

// journeys()
import {formatJourneysReq} from '../dbnav/journeys-req.js';
const {journeysEndpoint} = dbnavBase;

// refreshJourneys()
import {formatRefreshJourneyReq} from '../dbnav/journeys-req.js';
const {refreshJourneysEndpointTickets, refreshJourneysEndpointPolyline} = dbnavBase;

// locations()
import {formatLocationsReq} from '../dbnav/locations-req.js';
import {formatLocationFilter} from '../dbnav/location-filter.js';
const {locationsEndpoint} = dbnavBase;

// stop()
import {formatStopReq} from '../dbnav/stop-req.js';
import {parseStop} from '../dbnav/parse-stop.js';
const {stopEndpoint} = dbnavBase;

// nearby()
import {formatNearbyReq} from '../dbnav/nearby-req.js';
const {nearbyEndpoint} = dbnavBase;

// trip()
import {formatTripReq} from './trip-req.js';
import {formatLocationFilter} from './location-filter.js';
import {formatLocationsReq} from './locations-req.js';
const tripEndpoint_dbnav = dbnavBase.tripEndpoint;
const tripEndpoint_dbregioguide = dbregioguideBase.tripEndpoint;

// arrivals(), departures()
const {boardEndpoint} = dbregioguideBase;

const profile = {
...baseProfile,
locale: 'de-DE',
timezone: 'Europe/Berlin',

products,

formatJourneysReq,
journeysEndpoint,

formatRefreshJourneyReq,
refreshJourneysEndpointTickets, refreshJourneysEndpointPolyline,

formatLocationsReq, formatLocationFilter,
locationsEndpoint,

formatStopReq, parseStop,
stopEndpoint,

formatNearbyReq,
nearbyEndpoint,

formatTripReq,
formatLocationsReq,
formatLocationFilter,
tripEndpoint_dbnav, tripEndpoint_dbregioguide,

boardEndpoint,
};


export {
profile,
};
15 changes: 8 additions & 7 deletions p/db/trip-req.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {formatTripReq as hafasFormatTripReq} from '../../format/trip-req.js';
import {formatTripReq as hafasFormatTripReq} from '../dbnav/trip-req.js';
import {formatTripReq as risTripReq} from '../dbregioguide/trip-req.js';


const formatTripReq = ({profile, opt}, id) => {
const _profile = {...profile};
if (id.includes('#')) {
return hafasFormatTripReq({profile, opt}, id);
_profile['tripEndpoint'] = profile.tripEndpoint_dbnav;
return hafasFormatTripReq({profile: _profile, opt}, id);
}
return {
endpoint: profile.regioGuideTripEndpoint,
path: id,
method: 'get',
};

_profile['tripEndpoint'] = profile.tripEndpoint_dbregioguide;
return risTripReq({profile: _profile, opt}, id);
};

export {
Expand Down
5 changes: 5 additions & 0 deletions p/dbregioguide/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tripEndpoint": "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/",
"defaultLanguage": "en"
}
18 changes: 18 additions & 0 deletions p/dbregioguide/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {createRequire} from 'module';
const require = createRequire(import.meta.url);
const baseProfile = require('./base.json');
import {products} from '../../lib/products.js';
import {formatTripReq} from './trip-req.js';

const profile = {
...baseProfile,
locale: 'de-DE',
timezone: 'Europe/Berlin',

products,
formatTripReq,
};

export {
profile,
};
11 changes: 11 additions & 0 deletions p/dbregioguide/trip-req.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const formatTripReq = ({profile, opt}, id) => {
return {
endpoint: profile.tripEndpoint,
path: id,
method: 'get',
};
};

export {
formatTripReq,
};
3 changes: 1 addition & 2 deletions p/db/base.json → p/dbweb/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"locationsEndpoint": "https://int.bahn.de/web/api/reiseloesung/orte",
"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"
}
File renamed without changes.
29 changes: 29 additions & 0 deletions p/dbweb/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {createRequire} from 'module';
const require = createRequire(import.meta.url);

const baseProfile = require('./base.json');
import {products} from '../../lib/products.js';
import {formatJourneysReq, formatRefreshJourneyReq} from './journeys-req.js';
import {formatLocationFilter} from './location-filter.js';
import {formatLocationsReq} from './locations-req.js';
import {formatStationBoardReq} from './station-board-req.js';

const profile = {
...baseProfile,
locale: 'de-DE',
timezone: 'Europe/Berlin',

products,

formatJourneysReq,
formatRefreshJourneyReq,
formatLocationsReq,
formatLocationFilter,
formatStationBoardReq,

departuresGetPasslist: true,
};

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

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: opt.stopovers || undefined,
verkehrsmittel: profile.formatProductsFilter(ctx, opt.products || {}),
},
method: 'GET',
};
};

export {
formatStationBoardReq,
};
23 changes: 17 additions & 6 deletions parse/arrival-or-departure.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ 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 || {bahnhofsId: 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),
// 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: [],
Expand All @@ -39,7 +39,18 @@ const createParseArrOrDep = (prefix) => {
if (opt.remarks) {
res.remarks = profile.parseRemarks(ctx, d);
}
// TODO opt.stopovers

if (opt.stopovers && Array.isArray(d.ueber)) {
const stopovers = d.ueber
.map(viaName => profile.parseStopover(ctx, {name: viaName}, null));

if (prefix === ARRIVAL) {
res.previousStopovers = stopovers;
} else if (prefix === DEPARTURE) {
res.nextStopovers = stopovers;
}
}

return res;
};

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 || p.verkehrsmittel?.langText || p.mitteltext || p.zugName || '').replace(/\D/g, '');
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?.name || p.verkehrsmittel?.langText || 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
18 changes: 15 additions & 3 deletions parse/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const parseLocation = (ctx, l) => {
const lid = parse(l.id || l.locationId, {delimiter: '@'});
const res = {
type: 'location',
id: (l.extId || l.evaNr || lid.L || l.evaNumber || l.evaNo || '').replace(leadingZeros, '') || null,
id: (l.extId || l.evaNr || lid.L || l.evaNumber || l.evaNo || l.bahnhofsId || '').replace(leadingZeros, '') || null,
};
const name = l.name || lid.O;

Expand All @@ -29,12 +29,14 @@ const parseLocation = (ctx, l) => {
}

// addresses and pois might also have fake evaNr sometimes!
if (l.type === STATION || l.extId || l.evaNumber || l.evaNo || lid.A == '1') {
if (l.type === STATION || l.extId || l.evaNumber || l.evaNo || lid.A == '1' || l.bahnhofsId) {
let stop = {
type: 'station',
id: res.id,
name: name,
};
if (name) {
stop.name = name;
}
if ('number' === typeof res.latitude) {
stop.location = res; // todo: remove `.id`
}
Expand All @@ -57,6 +59,16 @@ const parseLocation = (ctx, l) => {
return stop;
}

if (name && common?.locations?.[name] && res.id === null) {
delete res.type;
delete res.id;

return {
...common.locations[name],
...res,
};
}

res.name = name;
if (l.type === ADDRESS || lid.A == '2') {
res.address = name;
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
33 changes: 0 additions & 33 deletions test/db-departures.js

This file was deleted.

Loading