add rudimentary area queries

This commit is contained in:
Sanj 2012-09-11 16:28:35 +05:30
parent 7b5d3df2db
commit d9d2dacd85

View File

@ -19,17 +19,45 @@ STYLE = {"start": "", "repeat": "; ", "end": ""}
ChaloBest = arrest.Client("http://chalobest.in/1.0") ChaloBest = arrest.Client("http://chalobest.in/1.0")
def get_routes_for_matches(stops): def get_routes_for_matches(stops):
same_stops = [] # same_stops = []
same_stops.append(stops[0]) # same_stops.append(stops[0])
if len(stops) > 1: # if len(stops) > 1:
for s in stops[1:]: # for s in stops[1:]:
if s['properties']['official_name'] == stops[0]['properties']['official_name']: # if s['properties']['official_name'] == stops[0]['properties']['official_name']:
same_stops.append(s) # same_stops.append(s)
routes = [] routes = []
for stop in same_stops: for stop in stops:
routes.extend(stop['properties']['routes'].split(", ")) routes.extend(stop['properties']['routes'].split(", "))
return routes return routes
def get_stops_for_string(s):
stops = []
s = s.strip()
areas = ChaloBest.areas(q=s)
if len(areas) > 0:
for a in areas:
area = ChaloBest.area[a]
for stop in area['stops']['features']:
stops.append(stop)
return {
'name': ", ".join(areas),
'stops': stops
}
else:
stops_results = ChaloBest.stops(q=s)
if len(stops_results) == 0:
return None
same_stops = []
same_stops.append(stops[0])
if len(stops_results) > 1:
for s in stops_results[1:]:
if s['properties']['official_name'] == stops[0]['properties']['official_name']:
same_stops.append(s)
return {
'name': stops[0]['properties']['display_name'],
'stops': same_stops
}
class App(AppBase): class App(AppBase):
def handle(self, msg): def handle(self, msg):
if DIGIT.search(msg.text): if DIGIT.search(msg.text):
@ -37,6 +65,12 @@ class App(AppBase):
if not routes: if not routes:
msg.respond("Sorry, we found no route marked '%(text)s'.", text=msg.text) msg.respond("Sorry, we found no route marked '%(text)s'.", text=msg.text)
return return
detail = None
for route in routes:
if route.replace(" ", "").upper() == msg.text.replace(" ", "").upper():
detail = ChaloBest.route[route]
if detail == None:
detail = ChaloBest.route[routes[0]]
detail = ChaloBest.route[routes[0]] detail = ChaloBest.route[routes[0]]
stops = detail['stops']['features'] stops = detail['stops']['features']
origin, dest = stops[0]['properties'], stops[-1]['properties'] origin, dest = stops[0]['properties'], stops[-1]['properties']
@ -45,27 +79,32 @@ class App(AppBase):
msg.respond("%s: %s (%s) to %s (%s)" % ( msg.respond("%s: %s (%s) to %s (%s)" % (
",".join(routes), origin_name, origin_area, dest_name, dest_area)) ",".join(routes), origin_name, origin_area, dest_name, dest_area))
elif msg.text.find(" to ") != -1: elif msg.text.find(" to ") != -1:
stop1txt = msg.text.split("to")[0].strip()
stop2txt = msg.text.split("to")[1].strip() from_txt = msg.text.split("to")[0].strip()
stop1matches = ChaloBest.stops(q=stop1txt)['features'] to_txt = msg.text.split("to")[1].strip()
if not stop1matches:
msg.respond("Sorry, found no stop matching '%s'" % stop1txt) from_matches = get_stops_for_string(from_txt)
to_matches = get_stops_for_string(to_txt)
# stop1matches = ChaloBest.stops(q=stop1txt)['features']
if not from_matches:
msg.respond("Sorry, found no stop matching '%s'" % from_txt)
return return
best_match1 = stop1matches[0] #best_match1 = stop1matches[0]
routes1 = set(get_routes_for_matches(stop1matches)) routes1 = set(get_routes_for_matches(from_matches['stops']))
stop2matches = ChaloBest.stops(q=stop2txt)['features'] #stop2matches = ChaloBest.stops(q=stop2txt)['features']
if not stop2matches: if not to_matches:
msg.respond("Sorry, found no stop matching '%s'" % stop2txt) msg.respond("Sorry, found no stop matching '%s'" % to_txt)
best_match2 = stop2matches[0] #best_match2 = stop2matches[0]
routes2 = set(get_routes_for_matches(stop2matches)) routes2 = set(get_routes_for_matches(to_matches['stops']))
#routes1arr = set(routes1.split(", ")) #routes1arr = set(routes1.split(", "))
#routes2arr = set(routes2.split(", ")) #routes2arr = set(routes2.split(", "))
intersection = list(routes1.intersection(routes2)) intersection = list(routes1.intersection(routes2))
if len(intersection) == 0: if len(intersection) == 0:
msg.respond("Sorry, no direct buses found between %s and %s" % (best_match1['properties']['official_name'], best_match2['properties']['official_name'],)) msg.respond("Sorry, no direct buses found between %s and %s" % (from_matches['name'], to_matches['name'],))
return return
routesFound = ", ".join(intersection) routesFound = ", ".join(intersection)
msg.respond("%s to %s: %s" % (best_match1['properties']['official_name'], best_match2['properties']['official_name'], routesFound,)) msg.respond("%s to %s: %s" % (from_matches['name'], to_matches['name'], routesFound,))
return return