Last active
December 21, 2015 13:38
-
-
Save JamieMagee/6313662 to your computer and use it in GitHub Desktop.
A hubot script to fetch live departure times from the National Rail website
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
# Description: | |
# Get National Rail live departure information | |
# | |
# Dependencies: | |
# None | |
# | |
# Configuration: | |
# HUBOT_DEFAULT_STATION - set the default from station (nearest to your home/office) | |
# | |
# Commands: | |
# hubot: trains <departure station> (to) <arrival station> | |
# hubot: trains <arrival station> | |
# hubot: trains <departure station> to - lists next 5 departures | |
# | |
# Notes: | |
# Use the station code (https://en.wikipedia.org/wiki/UK_railway_stations) | |
# | |
# Author: | |
# JamieMagee | |
module.exports = (robot) -> | |
robot.respond /trains (\w{3})( (to)*(.*))*/i, (msg) -> | |
trainFrom = if !!msg.match[4] then msg.match[1].trim().toUpperCase() else process.env.HUBOT_DEFAULT_STATION | |
trainTo = if !!msg.match[4] then msg.match[4].trim().toUpperCase() else msg.match[1].trim().toUpperCase() | |
msg.http('http://ojp.nationalrail.co.uk/service/ldb/liveTrainsJson') | |
.query({departing: 'true', liveTrainsFrom: trainFrom, liveTrainsTo: trainTo}) | |
.get() (err, res, body) -> | |
stuff = JSON.parse(body) | |
if stuff.trains.length | |
msg.reply "Next trains from: #{trainFrom} to #{trainTo}" | |
for key, value of stuff.trains | |
if key < 5 | |
info = "#{value}".split "," | |
if !!info[4] | |
msg.send "The #{info[1]} to #{info[2]} at platform #{info[4]} is #{/[^;]*$/.exec(info[3])[0].trim().toLowerCase()}" | |
else | |
msg.send "The #{info[1]} to #{info[2]} is #{/[^;]*$/.exec(info[3])[0].trim().toLowerCase()}" | |
else | |
msg.reply "I couldn't find trains from: #{trainFrom} to #{trainTo}" | |
msg.send "Please check the station codes https://en.wikipedia.org/wiki/UK_railway_stations" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment