Skip to content

Instantly share code, notes, and snippets.

@jbeluch
Created June 29, 2011 03:03
Show Gist options
  • Save jbeluch/1052885 to your computer and use it in GitHub Desktop.
Save jbeluch/1052885 to your computer and use it in GitHub Desktop.
Get facebook ids for a list of email addresses.
#!/usr/bin/env python
'''A simple script for finding facebook profile ids for a given list of email
addresses.
Requires eventlet to be installed.
$ pip install eventlet
Also, currently requires a list of contacts exported from Google contacts.
Choose the outlook format and name the file 'contacts.csv' in this dir.'''
from urllib import urlencode, unquote_plus
from urlparse import urlparse, parse_qs
from eventlet.green import urllib2
from os.path import isfile
import csv
import eventlet
import json
# APP_ID and APP_URL are needed if you are requesting a new auth token.
APP_URL = ''
APP_ID = ''
# Get token from .token on disk.
TOKEN = ''
if not TOKEN and isfile('.token'):
with open('.token') as f:
TOKEN = f.read()
def get_new_oauth_token():
'''Will generate a new oauth token and save to ".token".'''
params = {
'type': 'user_agent',
'client_id': APP_ID,
'redirect_uri': APP_URL,
'scope': 'user_photos,email,user_birthday,user_online_presence',
}
base_url = 'https://graph.facebook.com/oauth/authorize'
url = '?'.join([base_url, urlencode(params)])
# Open a browser so the user can authorize the application.
print 'Please visit %s' % url
print 'After authorizing the app, you will be redirected to a new url.'
print 'Please paste that new url below.'
url = raw_input('> ')
_, fragment = url.split('#')
args = parse_qs(fragment)
access_token = args['access_token'][0]
with open('.token', 'w') as f:
f.write(access_token)
raw_input('Token has been saved to disk. Continue? >')
return access_token
def emails_from_google_csv(fn):
'''Export your google contacts as an outlook CSV file. This function will
return a list of email addresses. Currently only parses the first email
field.'''
with open(fn, 'rb') as f:
contacts = csv.DictReader(f)
emails = [c['E-mail Address'] for c in contacts if c['E-mail Address']]
return emails
def fetch(url):
'''Downloads a response for a given url.'''
print "Opening", url
u = urllib2.urlopen(url)
body = u.read()
u.close()
print "Closing", url
return body
def get_facebook_uuids(token, emails, poolsize=5):
'''Returns a list of uuids (or None) for a list of emails. Also requires an
oauth token.'''
urls = ['https://graph.facebook.com/search?q=%s&type=user&access_token=%s'
% (email, TOKEN) for email in emails]
pool = eventlet.GreenPool(poolsize)
responses = pool.imap(fetch, urls)
uuids = []
for resp in responses:
obj = json.loads(resp)
try:
uuids.append(obj['data'][0]['id'])
except IndexError:
# No facebook profile found
uuids.append(None)
return uuids
if __name__ == '__main__':
# If you need a new oauth token, uncomment this line.
#TOKEN = get_new_oauth_token()
emails = emails_from_google_csv('contacts.csv')
uuids = get_facebook_uuids(TOKEN, emails)
print zip(emails, uuids)
print 'Done.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment