Skip to content

Instantly share code, notes, and snippets.

@byteme206
Forked from mapledyne/oba.rb
Last active November 19, 2015 00:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save byteme206/de33ee9a625acaaf9a80 to your computer and use it in GitHub Desktop.
Local bus info widget in Dashing
require 'rubygems'
require 'nokogiri'
require 'open-uri'
## Forked from https://gist.github.com/mapledyne/4d3e417f2fef2d1ab83d#file-oba-rb
#
# To use, add an instance of the Meter widget to your dashboard for each route using the naming convention
# 'oba_<ROUTE>'. The <ROUTE> should match the display values for the routes you add to the rides hash below.
# Set walktime to the average walking time to the closest station(s) for the given routes
walktime = 5
# Add a new hash k/v pair for each route and stop
# The key name will be the display value for the corresponding widget instance.
# The values should be a two-element array, where the first value represents the
# stop ID and the second value represents the route ID.
# The easiest way to determine these values is to use the One Bus Away web interface to
# search on route number, then click on the appropriate stop, followed by "Real Time Arrival Info.
# The values will be displayed in the URL query parameters.
rides = {
"Link" => ["1_1108", "40_100479"],
"C-Line" => ["1_431", "1_102576"],
"E-Line" => ["1_578", "1_102615"],
"522" => ["1_682", "40_100232"],
"413" => ["1_970", "29_413"],
"511" => ["1_700", "29_511"],
"41" => ["1_1121", "1_100221"],
"312" => ["1_682", "1_100187"],
"157" => ["1_300", "1_100475"],
}
soonest = 15
SCHEDULER.every '1m', :first_in => '30s' do |job|
rides.each_pair do |k, v|
geturl = "http://pugetsound.onebusaway.org/where/sign/stop.action?id=#{v[0]}&route=#{v[1]}"
response = Nokogiri::HTML(open(geturl))
buses = response.xpath("//td[contains(@class,'arrivalsStatusEntry')]/span/text()")
buses.each do |bus|
if bus.to_s == "NOW"
next
end
b = bus.to_s.to_i - walktime
if b < 0
next
end
nextbus = [soonest,b].min.to_i
send_event("oba_#{k}", {title: "Next #{k}", value: nextbus})
end
end
end
@byteme206
Copy link
Author

Some small modifications to support multiple bus routes... useful if you have a larger number of people in your office. If you're playing with Dashing, you'll see that a lot of widgets use a similar method to make their feed jobs more flexible.

Don't panic if you deploy this and notice that some of the widgets are 'blank,' as this is how the data source from OneBusAway works. If a route is rush-hour-only, it will not return any data until the route itself is in service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment