-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
232 lines (195 loc) · 7.93 KB
/
main.py
File metadata and controls
232 lines (195 loc) · 7.93 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import os
import uuid
import plyvel
import simplejson as json
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.httpclient import AsyncHTTPClient
from tornado.web import RequestHandler, Application, url, StaticFileHandler, HTTPError
import math
class Gathering(object):
def __init__(self):
self.id = str(uuid.uuid4())
self.friends = {}
self.recommendations = []
self.selected_rec = None
self.centroid = None
def add_friend(self, lat, lng):
"""
Takes in the location of a new friend, and returns the id of the new friend
"""
friend_id = str(uuid.uuid4())
self.friends[friend_id] = {
'lat': lat,
'lng': lng,
'name': None
}
self.__update_centroid()
return friend_id
def update_friend(self, friend_id, lat, lng):
self.friends[friend_id]['lat'] = lat
self.friends[friend_id]['lng'] = lng
self.__update_centroid()
def update_friend_name(self, friend_id, name):
self.friends[friend_id]['name'] = name
def del_friend(self, id):
if id in self.friends:
del self.friends[id]
def to_dict(self):
return self.__dict__
@classmethod
def gathering_from_json(cls, jsonstr):
g = Gathering()
gdict = json.loads(jsonstr)
g.id = gdict['id']
g.friends = gdict['friends']
g.recommendations = gdict['recommendations']
g.selected_rec = gdict['selected_rec']
g.centroid = gdict['centroid']
return g
def __update_centroid(self):
self.centroid = self.__centrepoint([[float(f['lat']), float(f['lng'])] for f in self.friends.values()])
def __centrepoint(self, coordinates):
coordinates = [map(math.radians, c) for c in coordinates]
points = len(coordinates)
mean_x = sum(math.cos(lat) * math.cos(lng) for lat, lng in coordinates) / points
mean_y = sum(math.cos(lat) * math.sin(lng) for lat, lng in coordinates) / points
mean_z = sum(math.sin(lat) for lat, _ in coordinates) / points
lng = math.atan2(mean_y, mean_x)
hyp = math.sqrt(mean_x ** 2 + mean_y ** 2)
lat = math.atan2(mean_z, hyp)
return [math.degrees(lat), math.degrees(lng)]
class BaseHandler(RequestHandler):
@property
def db(self):
return self.application.db
def write_error(self, status_code, **kwargs):
"""Use custom error pages.
"""
self.render("error.html", code=status_code, message=self._reason)
class HomeHandler(BaseHandler):
def get(self):
"""Display home page.
"""
self.render("main.html")
class GatheringNewHandler(BaseHandler):
def post(self):
"""Create a new Gathering
"""
g = Gathering()
self.db.put(g.id, json.dumps(g.to_dict()))
self.redirect("/gatherings/" + g.id)
class GatheringHandler(BaseHandler):
def get(self, gathering_id):
"""Initial load of the gatherings page.
"""
gjson = self.db.get(str(gathering_id))
if not gjson:
raise HTTPError(404)
g = Gathering.gathering_from_json(gjson)
friend_id = self.get_secure_cookie("friend_id")
in_gathering = "true" if friend_id in g.friends else "false"
self.render("gathering.html", in_gathering=in_gathering)
def post(self, gathering_id):
"""Add a new friend.
"""
gjson = self.db.get(str(gathering_id))
if not gjson:
raise HTTPError(404)
g = Gathering.gathering_from_json(gjson)
friend_id = self.get_secure_cookie("friend_id")
if not friend_id or (friend_id not in g.friends):
lat, lng = self.get_body_argument("lat"), self.get_body_argument("lng")
friend_id = g.add_friend(lat, lng)
self.db.put(g.id, json.dumps(g.to_dict()))
self.set_secure_cookie("friend_id", friend_id)
fetch_recommendations(g, self.db)
self.set_cookie("my_id", friend_id)
self.write(g.to_dict())
def put(self, gathering_id):
"""Update the location of an existing friend.
"""
gjson = self.db.get(str(gathering_id))
if not gjson:
raise HTTPError(404)
g = Gathering.gathering_from_json(gjson)
friend_id = self.get_secure_cookie("friend_id")
if friend_id:
lat, lng = self.get_body_argument("lat"), self.get_body_argument("lng")
g.update_friend(friend_id, lat, lng)
if self.get_body_argument("name", None) :
g.update_friend_name(friend_id, self.get_body_argument("name"))
self.db.put(g.id, json.dumps(g.to_dict()))
fetch_recommendations(g, self.db)
self.write(g.to_dict())
class GatheringPollHandler(BaseHandler):
"""Endpoint for longpolling to hit, to constantly get updated
Gathering data. Should return 302 when provided with an If-Not-Match
etag header that matches.
"""
def get(self, gathering_id):
gjson = self.db.get(str(gathering_id))
if not gjson:
raise HTTPError(404)
g = Gathering.gathering_from_json(gjson)
self.write(g.to_dict())
class FriendHandler(BaseHandler):
def delete(self, gathering_id, friend_id):
"""Delete a friend.
"""
gjson = self.db.get(str(gathering_id))
if not gjson:
raise HTTPError(404)
g = Gathering.gathering_from_json(gjson)
g.del_friend(friend_id)
self.db.put(g.id, json.dumps(g.to_dict()))
self.set_header(200)
self.write('')
class NotFoundHandler(BaseHandler):
"""Default 404 handler
"""
def prepare(self):
self.set_status(404)
self.render("404.html")
class WTFSGEApplication(Application):
"""The main application used by this app.
We subclass Application in order to store a global DB connection
"""
def __init__(self):
handlers = [
url(r"/", HomeHandler),
url(r"/gatherings/", GatheringNewHandler, name="newgathering"),
url(r"/gatherings/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12})", GatheringHandler),
url(r"/gatherings/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12})/data", GatheringPollHandler),
url(r"/gatherings/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12})/friends/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12})", FriendHandler),
url(r"/static", StaticFileHandler)
]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
default_handler_class=NotFoundHandler,
# Insecure because public repo
cookie_secret=":A@[&%p<y~NQ^*e[T7ArS%(u^|TYf^1YB|cl*_$cG-U_X{5{L1&!n><mC)t8kh%.",
debug=True
)
Application.__init__(self, handlers, **settings)
# Create a global connection to leveldb.
self.db = plyvel.DB('db', create_if_missing=True)
@gen.coroutine
def fetch_recommendations(gathering, db):
"""Async fetch of recommendations from the Google Maps API.
"""
http_client = AsyncHTTPClient()
lat, lng = gathering.centroid[0], gathering.centroid[1]
url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIzaSyBEA3Z2q4QrCx4D71VBGg-WyKt8H1CZa94&location=" + str(lat) + "," + str(lng) + "&radius=1000&types=restaurant|food|cafe"
response = yield http_client.fetch(url)
res_json = json.loads(response.body)
gathering.recommendations = res_json["results"]
db.put(gathering.id, json.dumps(gathering.to_dict()))
def main():
app = WTFSGEApplication()
app.listen(4000)
IOLoop.current().start()
if __name__ == '__main__':
print "Go to http://localhost:4000 on your browser..."
main()