From def02ca104b557c964144570187fc0bd4fef325b Mon Sep 17 00:00:00 2001 From: Schuyler Erle Date: Tue, 28 Feb 2012 12:15:32 -0800 Subject: [PATCH] shape up smsBEST parsing a bit. --- smsBEST/mumbai/app.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/smsBEST/mumbai/app.py b/smsBEST/mumbai/app.py index 32c9ef2..c8d823a 100644 --- a/smsBEST/mumbai/app.py +++ b/smsBEST/mumbai/app.py @@ -6,9 +6,9 @@ MAX_MSG_LEN = 160 DIGIT = re.compile(r"\d{1,3}") PUNCT = re.compile(r"[^\w\s]") STYLE = { - "start": "-* ", - "repeat": "-*-", - "end": " *-" + "start": "-* ", + "repeat": " -*- ", + "end": " *-" } @@ -20,20 +20,29 @@ class App(AppBase): routes = ChaloBest.routes(q=msg.text.replace(" ", "")) detail = ChaloBest.route[routes[0]] stops = detail['stops']['features'] + if not stops: + msg.respond("Sorry, we found no route marked '%s'." % msg.text) + return 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'] + if not stops: + msg.respond("Sorry, we found no stops like '%s'." % msg.text) + return response = STYLE["start"] for feature in stops: stop = feature['properties'] 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 + if len(response) > len(STYLE["repeat"]): response += STYLE["repeat"] response += match + elif len(response) < len(STYLE["repeat"]): + response += match[:MAX_MSG_LEN-len(STYLE["start"])-len(STYLE["end"])+4] + response += " ..." else: break response += STYLE["end"]