From f42f4f3049fbbc0974f336679dd0df22b86831d2 Mon Sep 17 00:00:00 2001 From: Sanj Date: Sat, 23 Jul 2011 18:02:24 +0530 Subject: [PATCH] added stop handler. freeze for now. --- best/smsmesn/handlers/fromto.py | 2 ++ best/smsmesn/handlers/stop.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 best/smsmesn/handlers/stop.py diff --git a/best/smsmesn/handlers/fromto.py b/best/smsmesn/handlers/fromto.py index f2feb75..dc01eb6 100644 --- a/best/smsmesn/handlers/fromto.py +++ b/best/smsmesn/handlers/fromto.py @@ -95,6 +95,8 @@ class FromToHandler(KeywordHandler): toString = "to %s" % (getOptions(tos)) self.respond(fromString + " " + toString) + + def time_str(no): whole = int(no) decimal = (no - whole) * 10 diff --git a/best/smsmesn/handlers/stop.py b/best/smsmesn/handlers/stop.py new file mode 100644 index 0000000..a8f5927 --- /dev/null +++ b/best/smsmesn/handlers/stop.py @@ -0,0 +1,33 @@ +from fromto import getStop, getStops, getOptions +from rapidsms.contrib.handlers.handlers.keyword import KeywordHandler +# from new_best.models import * +import datetime +try: + import json +except: + import simplejson as json +from settings import JSON_PATH +from os.path import join + + +class StopHandler(KeywordHandler): + keyword = "stop" + + def help(self): + self.respond("Send stop to get buses that stop at a particular stop.") + + def handle(self, text): + place = text.strip().lower() + stopList = json.loads(open(join(JSON_PATH, "stoplist.json")).read().replace("\\", "/")) + places = getStops(stopList, place) + if len(places) == 0: + self.respond("place not found.") + elif len(places) == 1: + data = json.loads(open(join(JSON_PATH, "stopbus.json")).read().replace("\\", "/").replace(",,,", ",").replace(",,", ",")) + stop = getStop(data, places[0][1]) + stopStr = ",".join([val.split("|")[0] for val in stop[-1].split(",")]) + self.respond(stopStr) + else: + self.respond("try one of: %s" % getOptions(places)) + +