def getCloseFriends(): accessToken = request.args.get('a', '', type=unicode) uid = request.args.get('b', '', type=unicode) print 'uid='+uid print 'in get close friends' print 'checking db' update_flag = 0 users = session.query(User) for cur_user in users: print cur_user.uid, cur_user.modified_time if cur_user.uid == uid: print datetime.datetime.now() time_diff = datetime.datetime.now()-cur_user.modified_time print 'time diff: ',time_diff.total_seconds() if time_diff.total_seconds() > 2628000: update_flag = 1 break else: # fetch cur_user.set_of_scores print '################## Fetching from DB' print cur_user.set_of_scores, type(cur_user.set_of_scores) top_twenty = list() for list_elem in cur_user.set_of_scores: profile = json.loads(fetch_fb_functions.getProfile(accessToken, list_elem.friend_uid)) print 'PROFILE: ', profile print type(profile) print profile.keys() print 'score: ',list_elem.score if 'statuses' in profile.keys(): print '^^^^^^ &&&&&&& ', profile['statuses']['data'][0]['updated_time'] recent_status={} recent_status['updated_time'] = profile['statuses']['data'][0]['updated_time'] recent_status['message'] = profile['statuses']['data'][0]['message'] print '/n/n **** RECENT ', recent_status if 'relationship_status' in profile.keys(): top_twenty.append({'id': list_elem.friend_uid, 'score': list_elem.score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': profile['relationship_status'], 'link': profile['link'], 'picture': profile['picture']['data'], 'status': recent_status}) else: top_twenty.append({'id': list_elem.friend_uid, 'score': list_elem.score, 'name': profile['name'], 'gender': profile['gender'], 'link': profile['link'], 'picture': profile['picture']['data'], 'status': recent_status}) else: if 'relationship_status' in profile.keys(): top_twenty.append({'id': list_elem.friend_uid, 'score': list_elem.score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': profile['relationship_status'], 'link': profile['link'], 'picture': profile['picture']['data']}) else: top_twenty.append({'id': list_elem.friend_uid, 'score': list_elem.score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': 'nill', 'link': profile['link'], 'picture': profile['picture']['data'] }) print '****><><><>',top_twenty, type(top_twenty) session.end() return json.dumps(top_twenty) print '@@@@@@@@@@@@@@@@@ Calculating' my_friends = fetch_fb_functions.getFriends(accessToken) if 'friends' not in my_friends.keys(): print 'no friends' return json.dumps('') #print 'my friends' #print my_friends score_calculation.init_scores(my_friends['friends']['data']) my_photos = fetch_fb_functions.getPhotos(accessToken) if my_photos != None: print 'my_photos' #print my_photos print my_photos['tagged'] score_calculation.update_scores(my_photos['tagged'], 3) print my_photos['liked by'] score_calculation.update_scores(my_photos['liked by'], 2) print my_photos['commented by'] score_calculation.update_scores(my_photos['commented by'], 2) my_checkins = fetch_fb_functions.getCheckins(accessToken, uid) print 'my_checkins' if my_checkins != None: print my_checkins['from'] score_calculation.update_scores(my_checkins['from'], 4) print my_checkins['tagged'] score_calculation.update_scores(my_checkins['tagged'], 4) my_feeds = fetch_fb_functions.getFeed(accessToken) print 'my_feeds' print my_feeds['tagged'] score_calculation.update_scores(my_feeds['tagged'], 3) print my_feeds['liked by'] score_calculation.update_scores(my_feeds['liked by'], 2) print my_feeds['commented by'] score_calculation.update_scores(my_feeds['commented by'], 2) my_family = fetch_fb_functions.getFamily(accessToken) print 'my_family' if my_family != None: print my_family score_calculation.update_scores_family(my_family, 3) my_status = fetch_fb_functions.get_status(accessToken, uid) print 'my_status' print my_status['tagged'] score_calculation.update_scores(my_status['tagged'], 3) print my_status['liked by'] score_calculation.update_scores(my_status['liked by'], 2) print my_status['commented by'] score_calculation.update_scores(my_status['commented by'], 2) my_links = fetch_fb_functions.getLinks(accessToken) print 'my_links' print my_links['tagged'] score_calculation.update_scores(my_links['tagged'], 3) print my_links['liked by'] score_calculation.update_scores(my_links['liked by'], 2) print my_links['commented by'] score_calculation.update_scores(my_links['commented by'], 2) my_inbox = fetch_fb_functions.getInbox(accessToken, uid) if my_inbox != None: print 'my_inbox' print my_inbox score_calculation.update_scores_inbox(my_inbox, 3) sorted_score_list = score_calculation.show_scores() print '++++',sorted_score_list if len(sorted_score_list) >=20: top_twenty_friends = sorted_score_list[len(sorted_score_list)-20:] else: top_twenty_friends = sorted_score_list highest_index = len(top_twenty_friends)-1 print '?????? $$$$$$', top_twenty_friends top_twenty = list() highest = top_twenty_friends[highest_index][1] print 'highest: ', highest my_friends_scores=list() # set of scores to be stored in DB for list_elem in top_twenty_friends: profile = json.loads(fetch_fb_functions.getProfile(accessToken, list_elem[0])) print 'PROFILE: ', profile print type(profile) print profile.keys() print 'old score: ',list_elem[1] new_score = (list_elem[1]/highest)*100 print'new score: ',new_score my_friends_scores.append(Score(friend_uid=list_elem[0], score=new_score)) if 'statuses' in profile.keys(): print '^^^^^^ &&&&&&& ', profile['statuses']['data'][0]['updated_time'] recent_status={} recent_status['updated_time'] = profile['statuses']['data'][0]['updated_time'] recent_status['message'] = profile['statuses']['data'][0]['message'] print '/n/n **** RECENT ', recent_status if 'relationship_status' in profile.keys(): top_twenty.append({'id': list_elem[0], 'score': new_score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': profile['relationship_status'], 'link': profile['link'], 'picture': profile['picture']['data'], 'status': recent_status}) else: top_twenty.append({'id': list_elem[0], 'score': new_score, 'name': profile['name'], 'gender': profile['gender'], 'link': profile['link'], 'picture': profile['picture']['data'], 'status': recent_status}) else: if 'relationship_status' in profile.keys(): top_twenty.append({'id': list_elem[0], 'score': new_score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': profile['relationship_status'], 'link': profile['link'], 'picture': profile['picture']['data']}) else: top_twenty.append({'id': list_elem[0], 'score': new_score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': 'nill', 'link': profile['link'], 'picture': profile['picture']['data'] }) store_user_record(uid, my_friends_scores, update_flag) print '****><><><>',top_twenty, type(top_twenty) session.end() return json.dumps(top_twenty)
def calculateClosest(uid, update_flag, accessToken): print 'Calculating' my_friends = fetch_fb_functions.getFriends(accessToken) if 'friends' not in my_friends.keys(): print 'no friends' return json.dumps('') #print 'my friends' #print my_friends score_list = {} sorted_score_list = {} friend_list = my_friends['friends']['data'] print 'init_scores' for current_friend in friend_list: score_list[current_friend['id']] = 1 #print score_list fetch_string = 'photos, checkins, feed, links, family, inbox.limit(500), statuses, friendlists.fields(members,list_type)' all_data = fetch_fb_functions.getData(accessToken, uid, fetch_string) my_friendlists = fetch_fb_functions.getFriendLists(accessToken, all_data) if my_friendlists != None: print 'my_friendlists' score_calculation.update_scores_friendlist(my_friendlists['close_friends'], 4, score_list) score_calculation.update_scores_friendlist(my_friendlists['work'], 1.1, score_list) score_calculation.update_scores_friendlist(my_friendlists['education'], 1.1, score_list) score_calculation.update_scores_friendlist(my_friendlists['user_created'], 1.1, score_list) score_calculation.update_scores_friendlist(my_friendlists['current_city'], 1.05, score_list) pics_by_me = [] pics_by_others = [] split_pics = fetch_fb_functions.splitPhotos(accessToken, all_data, uid, pics_by_me, pics_by_others) if pics_by_me != None: few_tags_pics_by_me = [] many_tags_pics_by_me = [] fetch_fb_functions.splitPhotosByTagCount(pics_by_me, few_tags_pics_by_me, many_tags_pics_by_me) by_me_photos = fetch_fb_functions.getPhotos(accessToken, pics_by_me, few_tags_pics_by_me, uid) print 'by_me_photos' #print my_photos print by_me_photos['tagged'] score_calculation.update_scores(by_me_photos['tagged'], 4, score_list) print by_me_photos['liked by'] score_calculation.update_scores(by_me_photos['liked by'], 2, score_list) print by_me_photos['commented by'] score_calculation.update_scores(by_me_photos['commented by'], 2, score_list) by_me_photos = fetch_fb_functions.getPhotos(accessToken, pics_by_me, many_tags_pics_by_me, uid) print 'by_me_photos' #print my_photos print by_me_photos['tagged'] score_calculation.update_scores(by_me_photos['tagged'], 3, score_list) print by_me_photos['liked by'] score_calculation.update_scores(by_me_photos['liked by'], 2, score_list) print by_me_photos['commented by'] score_calculation.update_scores(by_me_photos['commented by'], 2, score_list) if pics_by_others != None: few_tags_pics_by_others = [] many_tags_pics_by_others = [] fetch_fb_functions.splitPhotosByTagCount(pics_by_me, few_tags_pics_by_others, many_tags_pics_by_others) by_others_photos = fetch_fb_functions.getPhotos(accessToken, pics_by_others, few_tags_pics_by_others, uid) print 'by_others_photos' #print my_photos print by_others_photos['tagged'] score_calculation.update_scores(by_others_photos['tagged'], 3, score_list) print by_others_photos['liked by'] score_calculation.update_scores(by_others_photos['liked by'], 2, score_list) print by_others_photos['commented by'] score_calculation.update_scores(by_others_photos['commented by'], 2, score_list) by_others_photos = fetch_fb_functions.getPhotos(accessToken, pics_by_others, many_tags_pics_by_others, uid) print 'by_others_photos' #print my_photos print by_others_photos['tagged'] score_calculation.update_scores(by_others_photos['tagged'], 2.5, score_list) print by_others_photos['liked by'] score_calculation.update_scores(by_others_photos['liked by'], 2, score_list) print by_others_photos['commented by'] score_calculation.update_scores(by_others_photos['commented by'], 2, score_list) my_checkins = fetch_fb_functions.getCheckins(accessToken, uid, all_data) print 'my_checkins' if my_checkins != None: print my_checkins['from'] score_calculation.update_scores(my_checkins['from'], 4, score_list) print my_checkins['tagged'] score_calculation.update_scores(my_checkins['tagged'], 4, score_list) feed_by_me = [] feed_by_others = [] split_feed = fetch_fb_functions.splitFeed(accessToken, all_data, uid, feed_by_me, feed_by_others) if feed_by_me != None: my_feeds = fetch_fb_functions.getFeed(accessToken, feed_by_me) print 'feeds_by_me' print my_feeds['tagged'] score_calculation.update_scores(my_feeds['tagged'], 3.5, score_list) print my_feeds['liked by'] score_calculation.update_scores(my_feeds['liked by'], 2, score_list) print my_feeds['commented by'] score_calculation.update_scores(my_feeds['commented by'], 2, score_list) if feed_by_others != None: others_feeds = fetch_fb_functions.getFeed(accessToken, feed_by_others) print 'feeds_by_others' print others_feeds['tagged'] score_calculation.update_scores(others_feeds['tagged'], 3, score_list) print others_feeds['liked by'] score_calculation.update_scores(others_feeds['liked by'], 2, score_list) print others_feeds['commented by'] score_calculation.update_scores(others_feeds['commented by'], 2, score_list) my_family = fetch_fb_functions.getFamily(accessToken, all_data) print 'my_family' if my_family != None: print my_family score_calculation.update_scores_family(my_family, 3, score_list) my_status = fetch_fb_functions.get_status(accessToken, uid, all_data) if my_status != None: print 'my_status' print my_status['tagged'] score_calculation.update_scores(my_status['tagged'], 3, score_list) print my_status['liked by'] score_calculation.update_scores(my_status['liked by'], 2, score_list) print my_status['commented by'] score_calculation.update_scores(my_status['commented by'], 2, score_list) my_links = fetch_fb_functions.getLinks(accessToken, all_data) if my_links != None: print 'my_links' print my_links['tagged'] score_calculation.update_scores(my_links['tagged'], 3, score_list) print my_links['liked by'] score_calculation.update_scores(my_links['liked by'], 2, score_list) print my_links['commented by'] score_calculation.update_scores(my_links['commented by'], 2, score_list) my_inbox = fetch_fb_functions.getInbox(accessToken, uid, all_data) if my_inbox != None: print 'my_inbox' print my_inbox score_calculation.update_scores_inbox(my_inbox, 3, score_list) sorted_score_list = score_calculation.show_scores(score_list) print '++++',sorted_score_list if len(sorted_score_list) >=20: top_twenty_friends = sorted_score_list[len(sorted_score_list)-20:] else: top_twenty_friends = sorted_score_list top_twenty = fetchClosestProfiles(top_twenty_friends, update_flag, uid, accessToken) return top_twenty
def getCloseFriends(): accessToken = request.args.get("a", "", type=unicode) uid = request.args.get("b", "", type=unicode) print "uid=" + uid print "in get close friends" print "checking db" update_flag = 0 users = session.query(User) for cur_user in users: print cur_user.uid, cur_user.modified_time if cur_user.uid == uid: print datetime.datetime.now() time_diff = datetime.datetime.now() - cur_user.modified_time print "time diff: ", time_diff.total_seconds() if time_diff.total_seconds() > 2628000: update_flag = 1 break else: # fetch cur_user.set_of_scores print "################## Fetching from DB" print cur_user.set_of_scores, type(cur_user.set_of_scores) top_twenty = list() for list_elem in cur_user.set_of_scores: profile = json.loads(fetch_fb_functions1.getProfile(accessToken, list_elem.friend_uid)) print "PROFILE: ", profile print type(profile) print profile.keys() print "score: ", list_elem.score if "relationship_status" in profile.keys(): top_twenty.append( { "id": list_elem.friend_uid, "score": list_elem.score, "name": profile["name"], "gender": profile["gender"], "relationship_status": profile["relationship_status"], "link": profile["link"], "picture": profile["picture"]["data"], } ) else: top_twenty.append( { "id": list_elem.friend_uid, "score": list_elem.score, "name": profile["name"], "gender": profile["gender"], "relationship_status": "nill", "link": profile["link"], "picture": profile["picture"]["data"], } ) print "****><><><>", top_twenty, type(top_twenty) session.end() return json.dumps(top_twenty) print "@@@@@@@@@@@@@@@@@ Calculating" my_friends = fetch_fb_functions1.getFriends(accessToken) if "friends" not in my_friends.keys(): print "no friends" return json.dumps("") # print 'my friends' # print my_friends score_calculation.init_scores(my_friends["friends"]["data"]) my_photos = fetch_fb_functions1.getPhotos(accessToken) if my_photos != None: print "my_photos" # print my_photos print my_photos["tagged"] score_calculation.update_scores(my_photos["tagged"], 3) print my_photos["liked by"] score_calculation.update_scores(my_photos["liked by"], 2) print my_photos["commented by"] score_calculation.update_scores(my_photos["commented by"], 2) my_checkins = fetch_fb_functions1.getCheckins(accessToken, uid) print "my_checkins" if my_checkins != None: print my_checkins["from"] score_calculation.update_scores(my_checkins["from"], 4) print my_checkins["tagged"] score_calculation.update_scores(my_checkins["tagged"], 4) my_feeds = fetch_fb_functions1.getFeed(accessToken) print "my_feeds" print my_feeds["tagged"] score_calculation.update_scores(my_feeds["tagged"], 3) print my_feeds["liked by"] score_calculation.update_scores(my_feeds["liked by"], 2) print my_feeds["commented by"] score_calculation.update_scores(my_feeds["commented by"], 2) my_family = fetch_fb_functions1.getFamily(accessToken) print "my_family" if my_family != None: print my_family score_calculation.update_scores_family(my_family, 3) my_status = fetch_fb_functions1.get_status(accessToken, uid) print "my_status" print my_status["tagged"] score_calculation.update_scores(my_status["tagged"], 3) print my_status["liked by"] score_calculation.update_scores(my_status["liked by"], 2) print my_status["commented by"] score_calculation.update_scores(my_status["commented by"], 2) my_links = fetch_fb_functions1.getLinks(accessToken) print "my_links" print my_links["tagged"] score_calculation.update_scores(my_links["tagged"], 3) print my_links["liked by"] score_calculation.update_scores(my_links["liked by"], 2) print my_links["commented by"] score_calculation.update_scores(my_links["commented by"], 2) my_inbox = fetch_fb_functions1.getInbox(accessToken, uid) if my_inbox != None: print "my_inbox" print my_inbox score_calculation.update_scores_inbox(my_inbox, 3) sorted_score_list = score_calculation.show_scores() print "++++", sorted_score_list if len(sorted_score_list) >= 20: top_twenty_friends = sorted_score_list[len(sorted_score_list) - 20 :] else: top_twenty_friends = sorted_score_list highest_index = len(top_twenty_friends) - 1 print "?????? $$$$$$", top_twenty_friends top_twenty = list() highest = top_twenty_friends[highest_index][1] print "highest: ", highest my_friends_scores = list() # set of scores to be stored in DB for list_elem in top_twenty_friends: profile = json.loads(fetch_fb_functions1.getProfile(accessToken, list_elem[0])) print "PROFILE: ", profile print type(profile) print profile.keys() print "old score: ", list_elem[1] new_score = (list_elem[1] / highest) * 100 print "new score: ", new_score my_friends_scores.append(Score(friend_uid=list_elem[0], score=new_score)) if "relationship_status" in profile.keys(): top_twenty.append( { "id": list_elem[0], "score": new_score, "name": profile["name"], "gender": profile["gender"], "relationship_status": profile["relationship_status"], "link": profile["link"], "picture": profile["picture"]["data"], } ) else: top_twenty.append( { "id": list_elem[0], "score": new_score, "name": profile["name"], "gender": profile["gender"], "relationship_status": "nill", "link": profile["link"], "picture": profile["picture"]["data"], } ) store_user_record(uid, my_friends_scores, update_flag) print "****><><><>", top_twenty, type(top_twenty) session.end() return json.dumps(top_twenty)
def getCloseFriends(): accessToken = request.args.get('a', '', type=unicode) uid = request.args.get('b', '', type=unicode) print 'uid='+uid print 'in get close friends' print '@@@@@@@@@@@@@@@@@ Calculating' my_friends = fetch_fb_functions.getFriends(accessToken) if 'friends' not in my_friends.keys(): print 'no friends' return json.dumps('') #print 'my friends' #print my_friends score_calculation.init_scores(my_friends['friends']['data']) fetch_string = 'photos, checkins, feed, links, family, inbox.limit(500), statuses' data = fetch_fb_functions.getData(accessToken, uid, fetch_string) my_photos = fetch_fb_functions.getPhotos(accessToken) if my_photos != None: print 'my_photos' #print my_photos print my_photos['tagged'] score_calculation.update_scores(my_photos['tagged'], 3) print my_photos['liked by'] score_calculation.update_scores(my_photos['liked by'], 2) print my_photos['commented by'] score_calculation.update_scores(my_photos['commented by'], 2) my_checkins = fetch_fb_functions.getCheckins(accessToken, uid) print 'my_checkins' if my_checkins != None: print my_checkins['from'] score_calculation.update_scores(my_checkins['from'], 4) print my_checkins['tagged'] score_calculation.update_scores(my_checkins['tagged'], 4) my_feeds = fetch_fb_functions.getFeed(accessToken) if my_feeds != None: print 'my_feeds' print my_feeds['tagged'] score_calculation.update_scores(my_feeds['tagged'], 3) print my_feeds['liked by'] score_calculation.update_scores(my_feeds['liked by'], 2) print my_feeds['commented by'] score_calculation.update_scores(my_feeds['commented by'], 2) my_family = fetch_fb_functions.getFamily(accessToken) print 'my_family' if my_family != None: print my_family score_calculation.update_scores_family(my_family, 3) my_status = fetch_fb_functions.get_status(accessToken, uid) if my_status != None: print 'my_status' print my_status['tagged'] score_calculation.update_scores(my_status['tagged'], 3) print my_status['liked by'] score_calculation.update_scores(my_status['liked by'], 2) print my_status['commented by'] score_calculation.update_scores(my_status['commented by'], 2) my_links = fetch_fb_functions.getLinks(accessToken) if my_links != None: print 'my_links' print my_links['tagged'] score_calculation.update_scores(my_links['tagged'], 3) print my_links['liked by'] score_calculation.update_scores(my_links['liked by'], 2) print my_links['commented by'] score_calculation.update_scores(my_links['commented by'], 2) my_inbox = fetch_fb_functions.getInbox(accessToken, uid) if my_inbox != None: print 'my_inbox' print my_inbox score_calculation.update_scores_inbox(my_inbox, 3) sorted_score_list = score_calculation.show_scores() print '++++',sorted_score_list if len(sorted_score_list) >=20: top_twenty_friends = sorted_score_list[len(sorted_score_list)-20:] else: top_twenty_friends = sorted_score_list highest_index = len(top_twenty_friends)-1 print '?????? $$$$$$', top_twenty_friends highest = top_twenty_friends[highest_index][1] print 'highest: ', highest my_friends_scores=list() # set of scores to be stored in DB start_time = time.time() global q global concurrent q=Queue(concurrent*2) for i in range(concurrent): t=Thread(target=doWork) t.daemon=True t.start() try: for list_elem in top_twenty_friends: url = "https://graph.facebook.com/" + list_elem[0] + "?fields=id, name, gender, relationship_status, picture.height(200).width(200), link, statuses.limit(1).fields(message,updated_time) &access_token="+accessToken print 'old score: ',list_elem[1] new_score = (list_elem[1]/highest)*100 print'new score: ',new_score q.put((list_elem[0], url.strip(), new_score)) # insert tuple(uid, url, new_score) in queue my_friends_scores.append(Score(friend_uid=list_elem[0], score=new_score)) q.join() except KeyboardInterrupt: sys.exit(1) print time.time() - start_time, "seconds" global top_twenty print '****><><><>',top_twenty, type(top_twenty) return json.dumps(top_twenty)
def facebook_authorized(resp): if resp is None: return 'Access denied: reason=%s error=%s' % ( request.args['error_reason'], request.args['error_description'] ) session['oauth_token'] = (resp['access_token'], '') me = facebook.get('/me') my_id = getUID(resp['access_token']) print my_id #my_profile = getProfile(my_id) #print my_profile my_friends = getFriends(resp['access_token'], my_id) print my_friends['data'] #score_list = score_calculation.init_scores(my_friends['data']) #print score_list score_calculation.init_scores(my_friends['data']) #permi = checkPermissions(my_id, resp['access_token']) #print permi['permissions']['data'][0] #if 'read_mailbox' in permi['permissions']['data'][0]: # getMessages(my_id, resp['access_token']) my_photos = getPhotos(my_id, resp['access_token']) print my_photos print my_photos['tagged'] score_calculation.update_scores(my_photos['tagged'], 75) print my_photos['liked by'] score_calculation.update_scores(my_photos['liked by'], 35) print my_photos['commented by'] score_calculation.update_scores(my_photos['commented by'], 35) my_checkins = getCheckins(my_id, resp['access_token']) print my_checkins print my_checkins['from'] score_calculation.update_scores(my_checkins['from'], 100) print my_checkins['tagged'] score_calculation.update_scores(my_checkins['tagged'], 100) my_feeds = getFeed(my_id, resp['access_token']) print my_feeds print my_feeds['tagged'] score_calculation.update_scores(my_feeds['tagged'], 75) print my_feeds['liked by'] score_calculation.update_scores(my_feeds['liked by'], 35) print my_feeds['commented by'] score_calculation.update_scores(my_feeds['commented by'], 35) my_family = getFamily(my_id, resp['access_token']) print my_family score_calculation.update_scores_family(my_family, 75) my_status = get_status(my_id,resp['access_token']) print my_status print my_status['tagged'] score_calculation.update_scores(my_status['tagged'], 75) print my_status['liked by'] score_calculation.update_scores(my_status['liked by'], 35) print my_status['commented by'] score_calculation.update_scores(my_status['commented by'], 35) my_links = getLinks(my_id, resp['access_token']) print my_links print my_links['tagged'] score_calculation.update_scores(my_links['tagged'], 75) print my_links['liked by'] score_calculation.update_scores(my_links['liked by'], 35) print my_links['commented by'] score_calculation.update_scores(my_links['commented by'], 35) my_inbox = getInbox(my_id,resp['access_token']) print my_inbox score_calculation.update_scores_inbox(my_inbox, 75) return 'Logged in as id=%s name=%s redirect=%s access token=%s data=%s' % \ (me.data['id'], me.data['name'], request.args.get('next'), resp['access_token'], me.data)
def getCloseFriends(): accessToken = request.args.get("a", "", type=unicode) uid = request.args.get("b", "", type=unicode) print "uid=" + uid print "in get close friends" my_friends = fetch_fb_functions.getFriends(accessToken) # print 'my_friends' # print my_friends['friends']['data'] score_calculation.init_scores(my_friends["friends"]["data"]) my_photos = fetch_fb_functions.getPhotos(accessToken) print "my_photos" # print my_photos print my_photos["tagged"] score_calculation.update_scores(my_photos["tagged"], 75) print my_photos["liked by"] score_calculation.update_scores(my_photos["liked by"], 35) print my_photos["commented by"] score_calculation.update_scores(my_photos["commented by"], 35) my_checkins = fetch_fb_functions.getCheckins(accessToken, uid) print "my_checkins" if my_checkins != None: print my_checkins["from"] score_calculation.update_scores(my_checkins["from"], 100) print my_checkins["tagged"] score_calculation.update_scores(my_checkins["tagged"], 100) my_feeds = fetch_fb_functions.getFeed(accessToken) print "my_feeds" print my_feeds["tagged"] score_calculation.update_scores(my_feeds["tagged"], 75) print my_feeds["liked by"] score_calculation.update_scores(my_feeds["liked by"], 35) print my_feeds["commented by"] score_calculation.update_scores(my_feeds["commented by"], 35) my_family = fetch_fb_functions.getFamily(accessToken) print "my_family" if my_family != None: print my_family score_calculation.update_scores_family(my_family, 75) my_status = fetch_fb_functions.get_status(accessToken, uid) print "my_status" print my_status["tagged"] score_calculation.update_scores(my_status["tagged"], 75) print my_status["liked by"] score_calculation.update_scores(my_status["liked by"], 35) print my_status["commented by"] score_calculation.update_scores(my_status["commented by"], 35) my_links = fetch_fb_functions.getLinks(accessToken) print "my_links" print my_links["tagged"] score_calculation.update_scores(my_links["tagged"], 75) print my_links["liked by"] score_calculation.update_scores(my_links["liked by"], 35) print my_links["commented by"] score_calculation.update_scores(my_links["commented by"], 35) my_inbox = fetch_fb_functions.getInbox(accessToken, uid) print "my_inbox" print my_inbox score_calculation.update_scores_inbox(my_inbox, 75) sorted_score_list = score_calculation.show_scores() print "++++", sorted_score_list top_twenty_friends = sorted_score_list[len(sorted_score_list) - 20 :] print "????", top_twenty_friends top_twenty = list() for list_elem in top_twenty_friends: profile = json.loads(fetch_fb_functions.getProfile(accessToken, list_elem[0])) print "PROFILE: ", profile print type(profile) print profile.keys() if "relationship_status" in profile.keys(): top_twenty.append( { "id": list_elem[0], "score": list_elem[1], "name": profile["name"], "gender": profile["gender"], "relationship_status": profile["relationship_status"], "link": profile["link"], "picture": profile["picture"], } ) else: top_twenty.append( { "id": list_elem[0], "score": list_elem[1], "name": profile["name"], "gender": profile["gender"], "relationship_status": "nill", "link": profile["link"], "picture": profile["picture"], } ) print "****><><><>", top_twenty return jsonify(top_twenty)
def getCloseFriends(): accessToken = request.args.get('a', '', type=unicode) uid = request.args.get('b', '', type=unicode) print 'uid='+uid print 'in get close friends' granted_permissions = fetch_fb_functions1.checkPermissions(accessToken) print '\n\npermissions\n\n', granted_permissions['data'][0] print 'checking db' update_flag = 0 users = session.query(User) for cur_user in users: print cur_user.uid, cur_user.modified_time if cur_user.uid == uid: print datetime.datetime.now() time_diff = datetime.datetime.now()-cur_user.modified_time print 'time diff: ',time_diff.total_seconds() if time_diff.total_seconds() > 2628000: update_flag = 1 break else: # fetch cur_user.set_of_scores print '################## Fetching from DB' print cur_user.set_of_scores, type(cur_user.set_of_scores) top_twenty = list() for list_elem in cur_user.set_of_scores: profile = json.loads(fetch_fb_functions1.getProfile(accessToken, list_elem.friend_uid)) print 'PROFILE: ', profile print type(profile) print profile.keys() print 'score: ',list_elem.score if 'relationship_status' in profile.keys(): top_twenty.append({'id': list_elem.friend_uid, 'score': list_elem.score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': profile['relationship_status'], 'link': profile['link'], 'picture': profile['picture']['data'] }) else: top_twenty.append({'id': list_elem.friend_uid, 'score': list_elem.score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': 'nill', 'link': profile['link'], 'picture': profile['picture']['data'] }) print '****><><><>',top_twenty, type(top_twenty) session.end() return json.dumps(top_twenty) print '@@@@@@@@@@@@@@@@@ Calculating' fetch_string = '' done_flag = 0 if not set(REQUIRED_PERMISSIONS['PHOTOS']).isdisjoint(granted_permissions['data'][0].keys()): fetch_string = fetch_string + 'photos' done_flag = 1 if not set(REQUIRED_PERMISSIONS['CHECKINS']).isdisjoint(granted_permissions['data'][0].keys()): if done_flag == 1: fetch_string = fetch_string + ', ' fetch_string = fetch_string + 'checkins' done_flag = 1 if not set(REQUIRED_PERMISSIONS['FEEDS AND LINKS']).isdisjoint(granted_permissions['data'][0].keys()): if done_flag == 1: fetch_string = fetch_string + ', ' fetch_string = fetch_string + 'feed, links' done_flag =1 if not set(REQUIRED_PERMISSIONS['FAMILY']).isdisjoint(granted_permissions['data'][0].keys()): if done_flag == 1: fetch_string = fetch_string + ', ' fetch_string = fetch_string + 'family' done_flag =1 if not set(REQUIRED_PERMISSIONS['STATUS']).isdisjoint(granted_permissions['data'][0].keys()): if done_flag == 1: fetch_string = fetch_string + ', ' fetch_string = fetch_string + 'statuses' done_flag =1 if not set(REQUIRED_PERMISSIONS['INBOX']).isdisjoint(granted_permissions['data'][0].keys()): if done_flag == 1: fetch_string = fetch_string + ', ' fetch_string = fetch_string + 'inbox.limit(500)' done_flag =1 fetch_fb_functions1.getData(accessToken, uid, fetch_string) # if atleast one required in granted if not set(REQUIRED_PERMISSIONS['FRIENDS']).isdisjoint(granted_permissions['data'][0].keys()): print '\n\n #### ##### atleast one FRIENDS\n\n' my_friends = fetch_fb_functions1.getFriends(accessToken) if 'friends' not in my_friends.keys(): print 'no friends' return json.dumps('') #print 'my friends' #print my_friends score_calculation.init_scores(my_friends['friends']['data']) else: return json.dumps('') # if atleast one required in granted if not set(REQUIRED_PERMISSIONS['PHOTOS']).isdisjoint(granted_permissions['data'][0].keys()): print '\n\n #### ##### atleast one PHOTOS\n\n' my_photos = fetch_fb_functions1.getPhotos(accessToken) if my_photos != None: print 'my_photos' #print my_photos print my_photos['tagged'] score_calculation.update_scores(my_photos['tagged'], 3) print my_photos['liked by'] score_calculation.update_scores(my_photos['liked by'], 2) print my_photos['commented by'] score_calculation.update_scores(my_photos['commented by'], 2) # if atleast one required in granted if not set(REQUIRED_PERMISSIONS['CHECKINS']).isdisjoint(granted_permissions['data'][0].keys()): print '\n\n #### ##### atleast one CHECKINS\n\n' my_checkins = fetch_fb_functions1.getCheckins(accessToken, uid) print 'my_checkins' if my_checkins != None: print my_checkins['from'] score_calculation.update_scores(my_checkins['from'], 4) print my_checkins['tagged'] score_calculation.update_scores(my_checkins['tagged'], 4) # if atleast one required in granted if not set(REQUIRED_PERMISSIONS['FEEDS AND LINKS']).isdisjoint(granted_permissions['data'][0].keys()): print '\n\n #### ##### atleast one FEEDS AND LINKS\n\n' my_feeds = fetch_fb_functions1.getFeed(accessToken) if my_feeds != None: print 'my_feeds' print my_feeds['tagged'] score_calculation.update_scores(my_feeds['tagged'], 3) print my_feeds['liked by'] score_calculation.update_scores(my_feeds['liked by'], 2) print my_feeds['commented by'] score_calculation.update_scores(my_feeds['commented by'], 2) my_links = fetch_fb_functions1.getLinks(accessToken) if my_links != None: print 'my_links' print my_links['tagged'] score_calculation.update_scores(my_links['tagged'], 3) print my_links['liked by'] score_calculation.update_scores(my_links['liked by'], 2) print my_links['commented by'] score_calculation.update_scores(my_links['commented by'], 2) # if atleast one required in granted if not set(REQUIRED_PERMISSIONS['FAMILY']).isdisjoint(granted_permissions['data'][0].keys()): print '\n\n #### ##### atleast one FAMILY\n\n' my_family = fetch_fb_functions1.getFamily(accessToken) print 'my_family' if my_family != None: print my_family score_calculation.update_scores_family(my_family, 3) # if atleast one required in granted if not set(REQUIRED_PERMISSIONS['STATUS']).isdisjoint(granted_permissions['data'][0].keys()): print '\n\n #### ##### atleast one STATUS\n\n' my_status = fetch_fb_functions1.get_status(accessToken, uid) if my_status != None: print 'my_status' print my_status['tagged'] score_calculation.update_scores(my_status['tagged'], 3) print my_status['liked by'] score_calculation.update_scores(my_status['liked by'], 2) print my_status['commented by'] score_calculation.update_scores(my_status['commented by'], 2) # if atleast one required in granted if not set(REQUIRED_PERMISSIONS['INBOX']).isdisjoint(granted_permissions['data'][0].keys()): print '\n\n #### ##### atleast one INBOX\n\n' my_inbox = fetch_fb_functions1.getInbox(accessToken, uid) if my_inbox != None: print 'my_inbox' print my_inbox score_calculation.update_scores_inbox(my_inbox, 3) sorted_score_list = score_calculation.show_scores() print '++++',sorted_score_list if len(sorted_score_list) >=20: top_twenty_friends = sorted_score_list[len(sorted_score_list)-20:] else: top_twenty_friends = sorted_score_list highest_index = len(top_twenty_friends)-1 print '?????? $$$$$$', top_twenty_friends top_twenty = list() highest = top_twenty_friends[highest_index][1] print 'highest: ', highest my_friends_scores=list() # set of scores to be stored in DB for list_elem in top_twenty_friends: profile = json.loads(fetch_fb_functions1.getProfile(accessToken, list_elem[0])) print 'PROFILE: ', profile print type(profile) print profile.keys() print 'old score: ',list_elem[1] new_score = (list_elem[1]/highest)*100 print'new score: ',new_score my_friends_scores.append(Score(friend_uid=list_elem[0], score=new_score)) if 'relationship_status' in profile.keys(): top_twenty.append({'id': list_elem[0], 'score': new_score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': profile['relationship_status'], 'link': profile['link'], 'picture': profile['picture']['data'] }) else: top_twenty.append({'id': list_elem[0], 'score': new_score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': 'nill', 'link': profile['link'], 'picture': profile['picture']['data'] }) store_user_record(uid, my_friends_scores, update_flag) print '****><><><>',top_twenty, type(top_twenty) session.end() return json.dumps(top_twenty)
def getCloseFriends(): accessToken = request.args.get('a', '', type=unicode) uid = request.args.get('b', '', type=unicode) print 'uid='+uid print 'in get close friends' '''print 'checking db' users = User.query(Obj).filter(Obj.uid==uid) print users''' my_friends = fetch_fb_functions.getFriends(accessToken) #print 'my_friends' #print my_friends['friends']['data'] score_calculation.init_scores(my_friends['friends']['data']) my_photos = fetch_fb_functions.getPhotos(accessToken) print 'my_photos' #print my_photos print my_photos['tagged'] score_calculation.update_scores(my_photos['tagged'], 3) print my_photos['liked by'] score_calculation.update_scores(my_photos['liked by'], 2) print my_photos['commented by'] score_calculation.update_scores(my_photos['commented by'], 2) my_checkins = fetch_fb_functions.getCheckins(accessToken, uid) print 'my_checkins' if my_checkins != None: print my_checkins['from'] score_calculation.update_scores(my_checkins['from'], 4) print my_checkins['tagged'] score_calculation.update_scores(my_checkins['tagged'], 4) my_feeds = fetch_fb_functions.getFeed(accessToken) print 'my_feeds' print my_feeds['tagged'] score_calculation.update_scores(my_feeds['tagged'], 3) print my_feeds['liked by'] score_calculation.update_scores(my_feeds['liked by'], 2) print my_feeds['commented by'] score_calculation.update_scores(my_feeds['commented by'], 2) my_family = fetch_fb_functions.getFamily(accessToken) print 'my_family' if my_family != None: print my_family score_calculation.update_scores_family(my_family, 3) my_status = fetch_fb_functions.get_status(accessToken, uid) print 'my_status' print my_status['tagged'] score_calculation.update_scores(my_status['tagged'], 3) print my_status['liked by'] score_calculation.update_scores(my_status['liked by'], 2) print my_status['commented by'] score_calculation.update_scores(my_status['commented by'], 2) my_links = fetch_fb_functions.getLinks(accessToken) print 'my_links' print my_links['tagged'] score_calculation.update_scores(my_links['tagged'], 3) print my_links['liked by'] score_calculation.update_scores(my_links['liked by'], 2) print my_links['commented by'] score_calculation.update_scores(my_links['commented by'], 2) my_inbox = fetch_fb_functions.getInbox(accessToken, uid) print 'my_inbox' print my_inbox score_calculation.update_scores_inbox(my_inbox, 3) sorted_score_list = score_calculation.show_scores() print '++++',sorted_score_list top_twenty_friends = sorted_score_list[len(sorted_score_list)-20:] '''for index in range(0,len(top_twenty_friends)): print 'score: ',top_twenty_friends[index][1] length = len(str(top_twenty_friends[index][1]))-3 print 'len: ',length if length < 0: div = 1 else: div=10**length print 'div: ',div print 'new: ', top_twenty_friends[index][1] = (top_twenty_friends[index][1])/div print 'new score: ',top_twenty_friends[index][1]''' print '?????? $$$$$$', top_twenty_friends top_twenty = list() highest = top_twenty_friends[19][1] print 'highest: ', highest #my_friends_scores=list() # set of scores to be stored in DB for list_elem in top_twenty_friends: profile = json.loads(fetch_fb_functions.getProfile(accessToken, list_elem[0])) print 'PROFILE: ', profile print type(profile) print profile.keys() print 'old score: ',list_elem[1] new_score = (list_elem[1]/highest)*100 print'new score: ',new_score #my_friends_scores.append(Score(friend_uid=list_elem[0], score=new_score)) if 'relationship_status' in profile.keys(): top_twenty.append({'id': list_elem[0], 'score': new_score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': profile['relationship_status'], 'link': profile['link'], 'picture': profile['picture']['data'] }) else: top_twenty.append({'id': list_elem[0], 'score': new_score, 'name': profile['name'], 'gender': profile['gender'], 'relationship_status': 'nill', 'link': profile['link'], 'picture': profile['picture']['data'] }) #store_user_record(uid, my_friends_scores) print '****><><><>',top_twenty, type(top_twenty) return json.dumps(top_twenty)
def getCloseFriends(): accessToken = request.args.get("a", "", type=unicode) uid = request.args.get("b", "", type=unicode) print "uid=" + uid print "in get close friends" print "checking db" update_flag = 0 users = session.query(User) for cur_user in users: print cur_user.uid, cur_user.modified_time if cur_user.uid == uid: print datetime.datetime.now() time_diff = datetime.datetime.now() - cur_user.modified_time print "time diff: ", time_diff.total_seconds() if time_diff.total_seconds() > 2628000: update_flag = 1 break else: # fetch cur_user.set_of_scores print "################## Fetching from DB" print cur_user.set_of_scores, type(cur_user.set_of_scores) top_twenty = list() start_time = time.time() try: t = [] # list of threads for list_elem in cur_user.set_of_scores: url = ( "https://graph.facebook.com/" + list_elem.friend_uid + "?fields=id, name, gender, relationship_status, picture.height(200).width(200), link, statuses.limit(1).fields(message,updated_time) &access_token=" + accessToken ) print "score: ", list_elem.score temp = ThreadWithReturnValue( target=doWork, args=(list_elem.friend_uid, url.strip(), list_elem.score) ) # pass tuple(uid, url, new_score) in thread temp.daemon = True temp.start() t.append(temp) for thread_index in range(0, len(t)): current_profile = t[thread_index].join() print "\n\nWhile Joining: ", current_profile top_twenty.append(current_profile) except KeyboardInterrupt: sys.exit(1) print time.time() - start_time, "seconds" print "****><><><>", top_twenty, type(top_twenty) session.end() return json.dumps(top_twenty) print "@@@@@@@@@@@@@@@@@ Calculating" my_friends = fetch_fb_functions.getFriends(accessToken) if "friends" not in my_friends.keys(): print "no friends" return json.dumps("") # print 'my friends' # print my_friends score_list = {} sorted_score_list = {} friend_list = my_friends["friends"]["data"] print "init_scores" for current_friend in friend_list: score_list[current_friend["id"]] = 1 # print score_list fetch_string = "photos, checkins, feed, links, family, inbox.limit(500), statuses" all_data = fetch_fb_functions.getData(accessToken, uid, fetch_string) my_photos = fetch_fb_functions.getPhotos(accessToken, all_data) if my_photos != None: print "my_photos" # print my_photos print my_photos["tagged"] score_calculation.update_scores(my_photos["tagged"], 3, score_list) print my_photos["liked by"] score_calculation.update_scores(my_photos["liked by"], 2, score_list) print my_photos["commented by"] score_calculation.update_scores(my_photos["commented by"], 2, score_list) my_checkins = fetch_fb_functions.getCheckins(accessToken, uid, all_data) print "my_checkins" if my_checkins != None: print my_checkins["from"] score_calculation.update_scores(my_checkins["from"], 4, score_list) print my_checkins["tagged"] score_calculation.update_scores(my_checkins["tagged"], 4, score_list) my_feeds = fetch_fb_functions.getFeed(accessToken, all_data) if my_feeds != None: print "my_feeds" print my_feeds["tagged"] score_calculation.update_scores(my_feeds["tagged"], 3, score_list) print my_feeds["liked by"] score_calculation.update_scores(my_feeds["liked by"], 2, score_list) print my_feeds["commented by"] score_calculation.update_scores(my_feeds["commented by"], 2, score_list) my_family = fetch_fb_functions.getFamily(accessToken, all_data) print "my_family" if my_family != None: print my_family score_calculation.update_scores_family(my_family, 3, score_list) my_status = fetch_fb_functions.get_status(accessToken, uid, all_data) if my_status != None: print "my_status" print my_status["tagged"] score_calculation.update_scores(my_status["tagged"], 3, score_list) print my_status["liked by"] score_calculation.update_scores(my_status["liked by"], 2, score_list) print my_status["commented by"] score_calculation.update_scores(my_status["commented by"], 2, score_list) my_links = fetch_fb_functions.getLinks(accessToken, all_data) if my_links != None: print "my_links" print my_links["tagged"] score_calculation.update_scores(my_links["tagged"], 3, score_list) print my_links["liked by"] score_calculation.update_scores(my_links["liked by"], 2, score_list) print my_links["commented by"] score_calculation.update_scores(my_links["commented by"], 2, score_list) my_inbox = fetch_fb_functions.getInbox(accessToken, uid, all_data) if my_inbox != None: print "my_inbox" print my_inbox score_calculation.update_scores_inbox(my_inbox, 3, score_list) sorted_score_list = score_calculation.show_scores(score_list) print "++++", sorted_score_list if len(sorted_score_list) >= 20: top_twenty_friends = sorted_score_list[len(sorted_score_list) - 20 :] else: top_twenty_friends = sorted_score_list highest_index = len(top_twenty_friends) - 1 print "?????? $$$$$$", top_twenty_friends highest = top_twenty_friends[highest_index][1] print "highest: ", highest top_twenty = list() # returned to front end my_friends_scores = list() # set of scores to be stored in DB start_time = time.time() try: t = [] # list of threads for list_elem in top_twenty_friends: url = ( "https://graph.facebook.com/" + list_elem[0] + "?fields=id, name, gender, relationship_status, picture.height(200).width(200), link, statuses.limit(1).fields(message,updated_time) &access_token=" + accessToken ) print "old score: ", list_elem[1] new_score = (list_elem[1] / highest) * 100 print "new score: ", new_score temp = ThreadWithReturnValue( target=doWork, args=(list_elem[0], url.strip(), new_score) ) # pass tuple(uid, url, new_score, top_twenty) in thread temp.daemon = True temp.start() t.append(temp) my_friends_scores.append(Score(friend_uid=list_elem[0], score=new_score)) for thread_index in range(0, len(t)): current_profile = t[thread_index].join() print "\n\nWhile Joining: ", current_profile top_twenty.append(current_profile) except KeyboardInterrupt: sys.exit(1) print time.time() - start_time, "seconds" store_user_record(uid, my_friends_scores, update_flag) session.end() print "****><><><>", top_twenty, type(top_twenty) return json.dumps(top_twenty)
def calculateClosest(uid, update_flag, accessToken): print "Calculating" my_friends = fetch_fb_functions.getFriends(accessToken) if "friends" not in my_friends.keys(): print "no friends" return json.dumps("") # print 'my friends' # print my_friends score_list = {} sorted_score_list = {} friend_list = my_friends["friends"]["data"] print "init_scores" for current_friend in friend_list: score_list[current_friend["id"]] = 1 # print score_list fetch_string = "photos, checkins, feed, links, family, inbox.limit(500), statuses" all_data = fetch_fb_functions.getData(accessToken, uid, fetch_string) my_photos = fetch_fb_functions.getPhotos(accessToken, all_data) if my_photos != None: print "my_photos" # print my_photos print my_photos["tagged"] score_calculation.update_scores(my_photos["tagged"], 3, score_list) print my_photos["liked by"] score_calculation.update_scores(my_photos["liked by"], 2, score_list) print my_photos["commented by"] score_calculation.update_scores(my_photos["commented by"], 2, score_list) my_checkins = fetch_fb_functions.getCheckins(accessToken, uid, all_data) print "my_checkins" if my_checkins != None: print my_checkins["from"] score_calculation.update_scores(my_checkins["from"], 4, score_list) print my_checkins["tagged"] score_calculation.update_scores(my_checkins["tagged"], 4, score_list) my_feeds = fetch_fb_functions.getFeed(accessToken, all_data) if my_feeds != None: print "my_feeds" print my_feeds["tagged"] score_calculation.update_scores(my_feeds["tagged"], 3, score_list) print my_feeds["liked by"] score_calculation.update_scores(my_feeds["liked by"], 2, score_list) print my_feeds["commented by"] score_calculation.update_scores(my_feeds["commented by"], 2, score_list) my_family = fetch_fb_functions.getFamily(accessToken, all_data) print "my_family" if my_family != None: print my_family score_calculation.update_scores_family(my_family, 3, score_list) my_status = fetch_fb_functions.get_status(accessToken, uid, all_data) if my_status != None: print "my_status" print my_status["tagged"] score_calculation.update_scores(my_status["tagged"], 3, score_list) print my_status["liked by"] score_calculation.update_scores(my_status["liked by"], 2, score_list) print my_status["commented by"] score_calculation.update_scores(my_status["commented by"], 2, score_list) my_links = fetch_fb_functions.getLinks(accessToken, all_data) if my_links != None: print "my_links" print my_links["tagged"] score_calculation.update_scores(my_links["tagged"], 3, score_list) print my_links["liked by"] score_calculation.update_scores(my_links["liked by"], 2, score_list) print my_links["commented by"] score_calculation.update_scores(my_links["commented by"], 2, score_list) my_inbox = fetch_fb_functions.getInbox(accessToken, uid, all_data) if my_inbox != None: print "my_inbox" print my_inbox score_calculation.update_scores_inbox(my_inbox, 3, score_list) sorted_score_list = score_calculation.show_scores(score_list) print "++++", sorted_score_list if len(sorted_score_list) >= 20: top_twenty_friends = sorted_score_list[len(sorted_score_list) - 20 :] else: top_twenty_friends = sorted_score_list top_twenty = fetchClosestProfiles(top_twenty_friends, update_flag, uid, accessToken) return top_twenty
def getCloseFriends(): accessToken = request.args.get('a', '', type=unicode) uid = request.args.get('b', '', type=unicode) print 'uid='+uid print 'in get close friends' print 'checking db' update_flag = 0 users = session.query(User) for cur_user in users: print cur_user.uid, cur_user.modified_time if cur_user.uid == uid: print datetime.datetime.now() time_diff = datetime.datetime.now()-cur_user.modified_time print 'time diff: ',time_diff.total_seconds() if time_diff.total_seconds() > RECALCULATE_AFTER: update_flag = 1 break else: # fetch cur_user.set_of_scores print '################## Fetching from DB' print cur_user.set_of_scores, type(cur_user.set_of_scores) top_twenty = list() start_time = time.time() try: t=[] # list of threads for list_elem in cur_user.set_of_scores: url = "https://graph.facebook.com/" + list_elem.friend_uid + "?fields=id, name, gender, relationship_status, picture.height(200).width(200), link, statuses.limit(1).fields(message,updated_time) &access_token="+accessToken print 'score: ',list_elem.score temp = ThreadWithReturnValue(target=doWork, args=(list_elem.friend_uid, url.strip(), list_elem.score)) # pass tuple(uid, url, new_score) in thread temp.daemon=True temp.start() t.append(temp) for thread_index in range(0,len(t)): current_profile = t[thread_index].join() print '\n\nWhile Joining: ', current_profile top_twenty.append(current_profile) except KeyboardInterrupt: sys.exit(1) print time.time() - start_time, "seconds" print '****><><><>',top_twenty, type(top_twenty) session.end() return json.dumps(top_twenty) print '@@@@@@@@@@@@@@@@@ Calculating' my_friends = fetch_fb_functions.getFriends(accessToken) if 'friends' not in my_friends.keys(): print 'no friends' return json.dumps('') #print 'my friends' #print my_friends score_list = {} sorted_score_list = {} friend_list = my_friends['friends']['data'] print 'init_scores' for current_friend in friend_list: score_list[current_friend['id']] = 1 #print score_list fetch_string = 'photos, checkins, feed, links, family, inbox.limit(500), statuses' all_data = fetch_fb_functions.getData(accessToken, uid, fetch_string) my_photos = fetch_fb_functions.getPhotos(accessToken, all_data) if my_photos != None: print 'my_photos' #print my_photos print my_photos['tagged'] score_calculation.update_scores(my_photos['tagged'], 3, score_list) print my_photos['liked by'] score_calculation.update_scores(my_photos['liked by'], 2, score_list) print my_photos['commented by'] score_calculation.update_scores(my_photos['commented by'], 2, score_list) my_checkins = fetch_fb_functions.getCheckins(accessToken, uid, all_data) print 'my_checkins' if my_checkins != None: print my_checkins['from'] score_calculation.update_scores(my_checkins['from'], 4, score_list) print my_checkins['tagged'] score_calculation.update_scores(my_checkins['tagged'], 4, score_list) my_feeds = fetch_fb_functions.getFeed(accessToken, all_data) if my_feeds != None: print 'my_feeds' print my_feeds['tagged'] score_calculation.update_scores(my_feeds['tagged'], 3, score_list) print my_feeds['liked by'] score_calculation.update_scores(my_feeds['liked by'], 2, score_list) print my_feeds['commented by'] score_calculation.update_scores(my_feeds['commented by'], 2, score_list) my_family = fetch_fb_functions.getFamily(accessToken, all_data) print 'my_family' if my_family != None: print my_family score_calculation.update_scores_family(my_family, 3, score_list) my_status = fetch_fb_functions.get_status(accessToken, uid, all_data) if my_status != None: print 'my_status' print my_status['tagged'] score_calculation.update_scores(my_status['tagged'], 3, score_list) print my_status['liked by'] score_calculation.update_scores(my_status['liked by'], 2, score_list) print my_status['commented by'] score_calculation.update_scores(my_status['commented by'], 2, score_list) my_links = fetch_fb_functions.getLinks(accessToken, all_data) if my_links != None: print 'my_links' print my_links['tagged'] score_calculation.update_scores(my_links['tagged'], 3, score_list) print my_links['liked by'] score_calculation.update_scores(my_links['liked by'], 2, score_list) print my_links['commented by'] score_calculation.update_scores(my_links['commented by'], 2, score_list) my_inbox = fetch_fb_functions.getInbox(accessToken, uid, all_data) if my_inbox != None: print 'my_inbox' print my_inbox score_calculation.update_scores_inbox(my_inbox, 3, score_list) sorted_score_list = score_calculation.show_scores(score_list) print '++++',sorted_score_list if len(sorted_score_list) >=20: top_twenty_friends = sorted_score_list[len(sorted_score_list)-20:] else: top_twenty_friends = sorted_score_list highest_index = len(top_twenty_friends)-1 print '?????? $$$$$$', top_twenty_friends highest = top_twenty_friends[highest_index][1] print 'highest: ', highest top_twenty = list() # returned to front end my_friends_scores=list() # set of scores to be stored in DB start_time = time.time() try: t=[] # list of threads for list_elem in top_twenty_friends: url = "https://graph.facebook.com/" + list_elem[0] + "?fields=id, name, gender, relationship_status, picture.height(200).width(200), link, statuses.limit(1).fields(message,updated_time) &access_token="+accessToken print 'old score: ',list_elem[1] new_score = (list_elem[1]/highest)*100 print'new score: ',new_score temp = ThreadWithReturnValue(target=doWork, args=(list_elem[0], url.strip(), new_score)) # pass tuple(uid, url, new_score, top_twenty) in thread temp.daemon=True temp.start() t.append(temp) my_friends_scores.append(Score(friend_uid=list_elem[0], score=new_score)) for thread_index in range(0,len(t)): current_profile = t[thread_index].join() print '\n\nWhile Joining: ', current_profile top_twenty.append(current_profile) except KeyboardInterrupt: sys.exit(1) print time.time() - start_time, "seconds" store_user_record(uid, my_friends_scores, update_flag) session.end() print '****><><><>',top_twenty, type(top_twenty) return json.dumps(top_twenty)