From f113628af662651c7f1f4781693519b309b65fdf Mon Sep 17 00:00:00 2001 From: Ray Wojciechowski Date: Tue, 6 Feb 2018 22:06:00 +0100 Subject: [PATCH] Added script to conveniently query the api for a station id. --- .gitignore | 1 + convenience/query_stations.js | 87 +++++++++++++++++++++++++++++++++++ package.json | 4 +- 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 convenience/query_stations.js diff --git a/.gitignore b/.gitignore index 7ce101c..71c796d 100644 --- a/.gitignore +++ b/.gitignore @@ -91,3 +91,4 @@ jspm_packages *.xml *.iml +package-lock.json diff --git a/convenience/query_stations.js b/convenience/query_stations.js new file mode 100644 index 0000000..c08041e --- /dev/null +++ b/convenience/query_stations.js @@ -0,0 +1,87 @@ +"use strict"; + +const createClient = require('hafas-client'); +const dbProfile = require('hafas-client/p/db'); +const client = createClient(dbProfile); + +const readline = require('readline'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +rl.question("Geben Sie eine Adresse oder einen Stationsnamen ein: ", (answer) => { + rl.close(); + + const opt = { + results: 10, + stations: true + }; + + client.locations(answer, opt).then((response) => { + console.log("\nGefundene Haltestellen für \"" + answer + "\":\n"); + + response.map((element) => { + printStationInfo(element); + }); + + process.exit(0); + }).catch(console.error); +}); + + +function printStationInfo(element) { + let id = element.id; + let name = element.name; + let products = element.products; + + if (id && name) { + console.log("> Haltestelle: \"" + name + "\"\n ID: " + id + "\n " + refineProducts(products) + "\n"); + } +} + + +function refineProducts(products) { + let result = "Verkehrsmittel: "; + + if (!products) { + return result + "keine"; + } + + let productNames = []; + + if (products.national || products.nationalExp) { + productNames.push("Fernverkehr"); + } + + if (products.regional) { + productNames.push("Regionalverkehr"); + } + + if (products.suburban) { + productNames.push("S-Bahn"); + } + + if (products.bus) { + productNames.push("Bus"); + } + + if (products.subway) { + productNames.push("U-Bahn"); + } + + if (products.tram) { + productNames.push("Tram"); + } + + if (products.ferry) { + productNames.push("Fähre"); + } + + if (products.taxi) { + productNames.push("Taxi"); + } + + return result + productNames.join(", "); +} diff --git a/package.json b/package.json index 3fe8f3d..d0b9230 100644 --- a/package.json +++ b/package.json @@ -29,9 +29,9 @@ "node": ">=6" }, "dependencies": { - "db-hafas": "^2.1.0", "lodash.isobject": "^3.0.2", "lvb": "0.0.2", - "stylelint": "^8.4.0" + "stylelint": "^8.4.0", + "hafas-client": "latest" } }