This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
/
http.py
135 lines (113 loc) · 3.95 KB
/
http.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import sys
import site
root_dir = os.path.dirname(__file__)
# with mod_wsgi >= 2.4, this line will add this path in front of the python path
site.addsitedir(os.path.join(root_dir, 'lib/python2.7/site-packages'))
# add this django project
sys.path.append(root_dir)
import web
from web.contrib.template import render_jinja
from geocoder import geocode
import re
import otp
import xml.etree.ElementTree as ET
import geocoder
from datetime import datetime
import StringIO
import json
import textdirections
import dateparser
render = render_jinja(
os.path.join(root_dir,'templates'), # Set template directory.
encoding = 'utf-8', # Encoding.
globals=dict(otp=otp)
)
# Add/override some global functions.
#render._lookup.globals.update(
# otp=otp
#)
urls = (
'/', 'app',
'/sms-test', 'sms_debug',
'/sms','sms',
'/smshelp','smshelp',
'/about','about',
)
class app:
def GET(self):
if 'mode' not in web.input():
return render.app(timemode='now',time=datetime.today().strftime("%H:%M"))
tvars = dict(web.input())
tvars['error'] = None
fromplace = getattr(web.input(),'from')
toplace = web.input().to
if not fromplace or not toplace:
tvars['error'] = 'Please enter an address or landmark for From and To'
return render.app(**tvars)
from_result,fromgeo = geocoder.geocode(fromplace)
if from_result != 'OK':
tvars['error'] = 'Unable to find address for %s' % fromplace
return render.app(**tvars)
tvars['fromgeo'] = fromgeo
to_result,togeo = geocoder.geocode(toplace)
if to_result != 'OK':
tvars['error'] = 'Unable to find address for %s' % toplace
return render.app(**tvars)
tvars['togeo'] = togeo
timemode = web.input().get('timemode')
if timemode == 'now':
result = otp.plan(fromgeo[0:2],togeo[0:2],web.input().mode)
else:
try:
time = dateparser.parse_time(web.input().time)
except ValueError:
tvars['error'] = "Invalid time format"
return render.app(**tvars)
result = otp.plan(fromgeo[0:2],togeo[0:2],web.input().mode,time,timemode)
if 'plan' in result:
tvars['result'] = result
else:
# no itinerary found - rare but possible
tvars['error'] = result['error']['msg']
return render.app(**tvars)
class sms:
def POST(self):
# https://www.twilio.com/docs/api/twiml/sms/twilio_request
body = web.input().Body
result,message,cookies = textdirections.handle_text(body,web.cookies().get('bikebus'))
if cookies is not None:
web.setcookie('bikebus',cookies)
else:
web.setcookie('bikebus',web.cookies().get('bikebus'))
# https://www.twilio.com/docs/api/twiml/sms/your_response
root = ET.Element("Response")
for msg in message:
el = ET.SubElement(root,'Sms')
el.text = msg
tree = ET.ElementTree(root)
web.header('Content-Type', 'text/xml')
output = StringIO.StringIO()
tree.write(output,encoding="UTF-8",xml_declaration=True)
return output.getvalue()
class smshelp:
def GET(self):
return render.smshelp()
class about:
def GET(self):
return render.about()
class sms_debug:
def POST(self):
q = web.input().q
result,message,cookies = textdirections.handle_text(q,web.cookies().get('bikebus'))
if cookies is not None:
web.setcookie('bikebus',cookies)
else:
web.setcookie('bikebus',web.cookies().get('bikebus'))
return render.sms(question=q,message=message,help=[textdirections.WELCOME_TEXT,textdirections.HELP_TEXT1,textdirections.HELP_TEXT2])
def GET(self):
return render.sms(qestion='',message=[],help=[textdirections.WELCOME_TEXT,textdirections.HELP_TEXT1,textdirections.HELP_TEXT2])
if __name__ == '__main__':
web.application(urls,globals()).run()
else:
application = web.application(urls, globals()).wsgifunc()