From a75af22f6a021695606530391a4ee83adbc9a6eb Mon Sep 17 00:00:00 2001 From: Schuyler Erle Date: Tue, 28 Feb 2012 12:03:35 -0800 Subject: [PATCH] bzr merge, dur --- smsBEST/mumbai/app.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/smsBEST/mumbai/app.py b/smsBEST/mumbai/app.py index 5031670..32c9ef2 100644 --- a/smsBEST/mumbai/app.py +++ b/smsBEST/mumbai/app.py @@ -4,27 +4,37 @@ import arrest MAX_MSG_LEN = 160 DIGIT = re.compile(r"\d{1,3}") -chalobest = arrest.Client("http://chalobest.in/1.0") +PUNCT = re.compile(r"[^\w\s]") +STYLE = { + "start": "-* ", + "repeat": "-*-", + "end": " *-" +} + + +ChaloBest = arrest.Client("http://chalobest.in/1.0") class App(AppBase): def handle(self, msg): if DIGIT.search(msg.text): - routes = chalobest.routes(q=msg.text.replace(" ", "")) - detail = chalobest.route[routes[0]] + routes = ChaloBest.routes(q=msg.text.replace(" ", "")) + detail = ChaloBest.route[routes[0]] stops = detail['stops']['features'] origin, destination = stops[0]['properties'], stops[-1]['properties'] msg.respond("%s: %s (%s) to %s (%s)" % (routes[0], origin['display_name'], origin['area'], destination['display_name'], destination['area'])) else: - stops = chalobest.stops(q=msg.text)['features'] - response = "" + stops = ChaloBest.stops(q=msg.text)['features'] + response = STYLE["start"] for feature in stops: stop = feature['properties'] - match = "%s (%s): %s" % (stop['official_name'], stop['area'], stop['routes']) - if len(response) + len(match) + 1 < MAX_MSG_LEN: - if response: response += ";" + area = PUNCT.sub('', stop['area']) + match = "%s (%s): %s" % (stop['official_name'], area, stop['routes']) + if len(response) + len(match) + len(STYLE["repeat"]) < MAX_MSG_LEN: + if len(response) > len(STYLE["repeat"]): response += SECTION_BREAK response += match else: break + response += STYLE["end"] msg.respond(response)