Fix totally broken longitude calculation.
[ottawa-travel-planner.git] / BusStopMashup.py
blobbd65b0f9e28d9d1bca6caef9f91117c8ef6a8455
2 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
4 """Downloads Google Maps bus stop locations using the official mashup."""
6 import urllib
7 import urllib2
8 import sys
9 import re
11 class StopRoute(object):
12 def __init__(self, number, direction):
13 self.number = number # route number
14 self.direction = direction # route direction, seems to be 10 or 11
16 def __str__(self):
17 return "route(%s,%s)" % (self.number, self.direction)
19 class StopLocation(object):
20 def __init__(self, latitude, longitude):
21 self.latitude = float(latitude)
22 self.longitude = float(longitude)
24 def __str__(self):
25 return "loc (%s,%s)" % (self.latitude, self.longitude)
27 class BaseStop(object):
28 def __init__(self):
29 self.number = None # 560-1000 number
30 # This is not necessarily unique - there
31 # are multiple stops #3034, for each
32 # platform at Billings Bridge
33 self.code = None # e.g. WD360 - looks to be unique
34 self.name = None # usually an intersection
35 self.location = None
37 def basestr(self):
38 return "560 %s,%s,%s,%s" % (self.number, self.code,
39 self.name, self.location)
41 def __str__(self):
42 return "basestop (%s)" % self.basestr()
44 class Stop(BaseStop):
45 """A regular bus stop."""
46 def __init__(self):
47 super(Stop, self).__init__()
48 self.routes = [] # list of StopRoutes at this stop
50 def __str__(self):
51 return "stop (%s): %s" % (
52 self.basestr(), ", ".join([str(r) for r in self.routes]))
54 class Station(BaseStop):
55 """A Transitway or O-Train station."""
56 def __init__(self):
57 super(Station, self).__init__()
58 # code will be short (3- or 4-letter) station identifier
59 # name will be station name
61 def __str__(self):
62 return "station (%s)" % self.basestr()
64 class UnknownStopType(BaseStop):
65 def __init__(self, location, html):
66 self.location = location
67 self.html = html
69 def __str__(self):
70 return "UNKNOWN (%s, %s)" % (self.location, self.html)
72 class HomeLocation(BaseStop):
73 def __init__(self, location, requestedAddress, respondedAddress):
74 self.location = location
75 self.requestedAddress = requestedAddress
76 self.respondedAddress = respondedAddress
78 def __str__(self):
79 return "HOME: (asked %s, got %s, %s)" % (
80 self.requestedAddress, self.respondedAddress, self.location)
82 class Marker(object):
83 def __init__(self, location, html):
84 self.location = location
85 self.html = html
87 def __str__(self):
88 return "marker (%s, %s)" % (self.location, self.html)
90 class Client(object):
91 def findStops(self, address):
92 for marker in self._findMarkers(self._grabHTML(address)):
93 for (rx, func) in (
94 (_station_rx, self._parseStation),
95 (_stop_rx, self._parseStop),
96 (_home_rx, self._parseHome)):
98 match = rx.search(marker.html)
99 if match is not None:
100 ret = func(address, marker, match)
101 if ret is not None:
102 yield ret
103 break
104 if match is None:
105 yield UnknownStopType(marker.location, marker.html)
107 def _grabHTML(self, address):
108 params = { 'address': address }
109 params.update(FIXED_PARAMS)
110 f = urllib2.urlopen(URL, urllib.urlencode(params))
112 html = ""
113 for line in f:
114 html += line
115 f.close()
117 return html
119 def _findMarkers(self, html):
120 for m in _marker_rx.finditer(html):
121 yield Marker(StopLocation(m.group("latitude"),
122 m.group("longitude")),
123 m.group("html"))
125 def _fillBaseStop(self, marker, match, stop):
126 stop.number = match.group("stopnum")
127 stop.code = match.group("code")
128 stop.name = match.group("name")
129 stop.location = marker.location
131 def _parseStation(self, address, marker, match):
132 stop = Station()
133 self._fillBaseStop(marker, match, stop)
134 return stop
136 def _parseStop(self, address, marker, match):
137 stop = Stop()
138 self._fillBaseStop(marker, match, stop)
140 for m in _stop_route_rx.finditer(marker.html):
141 stop.routes.append(
142 StopRoute(m.group("routenum"), m.group("direction")))
143 return stop
145 def _parseHome(self, address, marker, match):
146 return HomeLocation(marker.location, address, match.group("homeaddr"))
148 URL = "http://www.octranspo.com/maps/busstops/imap.asp"
149 FIXED_PARAMS = { 'page': 'search' }
151 # Any type of marker
152 # var marker = createMarker(new GPoint(..., ...), "blah blah");
153 # note: gmaps api v1 uses GPoint(long, lat) instead of GLatLng(lat, long)
154 _marker_re = (r'createMarker\(new GPoint\('
155 r'(?P<longitude>[^,]+),\s*(?P<latitude>[^,]+)\),\s*'
156 # ?s: DOTALL: . matches \n
157 # *? is non-greedy
158 r'"(?s)(?P<html>.*?)"\);\r?\n')
159 _marker_rx = re.compile(_marker_re)
162 # Marker for a transitway station
163 # <span><strong><b>613-560-1000 plus <a href='iframe.asp?route=busstop&INFO_PHONE=3034' target='iframe'>3034</a><br><a href='iframe.asp?route=bus_station&INFO_PHONE=3034&station_name=BILLINGS BRIDGE&station_id=BIB' target='iframe'>BILLINGS BRIDGE</b></strong></span>
164 # (?i): Make it case-insensitive
165 # For station name, assume they'll URL-encode it properly someday
166 _station_re = (r'(?i)INFO_PHONE=(?P<stopnum>\w+)'
167 r'.*station_name=(?P<name>[\w\s%+]+)'
168 r'.*station_id=(?P<code>\w+)')
169 _station_rx = re.compile(_station_re)
171 # Marker for a regular stop
172 # <span><strong><b>613-560-1000 plus <a href='iframe.asp?route=busstop&INFO_PHONE=4897' target='iframe'>4897</a></b></strong><small> (RA040)</small><br>BANK / TRANSITWAY<br><a href='iframe.asp?route=1&dir=10' target='iframe'>1</a> <a href='iframe.asp?route=5&dir=10' target='iframe'>5</a> <a href='iframe.asp?route=111&dir=10' target='iframe'>111</a> <a href='iframe.asp?route=141&dir=11' target='iframe'>141</a> <a href='iframe.asp?route=148&dir=10' target='iframe'>148</a> </span>
173 _stop_re = (r'(?i)INFO_PHONE=(?P<stopnum>\w+)'
174 r'.*\((?P<code>[A-Z]{2,3}[0-9]{3})\)'
175 # The stop name could be BLAH, BLAH / BLAH, BLAH STOP/ARR?T 1A.
176 # Look for some English letters and numbers at the start, then let
177 # anything in.
178 r'.*\<br\>(?P<name>\w[^<>]+)\<br\>')
179 _stop_rx = re.compile(_stop_re)
181 _stop_route_re = (r'(?i)route=(?P<routenum>\w+)&dir=(?P<direction>\w+)')
182 _stop_route_rx = re.compile(_stop_route_re)
184 # Marker for the current location
185 # <span><strong>HERON RD & DATA CENTRE RD, OTTAWA, ON, CANADA</strong></span>"
186 _home_re = (r'(?i)(?P<homeaddr>[^<>]+), CANADA')
187 _home_rx = re.compile(_home_re)
189 def main(argv=None):
190 if argv is None:
191 argv = sys.argv
193 cmdstr = " ".join(argv[1:])
194 for s in Client().findStops(cmdstr):
195 print s
197 if __name__ == '__main__':
198 sys.exit(main())