From 582e5d0ef7610d7592837eebd69141ee9e6bad90 Mon Sep 17 00:00:00 2001 From: Johannes Filter Date: Mon, 26 Feb 2018 22:14:11 +0100 Subject: [PATCH] add minimal working INSA endpoint --- p/insa/example.js | 22 +++++++++++++ p/insa/index.js | 62 +++++++++++++++++++++++++++++++++++ p/insa/products.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++ p/insa/readme.md | 18 +++++++++++ 4 files changed, 183 insertions(+) create mode 100644 p/insa/example.js create mode 100644 p/insa/index.js create mode 100644 p/insa/products.js create mode 100644 p/insa/readme.md diff --git a/p/insa/example.js b/p/insa/example.js new file mode 100644 index 00000000..e56e3d1b --- /dev/null +++ b/p/insa/example.js @@ -0,0 +1,22 @@ +'use strict' + +const createClient = require('../..') +const insaProfile = require('.') + +const client = createClient(insaProfile) + +// from Magdeburg-Neustadt to Magdeburg-Buckau +client.journeys('008010226', '008013456', {results: 1}) +// client.departures('008010226', {duration: 5}) +// client.locations('Magdeburg', {results: 2}) +// client.location('008010226') // Magdeburg-Neustadt +// client.nearby({ +// type: 'location', +// latitude: 52.148842, +// longitude: 11.641705 +// }, {distance: 200}) + +.then((data) => { + console.log(require('util').inspect(data, {depth: null})) +}) +.catch(console.error) diff --git a/p/insa/index.js b/p/insa/index.js new file mode 100644 index 00000000..8102e4ca --- /dev/null +++ b/p/insa/index.js @@ -0,0 +1,62 @@ +'use strict' + +const products = require('./products') +const createParseBitmask = require('../../parse/products-bitmask') +const createFormatBitmask = require('../../format/products-bitmask') + +const defaultProducts = { + nationalExp: true, + national: true, + regional: true, + suburban: true, + bus: true, + tram: true, + tourismTrain: true, +} + + +const transformReqBody = (body) => { + body.client = { + type: 'IPH', + id: 'NASA', + v: '4000200', + name: 'nasaPROD', + os: 'iPhone OS 11.2.5' + } + body.ver = '1.11' + body.auth = {aid: "nasa-apps"} + body.lang = 'en' // todo: `de`? + + return body +} + + + +const formatProducts = (products) => { + products = Object.assign(Object.create(null), defaultProducts, products) + return { + type: 'PROD', + mode: 'INC', + value: formatBitmask(products) + '' + } +} + +const formatBitmask = createFormatBitmask(products) + + +const insaProfile = { + locale: 'de-DE', + timezone: 'Europe/Berlin', + endpoint: 'http://reiseauskunft.insa.de/bin/mgate.exe', + products: products.allProducts, + transformReqBody, + + defaultProducts, + parseProducts: createParseBitmask(products.bitmasks), + formatProducts + + // todo: journeyLeg? + // todo: radar? +} + +module.exports = insaProfile; diff --git a/p/insa/products.js b/p/insa/products.js new file mode 100644 index 00000000..81cb82f7 --- /dev/null +++ b/p/insa/products.js @@ -0,0 +1,81 @@ +'use strict' + +// TODO Jannis R.: DRY +const p = { + nationalExp: { + bitmask: 1, + name: 'InterCityExpress', + short: 'ICE', + mode: 'train', + product: 'nationalExp' + }, + national: { + bitmask: 2, + name: 'InterCity & EuroCity', + short: 'IC/EC', + mode: 'train', + product: 'national' + }, + regional: { + bitmask: 8, + name: 'RegionalExpress & RegionalBahn', + short: 'RE/RB', + mode: 'train', + product: 'regional' + }, + suburban: { + bitmask: 16, + name: 'S-Bahn', + short: 'S', + mode: 'train', + product: 'suburban' + }, + tram: { + bitmask: 32, + name: 'Tram', + short: 'T', + mode: 'train', + product: 'tram' + }, + bus: { + bitmask: 64+128, + name: 'Bus', + short: 'B', + mode: 'bus', + product: 'bus' + }, + tourismTrain: { + bitmask: 256, + name: 'Tourism Train', + short: 'TT', + mode: 'train', + product: 'tourismTrain' + }, + unknown: { + bitmask: 0, + name: 'unknown', + short: '?', + product: 'unknown' + } +} + +p.bitmasks = [] +p.bitmasks[1] = p.nationalExp +p.bitmasks[2] = p.national +p.bitmasks[8] = p.regional +p.bitmasks[16] = p.suburban +p.bitmasks[32] = p.tram +p.bitmasks[64+128] = p.bus +p.bitmasks[256] = p.tourismTrain + +p.allProducts = [ + p.nationalExp, + p.national, + p.regional, + p.suburban, + p.tram, + p.bus, + p.tourismTrain +] + +module.exports = p diff --git a/p/insa/readme.md b/p/insa/readme.md new file mode 100644 index 00000000..4918b2b4 --- /dev/null +++ b/p/insa/readme.md @@ -0,0 +1,18 @@ +# INSA profile for `hafas-client` + +The [Nahverkehr Sachsen-Anhalt (NASA)](https://de.wikipedia.org/wiki/Nahverkehrsservice_Sachsen-Anhalt) offers [Informationssystem Nahverkehr Sachsen-Anhalt (INSA)](https://insa.de) to distribute their public transport data. + +## Usage + +```js +const createClient = require('hafas-client') +const insaProfile = require('hafas-client/p/insa') + +// create a client with INSA profile +const client = createClient(insaProfile) +``` + + +## Customisations + +- parses *INSA*-specific products (such as *Tourism Train*)