Skip to content

Instantly share code, notes, and snippets.

@lognaturel
Created August 5, 2011 20:09
Show Gist options
  • Save lognaturel/1128397 to your computer and use it in GitHub Desktop.
Save lognaturel/1128397 to your computer and use it in GitHub Desktop.
Facebook friends birth month counts (Python)
# Uses Facebook Graph API to calculate birth month counts for your network.
# Get an access token from http://developers.facebook.com/tools/explorer/ (you'll need to check friend_birthday in 'Get Access Token' permissions)
import json
import urllib2
def load(id, thing):
address = "https://graph.facebook.com/" + str(id) + "/" + str(thing) + "?access_token=" + ACCESS_TOKEN
return json.load(urllib2.urlopen(address))
USER = "" # Your user name or ID (what comes after the slash when you visit your own profile)
ACCESS_TOKEN = "" # Get from http://developers.facebook.com/tools/explorer/
months = [0]*13
total_friends = 0
bday_friends = 0
my_friends = load(USER, "friends")['data']
for friend in my_friends:
total_friends += 1
fr = load(friend['id'], "")
#print(fr['name'])
if('birthday' in fr):
bday_friends += 1
m = int(fr['birthday'][0:2])
#print("\t" + str(m))
months[m] += 1
print months
print str(bday_friends) + "/" + str(total_friends)
print
percents = [(m * 1.0) / bday_friends * 100 for m in months]
print(percents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment