def fetchClosestFromDB(cur_user, accessToken):
    # 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 top_twenty
def fetchClosestProfiles(top_twenty_friends, update_flag, uid, accessToken):
    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 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_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 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 '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()
              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 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
                     q.put((list_elem.friend_uid, url.strip(), list_elem.score))  # insert tuple(uid, url, score) in queue          
                 q.join()
              except KeyboardInterrupt:
                 sys.exit(1)
              print time.time() - start_time, "seconds"

              global top_twenty

              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'])

    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"

    store_user_record(uid, my_friends_scores, update_flag)
    session.end()
    global top_twenty
    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'

    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"
    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 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)