from rapidsms.apps.base import AppBase import re import arrest MAX_MSG_LEN = 160 DIGIT = re.compile(r"\d{1,3}") PUNCT = re.compile(r"[^\w\s]") """ STYLE = { "start": "-* ", "repeat": " -*- ", "end": " *-" } """ STYLE = {"start": "", "repeat": "; ", "end": ""} ChaloBest = arrest.Client("http://chalobest.in/1.0") def get_routes_for_matches(stops): 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) routes = [] for stop in same_stops: routes.extend(stop['properties']['routes'].split(", ")) return routes class App(AppBase): def handle(self, msg): if DIGIT.search(msg.text): routes = ChaloBest.routes(q=msg.text.replace(" ", "")) if not routes: msg.respond("Sorry, we found no route marked '%(text)s'.", text=msg.text) return detail = ChaloBest.route[routes[0]] stops = detail['stops']['features'] 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']) msg.respond("%s: %s (%s) to %s (%s)" % ( ",".join(routes), origin_name, origin_area, dest_name, dest_area)) elif msg.text.find(" to ") != -1: stop1txt = msg.text.split("to")[0].strip() stop2txt = msg.text.split("to")[1].strip() stop1matches = ChaloBest.stops(q=stop1txt)['features'] if not stop1matches: msg.respond("Sorry, found no stop matching '%s'" % stop1txt) return best_match1 = stop1matches[0] routes1 = set(get_routes_for_matches(stop1matches)) stop2matches = ChaloBest.stops(q=stop2txt)['features'] if not stop2matches: msg.respond("Sorry, found no stop matching '%s'" % stop2txt) best_match2 = stop2matches[0] routes2 = set(get_routes_for_matches(stop2matches)) #routes1arr = set(routes1.split(", ")) #routes2arr = set(routes2.split(", ")) intersection = list(routes1.intersection(routes2)) 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'],)) return routesFound = ", ".join(intersection) msg.respond("%s to %s: %s" % (best_match1['properties']['official_name'], best_match2['properties']['official_name'], routesFound,)) return else: features = ChaloBest.stops(q=msg.text)['features'] if not features: msg.respond("Sorry, we found no stops marked '%(text)s'.", text=msg.text) return 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) response = STYLE["start"] for stop in stops: match = stop["official_name"] + ": " + stop["routes"] 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)] + "..." response += STYLE["end"] msg.respond(response)