from_to handlers using mesn json data
This commit is contained in:
parent
80e9d73974
commit
c6d23c1b76
1
best/json/stopbus.json
Normal file
1
best/json/stopbus.json
Normal file
File diff suppressed because one or more lines are too long
3
best/json/stoplist.json
Normal file
3
best/json/stoplist.json
Normal file
File diff suppressed because one or more lines are too long
|
@ -5,7 +5,11 @@
|
|||
# -------------------------------------------------------------------- #
|
||||
# MAIN CONFIGURATION #
|
||||
# -------------------------------------------------------------------- #
|
||||
import os
|
||||
from os.path import join
|
||||
|
||||
PROJECT_PATH = os.path.dirname(__file__)
|
||||
JSON_PATH = join(PROJECT_PATH, "json")
|
||||
|
||||
# you should configure your database here before doing any real work.
|
||||
# see: http://docs.djangoproject.com/en/dev/ref/settings/#databases
|
||||
|
@ -57,6 +61,7 @@ INSTALLED_APPS = [
|
|||
"djtables",
|
||||
"rapidsms",
|
||||
"new_best",
|
||||
'smsmesn',
|
||||
"django_extensions",
|
||||
# "buses",
|
||||
# common dependencies (which don't clutter up the ui).
|
||||
|
|
0
best/smsmesn/handlers/__init__.py
Normal file
0
best/smsmesn/handlers/__init__.py
Normal file
103
best/smsmesn/handlers/fromto.py
Normal file
103
best/smsmesn/handlers/fromto.py
Normal file
|
@ -0,0 +1,103 @@
|
|||
#!/usr/bin/env python
|
||||
# vim: ai ts=4 sts=4 et sw=4
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
def getStops(data, s):
|
||||
stops = []
|
||||
for d in data:
|
||||
if d[1].lower() == s:
|
||||
return [d]
|
||||
else:
|
||||
if d[0].lower().find(s) != -1:
|
||||
stops.append(d)
|
||||
return stops
|
||||
|
||||
|
||||
def getStop(data, s):
|
||||
for d in data:
|
||||
if d[1] == s:
|
||||
return d
|
||||
return [""]
|
||||
|
||||
def getOptions(stops):
|
||||
arr = []
|
||||
for s in stops:
|
||||
arr.append(s[1])
|
||||
stopStr = ",".join(arr[0:4])
|
||||
return '(' + stopStr + ')'
|
||||
|
||||
def getBuses(frm, to):
|
||||
# return "hi"
|
||||
from_code = frm[1].strip()
|
||||
to_code = to[1].strip()
|
||||
# url = "http://mesn.org/Bestbus/busesfound.html"
|
||||
data = json.loads(open(join(JSON_PATH, "stopbus.json")).read().replace("\\", "/").replace(",,,", ",").replace(",,", ","))
|
||||
to = getStop(data, to_code)
|
||||
frm = getStop(data, from_code)
|
||||
# return to[0]
|
||||
stopListFrom = frm[-1]
|
||||
stopListTo = to[-1]
|
||||
fromArr = stopListFrom.split(",")
|
||||
fromArr = [val.split("|")[0] for val in fromArr]
|
||||
toArr = stopListTo.split(",")
|
||||
toArr = [val.split("|")[0] for val in toArr]
|
||||
intersectArr = [val for val in fromArr if val in toArr]
|
||||
# return ",".join(fromArr) + ",".join(toArr)
|
||||
if (len(intersectArr) == 0):
|
||||
return "no direct buses found."
|
||||
# return "from %s to %s" % (",".join(fromArr), ",".join(toArr),)
|
||||
else:
|
||||
return ",".join(intersectArr)
|
||||
|
||||
class FromToHandler(KeywordHandler):
|
||||
keyword = "from"
|
||||
|
||||
def help(self):
|
||||
self.respond("Send from <place> to <place>")
|
||||
|
||||
def handle(self, text):
|
||||
txt = text.strip()
|
||||
from_to = txt.split("to")
|
||||
frm = from_to[0].strip()
|
||||
to = from_to[1].strip()
|
||||
# print frm
|
||||
# print to
|
||||
data = json.loads(open(join(JSON_PATH, "stoplist.json")).read().replace("\\", "/"))
|
||||
froms = getStops(data, frm)
|
||||
tos = getStops(data, to)
|
||||
|
||||
if len(froms) == len(tos) == 1:
|
||||
self.respond(getBuses(froms[0], tos[0]))
|
||||
else:
|
||||
if len(froms) == 0:
|
||||
fromString = "From location not found."
|
||||
elif len(froms) == 1:
|
||||
fromString = "From %s" % (froms[0][1])
|
||||
else:
|
||||
fromString = "From %s" % (getOptions(froms))
|
||||
|
||||
if len(tos) == 0:
|
||||
toString = "To location not found."
|
||||
elif len(tos) == 1:
|
||||
toString = "to %s" % (tos[0][1])
|
||||
else:
|
||||
toString = "to %s" % (getOptions(tos))
|
||||
self.respond(fromString + " " + toString)
|
||||
|
||||
def time_str(no):
|
||||
whole = int(no)
|
||||
decimal = (no - whole) * 10
|
||||
mins = int(((decimal + .0) / 10) * 60)
|
||||
return str(whole) + ":" + str(mins)
|
||||
|
Loading…
Reference in New Issue
Block a user