bestSMS/best/smsmesn/handlers/stop.py
2011-08-06 19:53:50 +05:30

35 lines
1.2 KiB
Python

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 <stop_name> to get buses that stop at a particular stop.")
def handle(self, text):
print "STOP HANDLER"
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 = stop[1] + ": " + ",".join([val.split("|")[0] for val in stop[-1].split(",")])
self.respond(stopStr)
else:
self.respond("try one of: %s" % getOptions(places))