2 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
4 """Downloads Google Maps bus stop locations using the official mashup."""
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
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
)
25 return "loc (%s,%s)" % (self
.latitude
, self
.longitude
)
27 class BaseStop(object):
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
38 return "560 %s,%s,%s,%s" % (self
.number
, self
.code
,
39 self
.name
, self
.location
)
42 return "basestop (%s)" % self
.basestr()
45 """A regular bus stop."""
47 super(Stop
, self
).__init
__()
48 self
.routes
= [] # list of StopRoutes at this stop
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."""
57 super(Station
, self
).__init
__()
58 # code will be short (3- or 4-letter) station identifier
59 # name will be station name
62 return "station (%s)" % self
.basestr()
64 class UnknownStopType(BaseStop
):
65 def __init__(self
, location
, html
):
66 self
.location
= location
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
79 return "HOME: (asked %s, got %s, %s)" % (
80 self
.requestedAddress
, self
.respondedAddress
, self
.location
)
83 def __init__(self
, location
, html
):
84 self
.location
= location
88 return "marker (%s, %s)" % (self
.location
, self
.html
)
91 def findStops(self
, address
):
92 for marker
in self
._findMarkers
(self
._grabHTML
(address
)):
94 (_station_rx
, self
._parseStation
),
95 (_stop_rx
, self
._parseStop
),
96 (_home_rx
, self
._parseHome
)):
98 match
= rx
.search(marker
.html
)
100 ret
= func(address
, marker
, match
)
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
))
119 def _findMarkers(self
, html
):
120 for m
in _marker_rx
.finditer(html
):
121 yield Marker(StopLocation(m
.group("latitude"),
122 m
.group("longitude")),
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
):
133 self
._fillBaseStop
(marker
, match
, stop
)
136 def _parseStop(self
, address
, marker
, match
):
138 self
._fillBaseStop
(marker
, match
, stop
)
140 for m
in _stop_route_rx
.finditer(marker
.html
):
142 StopRoute(m
.group("routenum"), m
.group("direction")))
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' }
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
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
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
)
193 cmdstr
= " ".join(argv
[1:])
194 for s
in Client().findStops(cmdstr
):
197 if __name__
== '__main__':