bzr merge, dur

This commit is contained in:
Schuyler Erle 2012-02-28 12:03:35 -08:00
parent 3a3e5d20f2
commit a75af22f6a

View File

@ -4,27 +4,37 @@ import arrest
MAX_MSG_LEN = 160 MAX_MSG_LEN = 160
DIGIT = re.compile(r"\d{1,3}") 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): class App(AppBase):
def handle(self, msg): def handle(self, msg):
if DIGIT.search(msg.text): if DIGIT.search(msg.text):
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']
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']
response = "" response = STYLE["start"]
for feature in stops: for feature in stops:
stop = feature['properties'] stop = feature['properties']
match = "%s (%s): %s" % (stop['official_name'], stop['area'], stop['routes']) area = PUNCT.sub('', stop['area'])
if len(response) + len(match) + 1 < MAX_MSG_LEN: match = "%s (%s): %s" % (stop['official_name'], area, stop['routes'])
if response: response += ";" if len(response) + len(match) + len(STYLE["repeat"]) < MAX_MSG_LEN:
if len(response) > len(STYLE["repeat"]): response += SECTION_BREAK
response += match response += match
else: else:
break break
response += STYLE["end"]
msg.respond(response) msg.respond(response)