bestSMS/best/smsmesn/handlers/stop.py

35 lines
1.2 KiB
Python
Raw Normal View History

2011-07-23 12:32:24 +00:00
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):
2011-08-06 14:23:50 +00:00
print "STOP HANDLER"
2011-07-23 12:32:24 +00:00
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])
2011-08-06 14:23:50 +00:00
stopStr = stop[1] + ": " + ",".join([val.split("|")[0] for val in stop[-1].split(",")])
2011-07-23 12:32:24 +00:00
self.respond(stopStr)
else:
self.respond("try one of: %s" % getOptions(places))