add dbris, moreStops param

This commit is contained in:
Traines 2025-03-21 20:49:38 +00:00
parent b887c674d4
commit b20cf1060a
7 changed files with 139 additions and 74 deletions

2
api.js
View file

@ -2,6 +2,7 @@ import {createClient} from './index.js';
import {profile as dbProfile} from './p/db/index.js';
import {profile as dbnavProfile} from './p/dbnav/index.js';
import {profile as dbwebProfile} from './p/dbweb/index.js';
import {profile as dbrisProfile} from './p/dbris/index.js';
import {mapRouteParsers} from './lib/api-parsers.js';
import {createHafasRestApi as createApi} from 'hafas-rest-api';
@ -26,6 +27,7 @@ const profiles = {
db: dbProfile,
dbnav: dbnavProfile,
dbweb: dbwebProfile,
dbris: dbrisProfile,
};
const start = async () => {

View file

@ -1,6 +1,10 @@
const formatStationBoardReq = (ctx, station, type) => {
const {profile, opt} = ctx;
if (opt.moreStops) {
station += ',' + opt.moreStops.join(',');
}
return {
endpoint: profile.boardEndpoint,
path: (type == 'departures' ? 'departure' : 'arrival') + '/' + station,
@ -15,35 +19,6 @@ const formatStationBoardReq = (ctx, station, type) => {
};
};
/*
TODO separate RIS::Boards profile?
const formatRisStationBoardReq = (ctx, station, type) => {
const {profile, opt} = ctx;
return {
endpoint: profile.boardEndpoint,
path: type + '/' + station,
query: {
// TODO direction
filterTransports: profile.formatProductsFilter(ctx, opt.products || {}, 'ris'),
timeStart: profile.formatTime(profile, opt.when, true),
timeEnd: profile.formatTime(profile, opt.when.getTime() + opt.duration * 60 * 1000, true),
maxViaStops: opt.stopovers ? undefined : 0,
includeStationGroup: opt.includeRelatedStations,
maxTransportsPerType: opt.results === Infinity ? undefined : opt.results,
includeMessagesDisruptions: opt.remarks,
sortBy: 'TIME_SCHEDULE',
},
method: 'get',
headers: {
'Db-Client-Id': process.env.DB_CLIENT_ID,
'Db-Api-Key': process.env.DB_API_KEY,
'Accept': 'application/vnd.de.db.ris+json',
},
};
};
*/
export {
formatStationBoardReq,
};

View file

@ -1,5 +1,5 @@
import {data as cards} from '../format/loyalty-cards.js';
import {parseBoolean, parseInteger} from 'hafas-rest-api/lib/parse.js';
import {parseBoolean, parseInteger, parseArrayOfStrings} from 'hafas-rest-api/lib/parse.js';
const typesByName = new Map([
['bahncard-1st-25', {type: cards.BAHNCARD, discount: 25, class: 1}],
@ -42,9 +42,7 @@ const parseArrayOr = (parseEntry) => {
};
const mapRouteParsers = (route, parsers) => {
if (route !== 'journeys' && route !== 'refresh-journey') {
return parsers;
}
if (route.includes('journey')) {
return {
...parsers,
firstClass: {
@ -79,6 +77,19 @@ const mapRouteParsers = (route, parsers) => {
parse: parseBoolean,
},
};
}
if (route.includes('departures') || route.includes('arrivals')) {
return {
...parsers,
moreStops: {
description: 'Also include departures/arrivals for up to nine comma-separated station evaNumbers',
type: 'string',
default: '',
parse: parseArrayOfStrings,
},
};
}
return parsers;
};
export {

4
p/dbris/base.json Normal file
View file

@ -0,0 +1,4 @@
{
"boardEndpoint": "https://apis.deutschebahn.com/db/apis/ris-boards/v1/public/",
"defaultLanguage": "en"
}

32
p/dbris/index.js Normal file
View file

@ -0,0 +1,32 @@
import baseProfile from './base.json' with { type: 'json' };
import {products} from '../../lib/products.js';
import {formatStationBoardReq} from './station-board-req.js';
const profile = {
...baseProfile,
locale: 'de-DE',
timezone: 'Europe/Berlin',
products,
formatStationBoardReq,
journeysOutFrwd: true,
departuresGetPasslist: true,
departuresStbFltrEquiv: true,
trip: false,
radar: false,
refreshJourney: false,
journeysFromTrip: false,
refreshJourneyUseOutReconL: false,
tripsByName: false,
remarks: false,
remarksGetPolyline: false,
reachableFrom: false,
lines: false,
};
export {
profile,
};

View file

@ -0,0 +1,34 @@
const formatStationBoardReq = (ctx, station, type) => {
const {profile, opt} = ctx;
const query = {
filterTransports: profile.formatProductsFilter(ctx, opt.products || {}, 'ris_alt'),
timeStart: profile.formatTime(profile, opt.when, true),
timeEnd: profile.formatTime(profile, opt.when.getTime() + opt.duration * 60 * 1000, true),
includeStationGroup: opt.includeRelatedStations,
maxTransportsPerType: opt.results === Infinity ? undefined : opt.results,
includeMessagesDisruptions: opt.remarks,
sortBy: 'TIME_SCHEDULE',
};
if (!opt.stopovers) {
query.maxViaStops = 0;
}
if (opt.moreStops) {
station += ',' + opt.moreStops.join(',');
}
return {
endpoint: profile.boardEndpoint,
path: type + '/' + station,
query: query,
method: 'get',
headers: {
'Db-Client-Id': process.env.DB_CLIENT_ID,
'Db-Api-Key': process.env.DB_API_KEY,
'Accept': 'application/vnd.de.db.ris+json',
},
};
};
export {
formatStationBoardReq,
};

View file

@ -40,16 +40,23 @@ const createParseArrOrDep = (prefix) => {
res.remarks = profile.parseRemarks(ctx, d);
}
if ((opt.stopovers || opt.direction) && Array.isArray(d.ueber)) {
const stopovers = d.ueber
if (opt.stopovers || opt.direction) {
let stopovers = undefined;
if (Array.isArray(d.ueber)) {
stopovers = d.ueber
.map(viaName => profile.parseStopover(ctx, {name: viaName}, null));
} else if (Array.isArray(d.transport?.via)) {
stopovers = (d.transport?.via)
.map(via => profile.parseStopover(ctx, via, null));
}
if (stopovers) {
if (prefix === ARRIVAL) {
res.previousStopovers = stopovers;
} else if (prefix === DEPARTURE) {
res.nextStopovers = stopovers;
}
}
}
return res;
};