From 7444e0881946c9a83ee53a37f2e1d14e4a0e665c Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Wed, 18 Nov 2020 14:58:25 +0100 Subject: [PATCH] add SBB profile closes #198 --- p/sbb/example.js | 46 +++++++++++++++++++++++++ p/sbb/index.js | 36 ++++++++++++++++++++ p/sbb/products.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++ p/sbb/readme.md | 23 +++++++++++++ readme.md | 1 + 5 files changed, 191 insertions(+) create mode 100644 p/sbb/example.js create mode 100644 p/sbb/index.js create mode 100644 p/sbb/products.js create mode 100644 p/sbb/readme.md diff --git a/p/sbb/example.js b/p/sbb/example.js new file mode 100644 index 00000000..51492d77 --- /dev/null +++ b/p/sbb/example.js @@ -0,0 +1,46 @@ +'use strict' + +const createClient = require('../../') +const sbbProfile = require('.') + +const client = createClient(sbbProfile, 'hafas-client-example') + +const zurich1 = '8530813' // Zürich Kreuzplatz +const zurich2 = '8503000' // Zürich HB +const basel1 = '8500073' // Basel Aeschenplatz +const basel2 = '8500010' // Basel SBB +const baden1 = '8590197' +const baden2 = '8590173' +const locBaden = { type: 'location', latitude: 47.476, longitude: 8.30613 } + +// client.journeys(zurich1, basel1, { results: 1 }) +// client.journeys(zurich2, basel2, { results: 1 }) +client.journeys(baden1, baden2, { results: 1 }) +// client.departures(zurich2, {duration: 1}) +// client.arrivals(zurich2, {duration: 10, linesOfStops: true}) +// client.locations('Basel', { results: 2 }) +// client.locations('Luzern', { poi: true, stops: false }) +// client.stop(basel2) +// client.nearby(locBaden, { distance: 100 }) +// client.radar({ +// north: 47.5, +// west: 8.2, +// south: 47.3, +// east: 8.5, +// }, { results: 10 }) +// client.reachableFrom({ +// type: 'location', +// id: '980023031', +// latitude: 47.052417, +// longitude: 8.304487, +// name: 'Luzern, Adlun', +// poi: true +// }, { +// maxDuration: 20 +// }) + + +.then((data) => { + console.log(require('util').inspect(data, {depth: null, colors: true})) +}) +.catch(console.error) diff --git a/p/sbb/index.js b/p/sbb/index.js new file mode 100644 index 00000000..9788fe81 --- /dev/null +++ b/p/sbb/index.js @@ -0,0 +1,36 @@ +'use strict' + +const products = require('./products') + +const transformReqBody = (ctx, body) => { + // sourced from https://github.com/marudor/BahnhofsAbfahrten/blob/1c99d8b/packages/server/HAFAS/profiles.ts#L344-L361 + body.client = { + type: 'AND', + id: 'DBZUGRADARNETZ', + v: '', + } + body.ver = '1.18' + body.auth = { aid: 'hf7mcf9bv3nv8g5f', type:'AID' } + body.lang = 'deu' + + return body +} + +const sbbProfile = { + locale: 'de-CH', + timezone: 'Europe/Zurich', + // todo: find a way to use https: + endpoint: 'http://fahrplan.sbb.ch/bin/mgate.exe', + transformReqBody, + + products: products, + + trip: true, + radar: true, + reachableFrom: true, +} + +const withLocale = (locale) => ({...sbbProfile, locale}) + +sbbProfile.withLocale = withLocale +module.exports = sbbProfile diff --git a/p/sbb/products.js b/p/sbb/products.js new file mode 100644 index 00000000..4ca5b701 --- /dev/null +++ b/p/sbb/products.js @@ -0,0 +1,85 @@ +'use strict' + +module.exports = [ + { + id: 'express-train', + mode: 'train', + bitmasks: [1], + name: 'ICE/EN/CNL/ES/RJ/TGV/THA', + short: 'ICE/TGV/RJ', + default: true, + }, + { + id: 'international-train', + mode: 'train', + bitmasks: [2], + name: 'EC/IC/ICN/OEC', + short: 'EC/IC', + default: true, + }, + { + id: 'interregional-train', + mode: 'train', + bitmasks: [4], + name: 'IR/PE', + short: 'IR', + default: true, + }, + { + id: 'regional-express-train', + mode: 'train', + bitmasks: [8], + name: 'RE/D', + short: 'RE', + default: true, + }, + { + id: 'watercraft', + mode: 'watercraft', + bitmasks: [16], + name: 'Schiff/Fähre/Dampfschiff', + short: 'Schiff', + default: true, + }, + { + id: 'suburban-train', + mode: 'train', + bitmasks: [32], + name: 'S/SN/R/TER/RB', + short: 'S', + default: true, + }, + { + id: 'bus-taxi', + mode: 'bus', + bitmasks: [64], + name: 'Bus/Taxi', + short: 'Bus', + default: true, + }, + { + id: 'gondola', + mode: 'gondola', + // e.g. `FUN 13` ("Gurtenbahn") at Gurten Kulm (ID `8507099`) + bitmasks: [128], + name: 'Standseilbahn/Gondelbahn/Luftseilbahn/Sesselbahn', + short: 'Seilbahn/Zahnradbahn', + default: true, + }, + { + id: 'car-train', + mode: 'train', + bitmasks: [256], // todo: correct? + name: 'Autoreisezug/Extrazug', + short: 'ARZ/EXT', + default: true, + }, + { + id: 'tram', + mode: 'train', + bitmasks: [512], + name: 'Tram/Metro', + short: 'Tram/Metro', + default: true, + }, +] diff --git a/p/sbb/readme.md b/p/sbb/readme.md new file mode 100644 index 00000000..86f8697a --- /dev/null +++ b/p/sbb/readme.md @@ -0,0 +1,23 @@ +# SBB profile for `hafas-client` + +[*Schweizerische Bundesbahnen (SBB)*](https://en.wikipedia.org/wiki/Swiss_Federal_Railways) is the largest Swiss long-distance public transport company. This profile adds *SBB*-specific customizations to `hafas-client`. + +## Usage + +```js +const createClient = require('hafas-client') +const sbbProfile = require('hafas-client/p/sbb') + +// create a client with SBB profile +const client = createClient(sbbProfile, 'my-awesome-program') +``` + +By default, the `de_CH` locale is being used. You can customize it using `withLocale()`: + +```js +// create a client with SBB profile and fr_CH locale +const client = createClient(sbbProfile.withLocale('fr_CH'), 'my-awesome-program') +``` + + +## Customisations diff --git a/readme.md b/readme.md index 7b4a12b7..3c8cd676 100644 --- a/readme.md +++ b/readme.md @@ -206,6 +206,7 @@ Each of the [profiles](p) has more detailed example code. HAFAS endpoint | wrapper library | docs | example code | source code ---------------|------------------|------|---------|------------ [Deutsche Bahn (DB)](https://en.wikipedia.org/wiki/Deutsche_Bahn) | [`db-hafas`](https://github.com/public-transport/db-hafas) | [docs](p/db/readme.md) | [example code](p/db/example.js) | [src](p/db/index.js) +[Swiss Railways (SBB)](https://en.wikipedia.org/wiki/Swiss_Federal_Railways) | - | [docs](p/sbb/readme.md) | [example code](p/sbb/example.js) | [src](p/sbb/index.js) [Polskie Koleje Państwowe (PKP)](https://en.wikipedia.org/wiki/Polish_State_Railways) | [`pkp-hafas`](https://github.com/juliuste/pkp-hafas) | [docs](p/pkp/readme.md) | [example code](p/pkp/example.js) | [src](p/pkp/index.js) [Belgian National Railways (SNCB/NMBS)](https://en.wikipedia.org/wiki/National_Railway_Company_of_Belgium) | - | [docs](p/sncb/readme.md) | [example code](p/sncb/example.js) | [src](p/sncb/index.js) [*Iarnród Éireann* (Irish Rail)](https://en.wikipedia.org/wiki/Iarnród_Éireann) | - | [docs](p/irish-rail/readme.md) | [example code](p/irish-rail/example.js) | [src](p/irish-rail/index.js)