Brad Fitzpatrick (brad) wrote,
Brad Fitzpatrick
brad

AddressBooker & exporting my Facebook Phonebook

This 4-day weekend was awesome for catching up on personal hacking projects. In addition to adding IPv6 support to Perlbal and hacking on my interactive shadow/art wall more, I also worked on a little address book management tool, AddressBooker [source here].

Basically AddressBooker takes a POST of contacts data in JSON form, and does stuff with it, where "stuff" is currently limited to merging it into your Google Contacts. (GMail, Android, etc) This was my experiment in learning GData, AuthSub, and App Engine a bit more.

Anyway, I then wanted to get my Facebook Phonebook exported to my Google Contacts, so it'd sync to my Android phone. I didn't see an export option in Facebook (maybe I missed it?), so I wrote a little GreaseMonkey script instead to automate the whole process:

http://bradfitz.com/greasemonkey/facebook_phonebook_export.user.js

If you have Firefox and GreaseMonkey, then click the above link and it'll ask if you want to install it. Install it, then go to your Facebook Phonebook (sorry, no permalink to it), then go into Tools > GreaseMonkey > User Script Commands... > and you'll see Export Facebook Phonebook. That'll then page through your phone book (you should probably start on page 1: it's kinda flaky) and extract the data, and then POST it to AddressBooker for you, which will then guide you through merging it into your Google Contacts.

Enjoy!

(And keep in mind I barely know browser stuff or Greasemonkey or Python or App Engine or GData, so patches welcome!... brad@danga.com, or Github)

Update 2009-05-09: Updated the JavaScript to work with Facebook's new layout.

Update 2010-06-06: Facebook's markup changed again, but I'm too lazy to go update the script. Patches welcome.
Tags: android, hacks, perlbal, tech
Subscribe
Hey Brad,

Thanks for creating this script - I used it a couple of years ago with no problems! One of my friends recently lost her contacts on her phone, so decided to use your script again to help her get her contacts back but found that it was not longer working. I've done some debugging and there's a couple of changes to your script that I've made to get it working again (presuming it stopped working because of changes Facebook has made since your last update).

First is on the child loop:

var ffriends = get("FriendsPage_Container");
var children = ffriends.childNodes;
// for (var i = 0; i < children.length; ++i) --Original line of code
for (var i = 1; i < children.length; ++i) { // Changed initial value of i = 1
var child = children[i];


Second one is on the name var:

// Extract the name.
// var name = xpath(child, "descendant::node()/a[@class='UIObjectListing_Title']/text()[1]"); -- original line of code
var name = xpath(child, "descendant::node()/a[@class='UIObjectListing_Title']/text()[1]"); // added the [1] due to their now being 2 lines in the div

Also, it should be noted that with if users have already enabled the Sync in the iPhone Facebook app, Facebook will include your non-Facebook contacts in their phonebook, which the script doesn't pick up in its name-grab.
You could probably fix this by changing the name var xpath expression to "descendant::node()/*[@class='UIObjectListing_Title']/text()[1]", but I think it's better if the user disables the sync and deletes those contacts from the Facebook Phonebook before running your script.

Cheers!

Comments for this post were locked by the author