Created
April 12, 2016 12:40
-
-
Save jonathanjuursema/c42cf164192885039aef7fd43cba27de to your computer and use it in GitHub Desktop.
This is a rudimentary Homey app for retrieving train schedules from the Dutch Railways, but it has never been made into an actual app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is a rudimentary Homey app for retrieving train schedules from the Dutch Railways, but it has never been made into an actual app. | |
* This gist has been created by Jonathan Juursema in Jan '15. You are free to use this code in your own project. | |
*/ | |
// Requirements | |
var prompt = require('prompt'); | |
var moment = require('moment-timezone'); | |
// NS API Credentials | |
var nsApi = require('ns-api'); | |
nsApi.username = 'username'; | |
nsApi.password = 'password'; | |
// Init variables | |
var nsHomeStation = null; | |
// Is user being asked for home station? | |
var askingForHomeStation = true; | |
// Load stations. | |
var nsStations; | |
nsApi.stations(function(err,data){ | |
nsStations = data; | |
iterate(); | |
}); | |
function validateStation(name) { | |
for (nsStationCode in nsStations) { | |
var nsStation = nsStations[nsStationCode]; | |
if (nsStation.Namen.Lang.toLowerCase() == name) { | |
return nsStation; | |
} | |
if (nsStation.Synoniemen != 0) { | |
if (typeof nsStation.Synoniemen.Synoniem === 'string') { | |
if (nsStation.Synoniemen.Synoniem.toLowerCase() == name) { | |
return nsStation; | |
} | |
} else { | |
for (nsSynoniem in nsStation.Synoniemen.Synoniem) { | |
if (nsStation.Synoniemen.Synoniem[nsSynoniem].toLowerCase() == name) { | |
return nsStation; | |
} | |
} | |
} | |
} | |
} | |
return false; | |
} | |
console.log('Wat is je voorkeursstation?'); | |
// Iterative function | |
function iterate() { | |
prompt.start(); | |
// Simulate text input | |
prompt.get('input', function(e,r) { | |
var input = r.input.toLowerCase().replace(/[^\w\s]/gi, ''); | |
// Quit simulation | |
if (input == 'quit') { | |
console.log("Okdoei!"); | |
process.exit(code=0); | |
} | |
// Test simulation | |
else if (input.indexOf('test') > -1) { | |
console.log("Pong!"); | |
} | |
// User is being asked for home station! | |
else if (askingForHomeStation == true) { | |
var validStation = validateStation(input); | |
if (!validStation) { | |
console.log('Dit is geen geldig station! Wat is je voorkeursstation?'); | |
} else { | |
nsHomeStation = validStation; | |
console.log('Je voorkeursstation is ingesteld op '+nsHomeStation.Namen.Lang+'!'); | |
askingForHomeStation = false; | |
} | |
iterate(); | |
} | |
// No home station set. | |
else if (nsHomeStation == null) { | |
console.log('Wat is je voorkeursstation?'); | |
askingForHomeStation == true;; | |
iterate(); | |
} | |
// See next train. | |
else if (input.indexOf('volgende trein') > -1) { | |
var input = input.split('volgende trein naar '); | |
var destination = input[1]; | |
destination = validateStation(destination); | |
if (!destination) { | |
console.log("Dit is geen geldig aankomststation!"); | |
} else { | |
nsApi.reisadvies( | |
{ | |
fromStation: nsHomeStation.Code, | |
toStation: destination.Code, | |
departure: true | |
}, | |
function( err, data ) { | |
var huidigeTijd = moment(); | |
for (nsVertrekken in data) { | |
var nsVertrek = data[nsVertrekken]; | |
var vertrekTijd = moment(nsVertrek.GeplandeVertrekTijd); | |
if (vertrekTijd.unix() > huidigeTijd.unix()) { | |
console.log("De volgende trein vertrekt om "+vertrekTijd.tz('Europe/Amsterdam').format('HH:mm')+" vanaf station "+nsHomeStation.Namen.Lang+" op spoor "+nsVertrek.ReisDeel[0].ReisStop[0].Spoor+"."); | |
if (nsVertrek.ReisDeel.length > 1) { | |
console.log("Voor deze reis moet je "+(nsVertrek.ReisDeel.length-1)+" keer overstappen."); | |
for (nsReisdeel in nsVertrek.ReisDeel) { | |
if (nsReisdeel == 0) continue; | |
console.log("Op "+nsVertrek.ReisDeel[nsReisdeel].ReisStop[0].Naam+" stap je over naar spoor "+nsVertrek.ReisDeel[nsReisdeel].ReisStop[0].Spoor+", vertrek om "+moment(nsVertrek.ReisDeel[nsReisdeel].ReisStop[0].Tijd).tz('Europe/Amsterdam').format('HH:mm')+"."); | |
} | |
} | |
console.log("Je arriveert dan op "+destination.Namen.Lang+" om "+moment(nsVertrek.GeplandeAankomstTijd).tz('Europe/Amsterdam').format('HH:mm')+"."); | |
break; | |
} | |
} | |
iterate(); | |
} | |
); | |
} | |
} | |
// Fallback | |
else { | |
console.log("Asjemenou?"); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment