Gathering Facebook Identities from Email

Looking at the announcement of the Facebook Graph API from Facebook F8 it seems like it will be a little easier to work with the Facebook system.

In Raindrop we already have some integration with Facebook in order to identify emails coming from the Facebook system and help you filter them out. But there is a lot more that can be done to help keep your email, Facebook, and other contacts in a cohesive form.

So here’s a quick code example written in Python to grab Facebook identities from emails sent by Facebook. This could be used to gather Facebook identities and then possibly merge those with twitter and email contacts.

First we need to import a couple things.

import email, json, urllib2

Then we’ll need to grab an email message.  I used Thunderbird to save a Facebook notification email message as an EML file locally, I called that file ‘facebook.eml’ as you can see below.

msg = email.message_from_file(open('facebook.eml'))

Now we have a parsed email message msg object and we want to look for the X-Facebook-Notify header in the email so we can extract what happened.

fb_notifiy = [tuple(t.strip().split("=")) for t in \
              msg.get('X-Facebook-Notify').split(";")]

The object fb_notify contains tuples of information about the type of notification.  Here is an example of an object you might see.

[('event_wall',),
 ('eid', '14102494623'),
 ('from', '21602578'),
 ('mailid', '12bf28cG149a112G63016bG21') ]

Using fb_notify we’ll do a really simple grab of the from attribute because that is what is going to be publicly available from the Facebook Graph.

from_identity = json.load(urllib2.urlopen("http://graph.facebook.com/%s" % fb_notify[2][1]))

Here’s an example from_identity object:

{u'first_name': u'Bryan',
 u'last_name': u'Clark',
 u'id': u'21602578',
 u'name': u'Bryan Clark' }

The from_identity can be used to more clearly identify who Facebook is sending this notification on behalf of and we could try merging this Facebook identity with other identities we already have in our contacts.

I saved all this code into this gist if you want to take a look at it in code only form with syntax highlighting.

aboot

This is the blog personality of Bryan Clark. I'm a designer in a world of open source. This blog reflects mostly writing about Design, Open Source, Economics, Beer, Wine, and Dogs. There's more information about me on this site or you can contact me directly at clarkbw@gmail.com.

scategories