2012-02-28 16:59:30 +00:00
|
|
|
from rapidsms.apps.base import AppBase
|
2012-02-28 18:10:22 +00:00
|
|
|
import re
|
|
|
|
import arrest
|
|
|
|
|
2012-02-28 19:48:28 +00:00
|
|
|
MAX_MSG_LEN = 160
|
2012-02-28 18:10:22 +00:00
|
|
|
DIGIT = re.compile(r"\d{1,3}")
|
2012-02-28 20:03:35 +00:00
|
|
|
PUNCT = re.compile(r"[^\w\s]")
|
2012-02-28 20:40:48 +00:00
|
|
|
"""
|
2012-02-28 20:03:35 +00:00
|
|
|
STYLE = {
|
2012-02-28 20:15:32 +00:00
|
|
|
"start": "-* ",
|
|
|
|
"repeat": " -*- ",
|
|
|
|
"end": " *-"
|
2012-02-28 20:03:35 +00:00
|
|
|
}
|
2012-02-28 20:40:48 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
STYLE = {"start": "", "repeat": "; ", "end": ""}
|
2012-02-28 20:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
ChaloBest = arrest.Client("http://chalobest.in/1.0")
|
2012-02-28 16:59:30 +00:00
|
|
|
|
2012-09-06 13:36:06 +00:00
|
|
|
def get_routes_for_matches(stops):
|
2012-09-11 10:58:35 +00:00
|
|
|
# same_stops = []
|
|
|
|
# same_stops.append(stops[0])
|
|
|
|
# if len(stops) > 1:
|
|
|
|
# for s in stops[1:]:
|
|
|
|
# if s['properties']['official_name'] == stops[0]['properties']['official_name']:
|
|
|
|
# same_stops.append(s)
|
2012-09-06 13:36:06 +00:00
|
|
|
routes = []
|
2012-09-11 10:58:35 +00:00
|
|
|
for stop in stops:
|
2012-09-06 13:49:08 +00:00
|
|
|
routes.extend(stop['properties']['routes'].split(", "))
|
2012-09-06 13:36:06 +00:00
|
|
|
return routes
|
|
|
|
|
2012-09-11 10:58:35 +00:00
|
|
|
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:
|
2012-09-11 11:07:14 +00:00
|
|
|
stops_results = ChaloBest.stops(q=s)['features']
|
2012-09-11 10:58:35 +00:00
|
|
|
if len(stops_results) == 0:
|
|
|
|
return None
|
|
|
|
same_stops = []
|
2012-09-11 11:13:22 +00:00
|
|
|
same_stops.append(stops_results[0])
|
2012-09-11 10:58:35 +00:00
|
|
|
if len(stops_results) > 1:
|
|
|
|
for s in stops_results[1:]:
|
2012-09-11 11:13:22 +00:00
|
|
|
if s['properties']['official_name'] == same_stops[0]['properties']['official_name']:
|
2012-09-11 10:58:35 +00:00
|
|
|
same_stops.append(s)
|
|
|
|
return {
|
2012-09-11 11:13:22 +00:00
|
|
|
'name': same_stops[0]['properties']['display_name'],
|
2012-09-11 10:58:35 +00:00
|
|
|
'stops': same_stops
|
|
|
|
}
|
|
|
|
|
2012-02-28 16:59:30 +00:00
|
|
|
class App(AppBase):
|
2012-02-28 18:10:22 +00:00
|
|
|
def handle(self, msg):
|
|
|
|
if DIGIT.search(msg.text):
|
2012-02-28 20:03:35 +00:00
|
|
|
routes = ChaloBest.routes(q=msg.text.replace(" ", ""))
|
2012-02-28 21:08:53 +00:00
|
|
|
if not routes:
|
2012-03-10 10:42:02 +00:00
|
|
|
msg.respond("Sorry, we found no route marked '%(text)s'.", text=msg.text)
|
2012-02-28 20:15:32 +00:00
|
|
|
return
|
2012-09-11 10:58:35 +00:00
|
|
|
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]]
|
2012-02-28 21:08:53 +00:00
|
|
|
detail = ChaloBest.route[routes[0]]
|
|
|
|
stops = detail['stops']['features']
|
2012-02-28 20:40:48 +00:00
|
|
|
origin, dest = stops[0]['properties'], stops[-1]['properties']
|
|
|
|
origin_name, dest_name = origin['display_name'], dest['display_name']
|
|
|
|
origin_area, dest_area = PUNCT.sub('', origin['area']), PUNCT.sub('', dest['area'])
|
2012-02-28 20:31:09 +00:00
|
|
|
msg.respond("%s: %s (%s) to %s (%s)" % (
|
2012-02-28 20:40:48 +00:00
|
|
|
",".join(routes), origin_name, origin_area, dest_name, dest_area))
|
2012-09-06 12:53:08 +00:00
|
|
|
elif msg.text.find(" to ") != -1:
|
2012-09-11 10:58:35 +00:00
|
|
|
|
|
|
|
from_txt = msg.text.split("to")[0].strip()
|
|
|
|
to_txt = msg.text.split("to")[1].strip()
|
|
|
|
|
|
|
|
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)
|
2012-09-06 12:53:08 +00:00
|
|
|
return
|
2012-09-11 10:58:35 +00:00
|
|
|
#best_match1 = stop1matches[0]
|
|
|
|
routes1 = set(get_routes_for_matches(from_matches['stops']))
|
|
|
|
#stop2matches = ChaloBest.stops(q=stop2txt)['features']
|
|
|
|
if not to_matches:
|
|
|
|
msg.respond("Sorry, found no stop matching '%s'" % to_txt)
|
|
|
|
#best_match2 = stop2matches[0]
|
|
|
|
routes2 = set(get_routes_for_matches(to_matches['stops']))
|
2012-09-06 13:44:02 +00:00
|
|
|
#routes1arr = set(routes1.split(", "))
|
|
|
|
#routes2arr = set(routes2.split(", "))
|
|
|
|
intersection = list(routes1.intersection(routes2))
|
2012-09-06 12:53:08 +00:00
|
|
|
if len(intersection) == 0:
|
2012-09-11 10:58:35 +00:00
|
|
|
msg.respond("Sorry, no direct buses found between %s and %s" % (from_matches['name'], to_matches['name'],))
|
2012-09-06 12:53:08 +00:00
|
|
|
return
|
|
|
|
routesFound = ", ".join(intersection)
|
2012-09-11 10:58:35 +00:00
|
|
|
msg.respond("%s to %s: %s" % (from_matches['name'], to_matches['name'], routesFound,))
|
2012-09-06 12:53:08 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
|
2012-02-28 18:10:22 +00:00
|
|
|
else:
|
2012-02-28 20:56:10 +00:00
|
|
|
features = ChaloBest.stops(q=msg.text)['features']
|
|
|
|
if not features:
|
2012-03-10 10:42:02 +00:00
|
|
|
msg.respond("Sorry, we found no stops marked '%(text)s'.", text=msg.text)
|
2012-02-28 20:15:32 +00:00
|
|
|
return
|
2012-02-28 20:56:10 +00:00
|
|
|
stops = []
|
|
|
|
for feat in features:
|
|
|
|
stop = feat['properties']
|
|
|
|
if stops and stop["official_name"] == stops[-1]["official_name"]:
|
|
|
|
stops[-1]["routes"] += ", " + stop["routes"]
|
|
|
|
else:
|
|
|
|
stops.append(stop)
|
2012-02-28 20:03:35 +00:00
|
|
|
response = STYLE["start"]
|
2012-02-28 20:56:10 +00:00
|
|
|
for stop in stops:
|
|
|
|
match = stop["official_name"] + ": " + stop["routes"]
|
2012-02-28 20:31:09 +00:00
|
|
|
if len(response) > len(STYLE["repeat"]): response += STYLE["repeat"]
|
|
|
|
response += match
|
|
|
|
if len(response) > MAX_MSG_LEN: break
|
|
|
|
if len(response) > MAX_MSG_LEN:
|
|
|
|
response = response[:MAX_MSG_LEN-(len(STYLE["end"])+4)] + "..."
|
2012-02-28 20:03:35 +00:00
|
|
|
response += STYLE["end"]
|
2012-02-28 19:48:28 +00:00
|
|
|
msg.respond(response)
|