-
Notifications
You must be signed in to change notification settings - Fork 0
/
wedding.py
507 lines (422 loc) · 19.9 KB
/
wedding.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import ast
import random
from threading import Thread, Event
from Queue import Queue
import logging
import sys
import time
import os.path
import tornado.auth
import tornado.escape
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.log
import tornado.gen
#non-blocking redis
import tornadoredis
import settings
from tornado.options import options
import tcelery
import calculations
import simplejson as json
#global redis client class
#don't forget to change back to db1
redis = tornadoredis.Client(selected_db=2)
redis.connect()
pipe = redis.pipeline()
tcelery.setup_nonblocking_producer()
class Application(tornado.web.Application):
def __init__(self):
debug = (tornado.options.options.environment == "dev")
handlers = [
#/user, /matches are for the api
(r"/", IndexHandler),
(r"/main", MainHandler),
(r"/love", BatchHandler),
(r"/user", UserHandler),
(r"/mymatches", CalculatedHandler),
(r"/matches", MatchApi),
(r"/auth/login", AuthLoginHandler),
(r"/auth/logout", AuthLogoutHandler),
(r"/privacy", PrivacyHandler),
(r"/terms", TermsHandler),
]
settings = dict(
cookie_secret="fdkfadsljdfklsjklad98u32#@RDSAF@#(@*&#jlitjuu#$%i99#@G",
login_url="/auth/login",
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookies=True,
facebook_api_key=options.facebook_api_key,
facebook_secret=options.facebook_secret,
ui_modules={
"Partner": PartnerModule, "Contender": ContenderModule},
debug=True,
autoescape=None
)
tornado.web.Application.__init__(self, handlers, **settings)
# remove get_likes and use in mainhandler
class IndexHandler(tornado.web.RequestHandler):
def get(self):
logging.info(self.request.headers)
self.render("index.html")
class ExceptionHandler(tornado.web.RequestHandler):
def __init__(self, application, request, status_code):
tornado.web.RequestHandler.__init__(self, application, request)
self.set_status(status_code)
def get_error_html(self, status_code, **kwargs):
self.require_setting("static_path")
if status_code in [404, 500, 503, 403]:
self.render("oops.html", status_code=status_code)
logging.error( {"code": status_code,"message": status_code})
logging.info(self.request.headers)
def get_error_scraping(self, status_code, **kwargs):
self.require_setting("static_path")
if status_code == 599:
self.redirect("/mymatches")
logging.error({"code":status_code})
def prepare(self):
raise tornado.web.HTTPError(self._status_code)
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
"""
This method is called whenever the self.current_user property is not
already cached. See tornado/web.py RequestHandler.current_user.
"""
user_json = self.get_secure_cookie("fbdemo_user")
if not user_json:
return None
return tornado.escape.json_decode(user_json)
@tornado.gen.coroutine
def user_exists(self, curr):
yield tornado.gen.Task(redis.hget, "users:%s" % str(curr["id"]), "name")
class AuthLoginHandler(BaseHandler, tornado.auth.FacebookGraphMixin):
@tornado.web.asynchronous
def get(self):
my_url = (self.request.protocol + "://" + self.request.host +
"/auth/login?next=" +
tornado.escape.url_escape(self.get_argument("next", "/")))
logging.info(self.request.headers)
if self.get_argument("code", False):
self.get_authenticated_user(
redirect_uri=my_url,
client_id=self.settings["facebook_api_key"],
client_secret=self.settings["facebook_secret"],
code=self.get_argument("code"),
callback=self._on_auth)
return
self.authorize_redirect(redirect_uri=my_url,
client_id=self.settings["facebook_api_key"],
extra_params={"scope": "read_stream, user_about_me,user_interests,friends_activities,friends_birthday,friends_education_history,friends_interests,friends_likes,friends_relationships,friends_relationship_details,friends_religion_politics,friends_subscriptions,friends_events,friends_status friends_about_me, offline_access,read_friendlists,user_likes,user_activities,user_relationships,user_relationship_details, friends_photos,user_photos"})
def _on_auth(self, user):
if not user:
raise tornado.web.HTTPError(500, "Facebook auth failed")
self.set_secure_cookie("fbdemo_user", tornado.escape.json_encode(user))
self.redirect(self.get_argument("next", "/"))
class AuthLogoutHandler(BaseHandler, tornado.auth.FacebookGraphMixin):
def get(self):
logging.info("%s disconnected", self.current_user["name"])
logging.info(self.request.headers)
self.clear_cookie("fbdemo_user")
self.redirect(self.get_argument("next", "/"))
class UserHandler(BaseHandler, tornado.auth.FacebookGraphMixin):
"""
Returns a json representation of current user
"""
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self):
user = yield tornado.gen.Task(redis.hgetall, "users:%s" % self.current_user["id"])
self.write(tornado.escape.json_encode(user))
# all user based magic happens here
# separate scrape logic from calculate and render pages
class MainHandler(BaseHandler, tornado.auth.FacebookGraphMixin):
"""
Version 1 is completed
Scrapes your likes and your information to help determine eligibility
"""
@tornado.web.asynchronous
@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
pipe = redis.pipeline()
access_token = self.current_user["access_token"]
logging.info("%s connected" % self.current_user["name"])
logging.info(self.request.headers)
pipe.hexists("users:%s" % self.current_user["id"], "attracted_to")
pipe.exists("users:%s" % self.current_user["id"])
pipe.exists("user:%s" % self.current_user["id"])
try:
attraction_known, user_exists, likes_exist = yield tornado.gen.Task(pipe.execute)
token = self.current_user["access_token"]
if not user_exists:
logging.debug("getting user info from Facebook")
self.facebook_request("/me", self.get_user, access_token=token)
fb_like_fields = (
"movies.fields(id,name),music.fields(id,name),"
"favorite_athletes,favorite_teams,religion,political,sports,"
"books.fields(id,name),games.fields(id,name),"
"interests.fields(id,name),television.fields(id,name),"
"activities.fields(id,name),religion,education,political"
)
if not likes_exist:
logging.debug("getting user likes from Facebook")
self.facebook_request("/me", self.get_likes, access_token=token,
fields=fb_like_fields)
# #use this once testing phase is complete
# logging.debug("prompting user for gender attraction")
# user = yield tornado.gen.Task(redis.hgetall, "users:%s" % self.current_user["id"])
# #show gender selection form always (like bang with friends)
# self.write(user)
if not attraction_known:
logging.debug("prompting user for gender attraction")
self.render("index.html", show_form=True)
else:
self.render("index.html", show_form=False)
except ValueError:
logging.error("too many values to unpack")
self.redirect("/main")
def get_user(self, d):
self.make_user(d)
@tornado.web.asynchronous
def get_likes(self, d):
self.parse_likes(d)
@tornado.web.asynchronous
@tornado.gen.coroutine
def post(self):
self.interested_in = self.get_argument('optionsRadios')
with redis.pipeline() as pipe:
pipe.hset("users:%s" % self.current_user[
"id"], "attracted_to", self.interested_in)
yield tornado.gen.Task(pipe.execute)
logging.info("updated interested in")
self.render("index.html", show_form=False)
@tornado.web.asynchronous
@tornado.gen.coroutine
def make_user(self, data):
print "inside make_user"
new_user = { "name": data["name"] }
if 'interested_in' not in data:
new_user["gender"] = data["gender"]
elif 'gender' not in data:
new_user["gender"] = None
else:
new_user["gender"] = data["gender"]
new_user["interested_in"] = data["interested_in"]
with redis.pipeline() as pipe:
pipe.hmset("users:%s" % data["id"], new_user)
yield tornado.gen.Task(pipe.execute)
@tornado.web.asynchronous
@tornado.gen.coroutine
def parse_likes(self, d):
pipe = redis.pipeline()
for item in d.iteritems():
#item is a tuple
#item[0] is the category keyword
if 'data' in item[1]:
for like in item[1]["data"]:
#print like["id"], like["name"]
pipe.hset(like["id"], "name", like["name"])
pipe.sadd("user_likes:%s" % (d["id"]), "%s:%s" % (item[0],like["id"]))
pipe.hmset("%s:%s" % (item[0], like["id"]), {"name": like["name"], "id": like["id"]})
yield tornado.gen.Task(pipe.execute)
class CalculatedHandler(BaseHandler, tornado.auth.FacebookGraphMixin):
#return json array api
@tornado.web.asynchronous
@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
yield self.get_matches()
@tornado.gen.coroutine
def get_matches(self):
pipe = redis.pipeline()
matches = yield tornado.gen.Task(redis.zrevrange, "matches:%s" % (self.current_user["id"]), 0, -1, with_scores=False)
match_list = []
for match in matches:
person = yield tornado.gen.Task(redis.hgetall, match)
person["likes"] = ast.literal_eval(person["likes"])
match_list.append(person)
try:
#deprecate partner.html as we switch to angular for the ui
self.render("partner.html", top_match=match_list[0], matches=match_list[1:])
#first in match_list is the
#self.write(tornado.escape.json_encode(match_list))
except IndexError:
self.render("partner.html", top_match=None, matches=None)
class BatchHandler(BaseHandler, tornado.auth.FacebookGraphMixin):
"""
This handler stores data, not retrieve
"""
@tornado.web.asynchronous
@tornado.gen.coroutine
def post(self):
#rewrite this boolean statement to signal celery calc backends
calc_cache = yield tornado.gen.Task(redis.exists, "calculated:%s" % (self.current_user["id"]))
did_login = yield tornado.gen.Task(redis.exists, "friend_calculated:%s" % self.current_user["id"])
delay_calc = yield tornado.gen.Task(redis.exists, "calc_det:%s" % self.current_user["id"])
if calc_cache:
if did_login and not delay_calc:
calculations.calculate.apply_async(args=[str(self.current_user["access_token"]), str(self.current_user["id"])])
self.redirect("/mymatches")
elif did_login and delay_calc:
self.redirect("/mymatches")
else:
self.redirect("/mymatches")
else:
res = yield self.facebook_request("/me", self.create_person, access_token=self.current_user["access_token"], fields="friends.fields(id,name,interested_in,relationship_status,gender,birthday)")
yield self.friendlist(res)
def test_celery(self, response):
self.write(str(response.result))
self.finish()
@tornado.web.asynchronous
@tornado.gen.coroutine
def friendlist(self, res):
#if else statement here to test whether friend size is larger than group or not
groupsize = 30
request_list = []
for i in res["friends"]["data"]:
batch_dict = {"method":"GET", "relative_url":"%s?fields=movies,name,gender,sports,books,music,relationship_status,television,political,games,religion,education,interests,favorite_athletes,favorite_teams" % str(i["id"])}
request_list.append(batch_dict)
requests =(request_list[i:i+groupsize] for i in xrange(0,len(request_list),groupsize))
fdata = yield [tornado.gen.Task(self.facebook_request, "", post_args={"batch":f},
access_token=self.current_user["access_token"]) for f in requests]
yield tornado.gen.Task(self.batched_req_gen, fdata)
@tornado.gen.coroutine
def batched_req_gen(self, data):
#this should be triggered once
pipe = redis.pipeline()
for i in data:
for j in i:
if "body" in j:
d = ast.literal_eval(j["body"])
if d:
if 'gender' in d:
#heirarchy of keys is keywords ie; 'movie', 'sports', etc and then 'da
yield tornado.gen.Task(self.asynch_data_handler, d, pipe, "movies","sports","books","music","television","political","games","religion","education","interests","favorite_athletes","favorite_teams")
else:
#no gender specified, no need to scrape (unless they chose an interested in)
continue
else:
print "Something went wrong, retry facebook request"
#right here, call to asynch function that scrapes data, be careful, this could be O(n*n!)
pipe.set("calculated:%s" % self.current_user["id"], "True")
pipe.setex("friend_calculated:%s" % self.current_user["id"], 518400, "True")
pipe.setex("calc_det:%s" % self.current_user["id"], 172800, "True")
yield tornado.gen.Task(pipe.execute)
yield tornado.gen.Task(self.make_matches)
self.redirect("/mymatches")
@tornado.gen.coroutine
def asynch_data_handler(self, data, pipe, *args):
yield [tornado.gen.Task(self.asynch_data_scraper, pipe=pipe, data=data, keyword=k) for k in args]
@tornado.gen.coroutine
def asynch_data_scraper(self, pipe, data, keyword, callback=None):
#put keyword logic here
if keyword in data:
#print data[keyword], data["id"], data["name"], data["relationship_status"]
#redis saves here
key = data[keyword]
if isinstance(key, list):
if "school" in key[0]:
pipe.sadd("%s:%s:%s" % (keyword, key[0]["school"]["id"],
data["gender"]), data["id"])
else:
for k in key:
pipe.sadd("%s:%s:%s" % (keyword, k["id"], data["gender"]), data["id"])
elif isinstance(key, dict):
for d in key["data"]:
pipe.sadd("%s:%s:%s" % (keyword, d["id"], data["gender"]), data["id"])
else:
pass
@tornado.gen.coroutine
def create_person(self, data):
pipe = redis.pipeline()
d = data["friends"]["data"]
for i in d:
pipe.hmset("users:%s" % i["id"], i)
pipe.hset("users:%s" % self.current_user["id"], "f_check", "True")
yield tornado.gen.Task(pipe.execute)
@tornado.gen.coroutine
def make_matches(self):
pipe = redis.pipeline()
categories = ["movies","sports","books","music","television","political","games","religion","education","interests","favorite_athletes","favorite_teams"]
pipe.hget("users:%s" % self.current_user["id"], "attracted_to")
pipe.smembers("user_likes:%s" % self.current_user["id"])
attracted_to, user_likes = yield tornado.gen.Task(pipe.execute)
match_list = []
for like in user_likes:
exists = yield tornado.gen.Task(redis.exists, "%s:%s" %(like, attracted_to))
if exists:
matches = yield tornado.gen.Task(redis.smembers, "%s:%s" % (like, attracted_to))
#check to see if match exists so we don't have to recalc this everytime
for match in matches:
match_exists = yield tornado.gen.Task(redis.exists, "match:%s:%s" % (match, self.current_user["id"]))
if match_exists:
continue
else:
pipe.sadd("match_likes:%s:%s" % (match, self.current_user["id"]), like)
match_list.append(match)
yield tornado.gen.Task(pipe.execute)
for match in match_list:
like_dict = {cat: [] for cat in categories}
person = yield tornado.gen.Task(redis.hgetall, "users:%s" % match)
likes = yield tornado.gen.Task(redis.smembers, "match_likes:%s:%s" %(match, self.current_user["id"]))
for like in likes:
cat, lid = like.split(":")
raw = yield tornado.gen.Task(redis.hgetall, like)
like_dict[cat].append(raw)
person["likes"] = like_dict
pipe.hmset("match:%s:%s" % (match, self.current_user["id"]), person)
pipe.zadd("matches:%s" % self.current_user["id"], match_list.count(match), "match:%s:%s" % (match, self.current_user["id"]))
yield tornado.gen.Task(pipe.execute)
#signals to angular that calculation has finished
def on_finish(self):
print "calc finished"
#signals angular that calc has started
def prepare(self):
print "calc started"
class MatchApi(BaseHandler, tornado.auth.FacebookGraphMixin):
@tornado.web.asynchronous
@tornado.web.authenticated
def get(self):
pipe = redis.pipeline()
matches = yield tornado.gen.Task(redis.zrevrange, "matches:%s" % (self.current_user["id"]), 0, -1, with_scores=False)
match_list = []
for match in matches:
person = yield tornado.gen.Task(redis.hgetall, match)
match_list.append(person)
try:
#deprecate partner.html as we switch to angular for the ui
#self.render("partner.html", top_match=match_list[0], matches=match_list)
#first in match_list is the
self.write(tornado.escape.json_encode(match_list))
except IndexError:
self.write(None)
class PrivacyHandler(BaseHandler):
def get(self):
logging.info(self.request.headers)
return self.render("privacy.html")
class TermsHandler(BaseHandler):
def get(self):
logging.info(self.request.headers)
return self.render("terms.html")
class PartnerModule(tornado.web.UIModule):
#may need to deprecate this module, if we're going the angular route
def render(self, partner, css="", interests=False):
return self.render_string("modules/partner.html",
partner=partner,
css=css,
interests=interests)
class ContenderModule(tornado.web.UIModule):
def render(self, contenders):
self.render_string("modules/contender.html", contenders=contenders)
def main():
tornado.web.ErrorHandler = ExceptionHandler
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
logging.info("listening on :%s" % options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()