What do my Facebook friends have in common in terms of the things they have Liked, or in terms of their music or movie preferences? (And does this say anything about me?!) Here’s a recipe for visualising that data…

After discovering via Martin Hawksey that the recent (December, 2011) 2.5 release of Google Refine allows you to import JSON and XML feeds to bootstrap a new project, I wondered whether it would be able to pull in data from the Facebook API if I was logged in to Facebook (Google Refine does run in the browser after all…)

Looking through the Facebook API documentation whilst logged in to Facebook, it’s easy enough to find exemplar links to things like your friends list (https://graph.facebook.com/me/friends?access_token=A_LONG_JUMBLE_OF_LETTERS) or the list of likes someone has made (https://graph.facebook.com/me/likes?access_token=A_LONG_JUMBLE_OF_LETTERS); replacing me with the Facebook ID of one of your friends should pull down a list of their friends, or likes, etc.

(Note that validity of the access token is time limited, so you can’t grab a copy of the access token and hope to use the same one day after day.)

Grabbing the link to your friends on Facebook is simply a case of opening a new project, choosing to get the data from a Web Address, and then pasting in the friends list URL:

Click on next, and Google Refine will download the data, which you can then parse as a JSON file, and from which you can identify individual record types:

If you click the highlighted selection, you should see the data that will be used to create your project:

You can now click on Create Project to start working on the data – the first thing I do is tidy up the column names:

We can now work some magic – such as pulling in the Likes our friends have made. To do this, we need to create the URL for each friend’s Likes using their Facebook ID, and then pull the data down. We can use Google Refine to harvest this data for us by creating a new column containing the data pulled in from a URL built around the value of each cell in another column:

The Likes URL has the form https://graph.facebook.com/me/likes?access_token=A_LONG_JUMBLE_OF_LETTERS which we’ll tinker with as follows:

The throttle control tells Refine how often to make each call. I set this to 500ms (that is, half a second), so it takes a few minutes to pull in my couple of hundred or so friends (I don’t use Facebook a lot;-). I’m not sure what limit the Facebook API is happy with (if you hit it too fast (i.e. set the throttle time too low), you may find the Facebook API stops returning data to you for a cooling down period…)?

Having imported the data, you should find a new column:

At this point, it is possible to generate a new column from each of the records/Likes in the imported data… in theory (or maybe not..). I found this caused Refine to hang though, so instead I exprted the data using the default Templating… export format, which produces some sort of JSON output…

I then used this Python script to generate a two column data file where each row contained a (new) unique identifier for each friend and the name of one of their likes:

import simplejson,csv
writer=csv.writer(open('fbliketest.csv','wb+'),quoting=csv.QUOTE_ALL)
fn='my-fb-friends-likes.txt'
data = simplejson.load(open(fn,'r'))
id=0
for d in data['rows']:
id=id+1
#'interests' is the column name containing the Likes data
interests=simplejson.loads(d['interests'])
for i in interests['data']:
print str(id),i['name'],i['category']
writer.writerow([str(id),i['name'].encode('ascii','ignore')])

I could then import this data into Gephi and use it to generate a network diagram of what they commonly liked:

Rather than returning Likes, I could equally have pulled back lists of the movies, music or books they like, their own friends lists (permissions settings allowing), etc etc, and then generated friends’ interest maps on that basis.

PS dropping out of Google Refine and into a Python script is a bit clunky, I have to admit. What would be nice would be to be able to do something like a “create new rows with new column from column” pattern that would let you set up an iterator through the contents of each of the cells in the column you want to generate the new column from, and for each pass of the iterator: 1) duplicate the original data row to create a new row; 2) add a new column; 3) populate the cell with the contents of the current iteration state. Or something like that…

PPS Related to the PS request, there is a sort of related feature in the 2.5 release of Google Refine that lets you merge data from across rows with a common key into a newly shaped data set: Key/value Columnize. Seeing this, it got me wondering what a fusion of Google Refine and RStudio might be like (or even just R support within Google Refine?)

There’s no reason Google Refine shouldn’t be able to parse the JSON that was fetched if it is well-formed — and it should never just “hang.”* Please fill a bug report with the data that you were trying to parse.

* “hangs” are usually server side errors that have been handled incorrectly causing the client to spin forever waiting for the results.

@tom Okay, I should have been clearer – I suspect that Google Chrome gave up the ghost as Refine was was trying to spawn hundreds of new columns. I find it hangs a lot; it somehow doesn’t like the fact that I have multiple windows and tabs open for days on end (or would if I didn’t have to keep restarting Chrome because of the way it keeps on stalling/hanging…)

It seems like it wouldn’t take much to write an app to grab all this as json to feed into Refine (or whatever). You can grab batches of “friends likes” at a go rather than going one at a time. graph.facebook.com/likes?ids=[comma_separated_list_of_uids]?access_token=ABC123

Hi,
I tried using the python script for converting the JSON file to csv format file, but somehow its not working. I don’t know why it prints the data on the terminal and creates a .csv extension file but does not write anything to it. Can someone guide please?

Hi, Tony,
thank you for letting me know Google Refine. If possible, could you help me with some issue? I want to add a column which makes such change (ABCZ1239 –> BCDA2340) based on the value of original column. It should be a piece of cake in Google Refine but I am far from understanding a GREL or similar expresson language. If you can make it easily please help me.