Skip to content

Instantly share code, notes, and snippets.

@pdxmph
Last active December 23, 2015 02:29
Show Gist options
  • Select an option

  • Save pdxmph/6567341 to your computer and use it in GitHub Desktop.

Select an option

Save pdxmph/6567341 to your computer and use it in GitHub Desktop.
So, it'd be nice if I could get an unsolicited ping about upcoming Max arrivals at my two most used stations in the morning, when I'm getting ready to go to work, and in the afternoon when it's almost time to go. It'd also be nice if there were a way to tell the unsolicited ping to shut up. This does that using an ifttt recipe (so I can append a…
#!/usr/bin/env ruby
require 'json'
require 'time'
require 'net/http'
require 'pushover'
# trimet stop ids for easy expansion to other stops
stops = {"lents" => 13135, "union" => 7763}
# take argv to run as a cron job - pass one of the keys above
stop_arg = ARGV[0]
# for testing, comment previous line out and use this line
# stop_arg = "lents"
# base trimet API url
base_url = "http://developer.trimet.org/ws/V1/arrivals"
# our trimet app id
appID = ""
# where we keep our pipe-delimited ifttt log
# this is the file we write our pings to via SMS, chatbot or whatever
# they'll read as "#{ping text}|#{time ping arrived}"
# ping text should be one of a stop name found in the stops hash above, or "mute" to tell the script to exit right away
# "mute" and station pings are valid for 30 minutes, then the script resumes normal operation
pingfile = ""
# Pushover API info
pushover_api = ""
pushover_usr = ""
# Get the ping file and do stuff with it
checkfile = `curl -Ls #{pingfile}`
last_ping = checkfile.lines.to_a.last.split("|")
ping_time = Time.parse(last_ping[1])
ping_note = last_ping[0]
ping_ago = (Time.now - ping_time)/60
# We might have told the script to shut up or override the stop recently
if ping_ago < 30 && ping_note == "mute"
exit
elsif ping_ago < 30 && ping_note != "mute"
stop_arg = ping_note.downcase
end
# Get our stop id from the stops hash
stop_id = stops[stop_arg]
# Set up our Pushover instance
Pushover.configure do |config|
config.user = pushover_usr
config.token = pushover_api
end
# Get the stop data from TriMet and do stuff with it
url = "#{base_url}?appID=#{appID}&json=true&locIDs=#{stop_id}"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
result = JSON.parse(data)
station = result['resultSet']['location'][0]['desc']
arrivals = result['resultSet']['arrival']
upcoming = arrivals.select { |a| a['locid'] == stop_id && a['route'] == 200 }.first
route = upcoming['shortSign']
time_diff = ((Time.parse(upcoming['estimated']) - Time.now)/60).to_i
# Think about what we want to say
message = "Next #{route} in #{time_diff} minutes"
# Tell Pushover
Pushover.notification(message: message, sound: 'tugboat', url: "pdxbus://#{stop_id}", url_title: 'Stop Information')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment