added bus route handler

This commit is contained in:
Sanj 2011-08-06 20:55:23 +05:30
parent 88709f7bcf
commit 27c3f554ce
3 changed files with 31 additions and 1 deletions

1
best/json/routes.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,29 @@
# 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 BusHandler(KeywordHandler):
keyword = "bus"
def help(self):
self.respond("Send bus <bus_no> to get first and last stop.")
def handle(self, text):
bus_no = text.strip().lower()
busList = json.loads(open(join(JSON_PATH, "routes.json")).read())
found = False
for b in busList:
if b[0].strip() == bus_no or b[1].strip() == bus_no:
self.respond("from " + b[2] + " to " + b[3])
found = True
if not found:
self.respond("sorry, bus not found")

View File

@ -26,7 +26,7 @@ class StopHandler(KeywordHandler):
elif len(places) == 1: elif len(places) == 1:
data = json.loads(open(join(JSON_PATH, "stopbus.json")).read().replace("\\", "/").replace(",,,", ",").replace(",,", ",")) data = json.loads(open(join(JSON_PATH, "stopbus.json")).read().replace("\\", "/").replace(",,,", ",").replace(",,", ","))
stop = getStop(data, places[0][1]) stop = getStop(data, places[0][1])
stopStr = stop[1] + ": " + ",".join([val.split("|")[0] for val in stop[-1].split(",")]) stopStr = str(stop[1]) + ": " + ",".join([val.split("|")[0] for val in stop[-1].split(",")])
self.respond(stopStr) self.respond(stopStr)
else: else:
self.respond("try one of: %s" % getOptions(places)) self.respond("try one of: %s" % getOptions(places))