shape up smsBEST parsing a bit.
This commit is contained in:
parent
a75af22f6a
commit
def02ca104
|
@ -6,9 +6,9 @@ MAX_MSG_LEN = 160
|
||||||
DIGIT = re.compile(r"\d{1,3}")
|
DIGIT = re.compile(r"\d{1,3}")
|
||||||
PUNCT = re.compile(r"[^\w\s]")
|
PUNCT = re.compile(r"[^\w\s]")
|
||||||
STYLE = {
|
STYLE = {
|
||||||
"start": "-* ",
|
"start": "-* ",
|
||||||
"repeat": "-*-",
|
"repeat": " -*- ",
|
||||||
"end": " *-"
|
"end": " *-"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,20 +20,29 @@ class App(AppBase):
|
||||||
routes = ChaloBest.routes(q=msg.text.replace(" ", ""))
|
routes = ChaloBest.routes(q=msg.text.replace(" ", ""))
|
||||||
detail = ChaloBest.route[routes[0]]
|
detail = ChaloBest.route[routes[0]]
|
||||||
stops = detail['stops']['features']
|
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']
|
origin, destination = stops[0]['properties'], stops[-1]['properties']
|
||||||
msg.respond("%s: %s (%s) to %s (%s)" % (routes[0],
|
msg.respond("%s: %s (%s) to %s (%s)" % (routes[0],
|
||||||
origin['display_name'], origin['area'],
|
origin['display_name'], origin['area'],
|
||||||
destination['display_name'], destination['area']))
|
destination['display_name'], destination['area']))
|
||||||
else:
|
else:
|
||||||
stops = ChaloBest.stops(q=msg.text)['features']
|
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"]
|
response = STYLE["start"]
|
||||||
for feature in stops:
|
for feature in stops:
|
||||||
stop = feature['properties']
|
stop = feature['properties']
|
||||||
area = PUNCT.sub('', stop['area'])
|
area = PUNCT.sub('', stop['area'])
|
||||||
match = "%s (%s): %s" % (stop['official_name'], area, stop['routes'])
|
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(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
|
response += match
|
||||||
|
elif len(response) < len(STYLE["repeat"]):
|
||||||
|
response += match[:MAX_MSG_LEN-len(STYLE["start"])-len(STYLE["end"])+4]
|
||||||
|
response += " ..."
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
response += STYLE["end"]
|
response += STYLE["end"]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user