FaceBook Mutual Friend Scraper

I’ve been playing with RestFB lately. Here’s a simple program adapted from their demo code that extracts my friends ids and displays our mutual friends. Note that you will need to generate your own access token, either via your app or from the this link : https://developers.facebook.com/tools/explorer .

Clique analysis anyone?

package com.vgb.social.facebook;

import java.util.List;

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.Page;
import com.restfb.types.Post;
import com.restfb.types.User;

public class FaceBookMutualFriendMiner {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String MY_ACCESS_TOKEN = "################# GET YOUR OWN ACCESS TOKEN #####################";

		FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN);

		User user = facebookClient.fetchObject("me", User.class);

		System.out.println("User name: " + user.getName());
		Connection myFriends = facebookClient.fetchConnection("me/friends", User.class);
		Connection myFeed = facebookClient.fetchConnection("me/feed", Post.class);

		System.out.println("Count of my friends: " + myFriends.getData().size());

		processFriends(facebookClient, myFriends.getData(), true, "My Friend");

	}

	private static void processFriends(FacebookClient facebookClient, List myFriend, boolean recurse, String pre) {
		for (User u : myFriend) {
			System.out.println(pre + " : " + u.getName() + " " + u.getId());

			if (recurse) {
				Connection myMutualFriends = facebookClient.fetchConnection("me/mutualfriends/" + u.getId(), User.class);
				processFriends(facebookClient, myMutualFriends.getData(), false, "My Mutual Friends with " + u.getName());
			}
		}
	}

}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s