2012-02-28 16:59:30 +00:00
|
|
|
from rapidsms.apps.base import AppBase
|
2012-02-28 18:10:22 +00:00
|
|
|
import re
|
|
|
|
import arrest
|
|
|
|
|
2012-02-28 19:48:28 +00:00
|
|
|
MAX_MSG_LEN = 160
|
2012-02-28 18:10:22 +00:00
|
|
|
DIGIT = re.compile(r"\d{1,3}")
|
2012-02-28 20:03:35 +00:00
|
|
|
PUNCT = re.compile(r"[^\w\s]")
|
|
|
|
STYLE = {
|
|
|
|
"start": "-* ",
|
|
|
|
"repeat": "-*-",
|
|
|
|
"end": " *-"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ChaloBest = arrest.Client("http://chalobest.in/1.0")
|
2012-02-28 16:59:30 +00:00
|
|
|
|
|
|
|
class App(AppBase):
|
2012-02-28 18:10:22 +00:00
|
|
|
def handle(self, msg):
|
|
|
|
if DIGIT.search(msg.text):
|
2012-02-28 20:03:35 +00:00
|
|
|
routes = ChaloBest.routes(q=msg.text.replace(" ", ""))
|
|
|
|
detail = ChaloBest.route[routes[0]]
|
2012-02-28 18:10:22 +00:00
|
|
|
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:
|
2012-02-28 20:03:35 +00:00
|
|
|
stops = ChaloBest.stops(q=msg.text)['features']
|
|
|
|
response = STYLE["start"]
|
2012-02-28 19:48:28 +00:00
|
|
|
for feature in stops:
|
|
|
|
stop = feature['properties']
|
2012-02-28 20:03:35 +00:00
|
|
|
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
|
2012-02-28 19:48:28 +00:00
|
|
|
response += match
|
|
|
|
else:
|
|
|
|
break
|
2012-02-28 20:03:35 +00:00
|
|
|
response += STYLE["end"]
|
2012-02-28 19:48:28 +00:00
|
|
|
msg.respond(response)
|