Skip to content

Commit

Permalink
Added script to conveniently query the api for a station id.
Browse files Browse the repository at this point in the history
  • Loading branch information
raywo committed Feb 6, 2018
1 parent af2578b commit f113628
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ jspm_packages
*.xml

*.iml
package-lock.json
87 changes: 87 additions & 0 deletions convenience/query_stations.js
Original file line number Diff line number Diff line change
@@ -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

This comment has been minimized.

Copy link
@derhuerst

derhuerst Feb 23, 2018

AFAICT you should add poi: false and addresses: false.

This comment has been minimized.

Copy link
@raywo

raywo Feb 23, 2018

Author Owner

Is this option new? I can’t remember seeing this before.

This comment has been minimized.

Copy link
@derhuerst

derhuerst Feb 23, 2018

It's not new.

};

client.locations(answer, opt).then((response) => {

This comment has been minimized.

Copy link
@derhuerst

derhuerst Feb 23, 2018

You might want to use db-stations-autocomplete and db-stations here.

There's an advantage: The HAFAS locations search is quite bad and quite slow.
There's a disadvantage: db-stations and db-stations-autocomplete fetch their data from the DB stations API, which does not contain every station found in HAFAS.

This comment has been minimized.

Copy link
@raywo

raywo Feb 23, 2018

Author Owner

This script exists only for convenience purposes and is not the main functionality of this product. So I probably won't use these APIs. But it’s good to know these exist. So thank you!

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(", ");
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit f113628

Please sign in to comment.