Created
August 14, 2010 08:45
-
-
Save abraham/524144 to your computer and use it in GitHub Desktop.
Finds who among your Facebook friends you have the most common interests and likes with
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python -tt | |
# MIT licensed by Abraham Williams - http://abrah.am | |
# Pulls your Facebook friends likes and interest and finds who you have the most in common with. | |
import urllib, json, sys | |
graph_url = 'https://graph.facebook.com/' | |
def print_intersect_count(access_token): | |
tally = {} | |
my_likes_and_interests = [] | |
me = get_and_decode_graph_url(graph_url + '/me?fields=likes,interests,friends&access_token=' + access_token) | |
if 'error' in me: | |
print me['error']['message'] | |
sys.exit(1) | |
for item in me['interests']['data'] and me['likes']['data']: | |
my_likes_and_interests.append(item['id']) | |
friends = [] | |
for friend in me['friends']['data']: | |
friends.append(friend['id']) | |
for x in range(0, len(friends) / 100 + 1): | |
print '...' | |
if len(friends) / 100 == x: final = -1 | |
else: final = x * 100 + 100 | |
friends_ids = ','.join(friends[x * 100: final]) | |
friends_likes = get_and_decode_graph_url(graph_url + '/likes?ids=' + friends_ids + '&access_token=' + access_token) | |
friends_interests = get_and_decode_graph_url(graph_url + '/interests?ids=' + friends_ids + '&access_token=' + access_token) | |
for fuid in friends_likes and friends_interests: | |
for item in friends_likes[fuid]['data']: | |
if item['id'] in my_likes_and_interests: | |
if fuid in tally: | |
tally[fuid] += 1 | |
else: | |
tally[fuid] = 1 | |
winner = sorted(tally, key=tally.get)[-1] | |
for friend in me['friends']['data']: | |
if friend['id'] == winner: | |
name = friend['name'] | |
print 'You have %s interests and likes in common with %s.' % (tally[winner], name) | |
def get_and_decode_graph_url(url): | |
response = urllib.urlopen(url) | |
if response: | |
return json.load(response) | |
return False | |
def main(): | |
if len(sys.argv) != 3: | |
print 'Usage: python interests.py --access_token "access_token"' | |
print 'You can find an access_token on the /me/friends URL from http://developers.facebook.com/docs/api.' | |
sys.exit(1) | |
if sys.argv[1] == '--access_token': | |
print 'Calculating...' | |
print_intersect_count(sys.argv[2]) | |
else: | |
print 'unknown option: ' + sys.argv[1] | |
sys.exit(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment