Last active
December 17, 2015 03:09
-
-
Save wwqrd/5541593 to your computer and use it in GitHub Desktop.
This file contains 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
from facepy import GraphAPI | |
import json | |
# connect to facebook | |
oauth_access_token = [TOKEN_HERE] | |
graph = GraphAPI(oauth_access_token) | |
# get friend list | |
friends = [] | |
for page in graph.get('me/friends', True): | |
friends = friends + page['data'] | |
# get mutual friends (1.5 network) | |
all_mutual_friends = {} | |
for friend in friends: | |
friend_id = friend['id'] | |
mutual_friends_path = 'me/mutualfriends/' + friend_id | |
mutual_friends = [] | |
for page in graph.get(mutual_friends_path, True): | |
mutual_friends = mutual_friends + page['data'] | |
all_mutual_friends[friend_id] = mutual_friends | |
# echo in json! | |
print json.dumps(all_mutual_friends) |
This file contains 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
import json | |
from facepy import GraphAPI | |
def slicer(seq, size): | |
return (seq[pos:pos + size] for pos in xrange(0, len(seq), size)) | |
oauth_access_token = [TOKEN_HERE] | |
# connect to facebook | |
graph = GraphAPI(oauth_access_token) | |
# get 'me' | |
me = graph.get('me') | |
uid = me["id"] | |
# get friend list | |
friends = [] | |
for page in graph.get('me/friends', True): | |
friends = friends + page['data'] | |
# get mutual friends (1.5 network) | |
all_mutual_friends = {} | |
friend_batch_size = 125 | |
fof_batch_size = 500 # friend of friend batch size | |
for friend_batch in slicer(friends, friend_batch_size): | |
for fof_batch in slicer(friends, fof_batch_size): | |
friend_start_id = friend_batch[0]['id'] | |
friend_end_id = friend_batch[-1]['id'] | |
fof_start_id = fof_batch[0]['id'] | |
query = ( | |
"select uid1, uid2 " | |
"from friend " | |
"where uid1 in " | |
"( " | |
"select uid2 " | |
"from friend " | |
"where uid1=%(uid)s" | |
"and uid2>=%(friend_start_id)s " | |
"and uid2<=%(friend_end_id)s " | |
") " | |
"and uid2 in " | |
"( " | |
"select uid1 " | |
"from friend " | |
"where uid2=%(uid)s " | |
"and uid1>=%(fof_start_id)s " | |
"and uid1<=%(friend_end_id)s " | |
")" | |
) % { | |
"uid": uid, | |
"friend_start_id": friend_start_id, | |
"friend_end_id": friend_end_id, | |
"fof_start_id": fof_start_id | |
} | |
results = graph.fql(query) | |
print json.dumps(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment