Skip to content

Instantly share code, notes, and snippets.

@hc5duke
Created May 25, 2009 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hc5duke/117354 to your computer and use it in GitHub Desktop.
Save hc5duke/117354 to your computer and use it in GitHub Desktop.
bart schedule for pleasanton-embarcadero, using ruby on apache
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>Bart</title>
<style type="text/css" media="screen">
div {max-width:300px;}
div.destination {font-size:24px;font-weight:bold;border:2px solid #00c;text-align:center;margin:0px -2px;}
div.estimates {padding-bottom:15px;}
div.good {background-color:#8d8;}
div.ignore {opacity:0.3;}
div.opposite {opacity:0.7; background-color:#f88;}
span.red {color:#c00;font-weight:bold;}
</style>
</head>
<body>
<%
require 'rubygems'
require 'open-uri'
require 'hpricot'
require 'json'
times = {}
cars = {}
#doc = Hpricot(open("http://www.bart.gov/dev/eta/bart_eta.xml"))
doc = Hpricot(open("bart_eta.xml"))
stations = ["EMBR", "MONT","POWL", "CIVC", "16TH"]
st_diffs = {"EMBR" => 0, "MONT" => 2, "POWL" => 4, "CIVC" => 5, "16TH" => 7}
inbound = [
"Daly City",
"SF Airport",
"Millbrae",
"SFO/Millbrae"
]
outbound = [
"Richmond",
"Pittsburg/Bay Point",
"Dublin/Pleasanton",
"Fremont",
]
stations.each do |st|
s = doc.search("station[text()*='#{st}']")
(s/"eta").each do |e|
dest = (e/"destination").inner_html
time = (e/"estimate").inner_html.split(',').map &:strip
time = time.map do |value|
arr = value.match(/\d+/)
arr ? arr[0].to_i : 0
end
times[st] ||= {}
times[st][dest] = time
end
end
#times.each_pair do |s_key, value|
stations.each do |st|
times[st].each_pair do |d_key, dest|
puts "<br/>#{st} : #{d_key} : #{dest.to_json}"
cars_d = cars[d_key] ||= []
diff = st_diffs[st].to_i
diff = -diff if inbound.include? d_key
dest.each do |d|
dx = d + diff
if !(cars_d.index(dx-1) || cars_d.index(dx) || cars_d.index(dx+1))
cars_d << dx
end
end
cars_d.sort!
end
end
cars.each_pair do |d_key, value|
puts "#{d_key} >> #{value.to_json} <br/>"
end
%>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment