-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.wsgi
More file actions
731 lines (550 loc) · 19.5 KB
/
app.wsgi
File metadata and controls
731 lines (550 loc) · 19.5 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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
import os, sys
#Uncomment for deployment using mod_wsgi or Passenger
os.chdir(os.path.dirname(__file__))
sys.path.append('.')
import shutil, subprocess, json, time, urllib, csv, tempfile, itertools, hashlib, zipfile
import cgi
import datetime
from datetime import date
from base64 import b64decode, b64encode
import bottle
from bottle import get, post, response, request, run, route, template, static_file, redirect, SimpleTemplate
from cork import Cork
from beaker.middleware import SessionMiddleware
from cork.backends import MongoDBBackend
import logging
import tps
import redis
import random
import requests
from rq import Queue
import fbminer_dp as fbm
from interactions import offline_get_interactions
q = Queue(connection=redis.Redis())
from default_config import config as def_config
r = redis.Redis()
# FB app auth
FACEBOOK_APP_ID = "333451740118555"
FACEBOOK_APP_SECRET = "f457263516b17536bca5981730737f1f"
REDIRECT_URL = "http://friendlyisland.info/appauth"
EXTENDED_PERMS = [
"user_about_me",
"friends_about_me",
"user_activities",
"friends_activities",
"user_birthday",
"friends_birthday",
"user_education_history",
"friends_education_history",
"user_events",
"friends_events",
"user_groups",
"friends_groups",
"user_hometown",
"friends_hometown",
"user_interests",
"friends_interests",
"user_likes",
"friends_likes",
"user_location",
"friends_location",
"user_notes",
"friends_notes",
"user_photo_video_tags",
"friends_photo_video_tags",
"user_photos",
"friends_photos",
"user_relationships",
"friends_relationships",
"user_status",
"friends_status",
"user_videos",
"friends_videos",
"read_friendlists",
"read_requests",
"read_stream",
"user_checkins",
"friends_checkins",
"read_mailbox"
]
logging.basicConfig(format='localhost - - [%(asctime)s] %(message)s', level=logging.DEBUG)
log = logging.getLogger(__name__)
bottle.debug(False)
arrows = {"ocean": "turtle", "island": "coconut", "space": "asteroid"}
session_opts = {
'session.type': 'cookie',
'session.validate_key': True,
'session.cookie_expires': True,
'session.timeout': 3600 * 24, # 1 day
'session.encrypt_key': 'couchhorse',
'session.auto': True
}
try:
backend = MongoDBBackend(db_name='friendly', initialize=False)
except:
backend = None
aaa = Cork(backend=backend)
application = bottle.default_app()
application = SessionMiddleware(application, session_opts)
def postd():
return bottle.request.forms
def post_get(name, default=''):
return bottle.request.POST.get(name, default).strip()
def sesh_redir(msg="Please log in to continue."):
"""
Helper function for setting redirect in the session.
"""
sess = request.environ.get("beaker.session")
sess['redir'] = request.path.lstrip("/")
sess['redir_msg'] = msg
return sess
@route('/get_dID')
def get_pid():
pid = r.get('dpid')
r.incr('dpid')
return 'pid=DP%s' % pid
@route("/my-data")
def show_logs():
if aaa.user_is_anonymous:
sesh_redir()
bottle.redirect("/login")
user = aaa.current_user
apps = {}
if request.query.filter:
app_list = [ app.strip() for app in request.query.filter.split(",") ]
else:
app_list = user.apps.split(',')
if len(app_list) > 0:
for appID in app_list:
apps[appID] = [ log for log in os.listdir("logs") if log.startswith(appID) and log.endswith(".json") ]
aaa.sort_nicely(apps[appID])
return template("my_data", apps=apps, user=user)
@route('/logout')
def logout():
aaa.logout(success_redirect='/login')
@route("/login")
def login():
if not aaa.user_is_anonymous:
bottle.redirect("/profile")
auth = request.query.auth
sess = request.environ.get("beaker.session")
if sess.has_key("redir_msg"):
msg = sess['redir_msg']
return template("login", error=msg)
else:
return template("login")
@post("/login")
def do_login():
auth = request.query.auth
sess = request.environ.get("beaker.session")
d = postd().dict
if sess.has_key("redir"):
success_redirect = sess['redir']
fail_redirect = "/login"
else:
success_redirect = "/profile"
fail_redirect = "/login"
aaa.login(d["username"][0], d["password"][0], success_redirect=success_redirect, fail_redirect=fail_redirect)
@route("/profile")
def show_profile():
if aaa.user_is_anonymous:
sesh_redir()
bottle.redirect("/login")
return template("profile", user=aaa.current_user, apps=aaa.list_apps(user=aaa.current_user))
@route("/admin")
def show_admin():
if aaa.user_is_anonymous:
sesh_redir()
bottle.redirect("/login")
aaa.require(role="admin", fail_redirect="/profile")
users = aaa.list_users()
apps = aaa.list_apps()
data = aaa.list_data()
return template("admin", user=aaa.current_user, apps=apps, users=users, data=data)
@route("/get_friends/<pID>")
def get_friends(pID):
try:
log = json.load(open("logs/dp_%s.json" % pID, 'rU'))
top6 = [ {'name': friend['name2']} for friend in log['friends'] if friend.has_key('name2')]
return json.dumps(top6)
except:
return "ERROR"
@post("/delete_app")
def delete_app():
if aaa.user_is_anonymous:
sesh_redir()
bottle.redirect("/login")
appID = post_get('appID')
msg = {}
if appID is not "":
try:
aaa.delete_app(appID)
msg['text'] = "App %s has been deleted" % appID
msg['type'] = "success"
except:
msg['text'] = "Sorry, app %s could not be deleted." % appID
msg['type'] = "error"
else:
msg['text'] = "Invalid app ID."
msg['type'] = "error"
return template("profile_apps_table", apps=aaa.list_apps(user=aaa.current_user), msg=msg)
@post("/validate")
def validate():
username = post_get('username')
appID = post_get('appID')
if username is not "":
if aaa.user(username):
return "false"
return "true"
if appID is not "":
if not aaa.check_apps_for(appID):
response.status = 500
return "That appID already exists."
if len( appID.split() ) > 1:
response.status = 500
return "Please remove any spaces from the app ID."
@route("/register")
def register():
if not aaa.user_is_anonymous:
bottle.redirect("/profile")
return template("register")
@post("/register")
def do_register():
d = postd().dict
if len(d["organization"][0]) > 0:
org = d["organization"][0]
else:
org = ""
try:
aaa.register(d["username"][0], d["first_name"][0], d["last_name"][0], d["password"][0], d["email_addr"][0], org)
except:
response.status = 500
return "Sorry, there was an error during registration. Please contact communication.neuroscience@gmail.com or try again later."
aaa.login(d["username"][0], d["password"][0], success_redirect="/profile")
@route("/load_config")
def load_config():
appID = request.query.appID
try:
config = aaa.load_app(appID)
except:
config = def_config
return config
@route('/configure')
def configure():
if aaa.user_is_anonymous:
sesh_redir()
bottle.redirect("/login")
categories = [ (cat["id"], cat["title"]) for cat in def_config["categories"] ]
components = [ (cat["id"], cat["title"]) for cat in def_config["components"] ]
components.insert(2, ("survey", "Survey"))
return template('config.tpl', user=aaa.current_user, categories=categories, components=components)
@post('/configure')
def do_config():
if aaa.user_is_anonymous:
sesh_redir()
bottle.redirect("/login")
#Create config dictionary
cData = { "created": datetime.datetime.now().strftime("%x") }
cData["owner"] = aaa.current_user.username
#Create appID, might be overwritten later
x = hashlib.sha1()
x.update(datetime.datetime.now().strftime("%c"))
appID = x.hexdigest()[:10].lower()
#Make sure appID doesn't exist
while not aaa.check_apps_for(appID):
x.update(datetime.datetime.now().strftime("%c"))
appID = x.hexdigest()[:10].lower()
#Get data
d = postd().dict
upload = request.files.get('file')
if upload:
try:
survey_dict = json.loads(upload.file.read())
except:
response.status = 500
return '<p>There was a problem parsing your JSON file.<p><p>Please make sure you submit a well-formed JSON file. Check out the <a href="assets/friendly/surveys_example.json" target="_blank">example</a> or the <a href="assets/friendly/surveys_template.json" target="_blank">template</a>.'
#####
#Build config dictionary
#####
#Set theme
cData["theme"] = d["theme"][0]
cData["arrowType"] = arrows[cData["theme"]]
#Set max friends or default
try:
cData['maxFriendsPerCategory'] = int(d['max'][0])
except:
cData['maxFriendsPerCategory'] = 20
#Set time frame for FB interactions
try:
cData['timeFrameNum'] = int(d['timeFrameNum'][0])
cData['timeFrameType'] = d['timeFrameType'][0]
except:
cData['timeFrameNum'] = 1
cData['timeFrameType'] = "weeks"
#Set appID
if len(d['appID'][0]) > 0:
appID = d['appID'][0].lower()
cData["appID"] = appID
#Set description
cData["description"] = d["description"][0]
#Set categories or default
cData["categories"] = []
if d['categories'][0] is not "":
cats = d["categories"][0].split(",")
for cat in cats:
for each in def_config["categories"]:
if each["id"] == cat:
cData["categories"].append(each)
else:
cData["categories"] = def_config["categories"]
#Set components or default
if len(d['components'][0]) > 0:
cData["components"] = []
comps = d["components"][0].split(",")
for comp in comps:
if comp == "survey" and upload:
cData["components"].append({
"id": comp,
"title": "Describe",
"help": ["Please respond to the following question for each person."],
"surveys": survey_dict["surveys"]
})
else:
for each in def_config["components"]:
if each["id"] == comp:
cData["components"].append(each)
else:
cData["components"] = def_config["components"]
#Save config file
config_filename = "%s.py" % appID
try:
aaa.save_app(cData)
response.status = 200
except:
response.status = 500
return "<p>We were unable to save your app. Please try again later.</p>"
return template("config_success", appID=appID)
@post('/get_interactions')
def get_interaction():
access_token = post_get('access_token')
APP_ID='333451740118555'
APP_SECRET='f457263516b17536bca5981730737f1f'
# get extended time token
# https://graph.facebook.com/oauth/access_token?
# client_id=APP_ID&
# client_secret=APP_SECRET&
# grant_type=fb_exchange_token&
# fb_exchange_token=EXISTING_ACCESS_TOKEN
url = 'https://graph.facebook.com/oauth/access_token?client_id=%s&client_secret=%s&grant_type=fb_exchange_token&fb_exchange_token=%s'
url = url % (APP_ID, APP_SECRET, access_token)
req=urllib.urlopen(url)
data = cgi.parse_qs(req.read())
new_access_token = data['access_token'][-1]
# store the access token in data folder - this can be made optional
pid = post_get('pID')
write_access_token(pid,access_token,'atok_bu')
write_access_token(pid,new_access_token)
timeframe_num = int(post_get('timeFrameNum'))
timeframe_type = post_get('timeFrameType')
ordered = post_get('ordered',False)
tps.stored_access_token = access_token
if timeframe_type == 'days':
timeframe = datetime.timedelta(days=timeframe_num)
else:
timeframe = datetime.timedelta(weeks=timeframe_num)
start_date = datetime.datetime.today() - timeframe
response = {}
try:
friends = tps.get_interactions_from_last(access_token, start_date, ordered=ordered)
print friends
response['response'] = 'true'
response['fbFriends'] = friends
except:
response['response'] = 'false'
token_path = '%s/%s.access_token' % (pid, pid)
q.enqueue(fbm.main, token_path, timeout=17200)
return json.dumps(response)
@route('/get_stored_interactions/<pID>')
def stored_interactions(pID):
response = {}
try:
response = json.load(open('data/%s/%s_interactions.json' % (pID,pID)))
except:
response['response']='false'
return json.dumps(response)
@route('/channel')
def channel():
return render('<script src="//connect.facebook.net/en_US/all.js"></script>')
@route('/')
def index():
if request.query.appID:
appID = request.query.appID
try:
config = aaa.load_app(appID.lower())
except:
print "Unable to load config for %s" % appID
config = def_config
else:
config = def_config
appID='default'
if request.query.pID:
pID = request.query.pID
else:
pID = "anon"
if request.query.theme:
themes = ["ocean", "island", "space"]
if request.query.theme in themes:
config['theme'] = request.query.theme
config['arrowType'] = arrows[config['theme']]
if os.path.exists('views/index_%s.tpl' % appID):
return template('index_%s.tpl' % appID, pID=pID, config=config)
else:
return template('index', pID=pID, config=config)
@route('/assets/<file_path:path>')
def static(file_path):
return static_file(file_path, root="assets/")
@post("/view-file")
def view_file():
if aaa.user_is_anonymous:
sesh_redir()
bottle.redirect("/login")
logs = os.listdir("logs")
file_name = post_get("file")
for file in logs:
if file_name == file:
file_out = json.loads(open("logs/%s" % file_name, "rU").read())
file_out = json.dumps(file_out, indent=2)
return file_out
return "<p>Sorry, %s not found.</p>" % file_name
@route("/download-data")
def download_data():
if aaa.user_is_anonymous:
sesh_redir()
bottle.redirect("/login")
user = aaa.current_user
if request.query.type and request.query.file:
type = request.query.type
file = request.query.file
if type == "all":
if file not in user.apps.split(','):
return "<p>Sorry, that app belongs to another user.</p>"
zfn = "%s-data.zip" % file
zpath = "logs/archives/%s" % zfn
logs = [ "logs/%s"%log for log in os.listdir("logs") if log.startswith(file) and log.endswith(".json")]
if len(logs) == 0:
return "<p>Sorry, no data files were found.</p>"
elif not os.path.exists(zpath):
zf = zipfile.ZipFile(zpath, "w")
for d in logs:
zf.write(d)
zf.close()
elif os.path.exists(zpath):
zf = zipfile.ZipFile(zpath, "a")
old_logs = zf.namelist()
new_logs = set(logs).difference(old_logs)
if len(new_logs) > 0:
for d in set(logs).difference(old_logs):
zf.write(d)
zf.close()
return static_file(zfn, root="logs/archives/", download=True)
elif type == "one":
return static_file(file, root="logs/", download=True)
else:
response.status = 404
return
else:
response.status = 404
return
@post('/log')
def write_log():
try:
data=json.loads(request.body.read())
log_file = check_unique("%s_%s" % (data['appID'],data['pID']))
log = open('logs/%s.json' % log_file, 'w')
log.write(json.dumps(data))
log.close()
request.response = 200
# create completion code
if data['appID'] in ('dp','dp2'):
return '%s#%i' % (data['pID'], random.randint(1000,9000))
except:
request.response = 500
def write_access_token(user_id,access_token,filename=''):
fn = ('%s.access_token' % user_id) if filename=='' else filename
if os.path.exists('data/%s' % (user_id)):
if os.path.exists('data/%s/%s' % (user_id,fn)):
# check if access_tokens are the same
stored_access_token = open('data/%s/%s' % (user_id,fn)).read().strip()
else:
stored_access_token = None
if not access_token == stored_access_token:
out = open('data/%s/%s' % (user_id, fn), 'w')
out.write(access_token)
out.close()
else:
os.mkdir('data/%s' % user_id)
out = open('data/%s/%s' % (user_id, fn), 'w')
out.write(access_token)
out.close()
def check_unique(fname,suffix=1):
fname_new=fname
while (os.path.exists('logs/%s.json' % fname_new)):
fname_new = '%s_%i' % (fname,suffix)
suffix+=1
return fname_new
# FB app authentication outside of Friendly Island app
@route('/appauth')
def appauth():
args = dict(client_id=FACEBOOK_APP_ID, redirect_uri=REDIRECT_URL)
if request.query.code: # return from first OAuth call
args["client_secret"] = FACEBOOK_APP_SECRET
args["code"] = request.query.code
user_id = request.query.state
response = cgi.parse_qs(urllib.urlopen(
"https://graph.facebook.com/oauth/access_token?" +
urllib.urlencode(args)).read())
access_token = response["access_token"][-1]
expires = response["expires"][-1]
if os.path.exists('data/%s' % user_id):
# check if access_tokens are the same
stored_access_token = open('data/%s/%s.access_token' % (user_id,user_id)).read().strip()
if not access_token == stored_access_token:
out = open('data/%s/%s.access_token' % (user_id, user_id), 'w')
out.write(access_token)
out.close()
#yield "USER ALREADY AUTHENTICATED and " + str(access_token == stored_access_token)
else:
os.mkdir('data/%s' % user_id)
out = open('data/%s/%s.access_token' % (user_id, user_id), 'w')
out.write(access_token)
out.close()
#yield "OK "
# set queue job to get interactions
job=q.enqueue(offline_get_interactions, user_id, access_token, timeout=7200)
print job.result
# create completion code
ccode = '%s#%i' % (user_id, random.randint(1000,9000))
return template('auth', uid=user_id, completion_code=ccode)
else: # no code param so make intial OAuth call
user_id = request.query.pID
args["scope"] = ",".join(EXTENDED_PERMS)
args["state"] = user_id
redirect("https://graph.facebook.com/oauth/authorize?" + urllib.urlencode(args))
def Xoffline_get_interactions(pID, access_token):
print "****", pID, access_token
data = {'access_token': access_token,
'timeFrameNum': 12,
'timeFrameType': 'weeks',
'ordered': 40
}
url = 'http://friendlyisland.info/get_interactions'
req = requests.post(url, data)
with open('data/%s/%s_interactions.json' % (pID,pID),'w') as out:
out.write(req.read())
# # Web application main # #
def main():
# Start the Bottle webapp
bottle.debug(False)
bottle.run(app=application, quiet=False, reloader=True)
if __name__ == "__main__":
main()