Thursday, January 20, 2011

Catch who removed you from Facebook...!

Facebook doesn't send notifications upon unfriending.. although it does when somebody added you.

This blog post explains how to catch people who remove you from Facebook friend list.

Before running the following java program, you need to do the following steps..

0. Go to http://www.facebook.com/#!/pages/Facebook-Developer/7213347185 and LIKE that page.

1. Go to http://www.facebook.com/developers and click on "Set Up New App"

2. Once done with [1] - go to the link http://developers.facebook.com/docs/reference/rest/auth.createtoken and select your application from the "Test Console" and click on the link which looks like https://api.facebook.com/method/auth.createToken?
access_token=...

3. Now a new browser window will be opened up and from the Url - copy the access_token - which is in the query string. Make sure you properly copy the access_token value only - don't mess up with the other query string parameters present in the same Url.

4. Replace the value of the constant ACCESS_TOKEN in code below with the one you got from [3].

5. Download the RestFB jar from here and add it to the classpath of the java program.

6. That's it and whenever you run the following program you will see the friends you made and lost from your previous run.
package samples.fb.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

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

public class FacebookTestClient {

 private final static String ACCESS_TOKEN = "126895777dsd7843z4erer7C2.WfdfgQmjj376b_gfdfdf__.3600.1295557200-787236966%7CQ0166mXlhvmBoCXohUDVpfWrYAg";

 public static void main(String[] args) {

  FacebookClient facebookClient = new DefaultFacebookClient(ACCESS_TOKEN);
  Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);

  List<User> list = myFriends.getData();
  Hashtable<String, String> newFriends = new Hashtable>String, String>();
  Hashtable<String, String> oldFriends = loadOldFriends();

  for (Iterator<User> iterator = list.iterator(); iterator.hasNext();) {
   User friend = iterator.next();
   newFriends.put(friend.getId(), friend.getName());

   if (oldFriends != null) {
    if (!oldFriends.containsKey(friend.getId())) {
     System.out.println("You have a new friend today, " + friend.getName());
    }
   }
  }

  if (oldFriends != null) {
   for (Iterator<Entry<String, String>> iterator = oldFriends.entrySet().iterator(); iterator
     .hasNext();) {
    Entry<String, String> friendEntry = iterator.next();
    if (!newFriends.containsKey(friendEntry.getKey())) {
     System.out.println("You lost a friend today, " + friendEntry.getValue());
    }

   }
  }

  saveFriends(newFriends);

 }

 private static void saveFriends(Hashtable<String, String> friends) {

  try {
   FileOutputStream fileOut = new FileOutputStream("friendlist.ser");
   ObjectOutputStream out = new ObjectOutputStream(fileOut);
   out.writeObject(friends);
   out.close();
   fileOut.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private static Hashtable<String, String> loadOldFriends() {

  Hashtable<String, String> friends = null;

  try {
   if (new File("friendlist.ser").exists()) {
    FileInputStream fileIn = new FileInputStream("friendlist.ser");
    ObjectInputStream in = new ObjectInputStream(fileIn);
    friends = (Hashtable<String, String>) in.readObject();
    in.close();
    fileIn.close();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return friends;
 }
}

5 comments:

Prabath Abeysekara said...

WOW! Awesome.!!

Jayanga Kaushalya said...

That's a thing I wanted...!

sLoP said...

For anyone who doesn't want to code and likes a nice integration directly into Facebook, try this userscript: http://userscripts.org/scripts/show/58852 (using it myself, works perfectly!)

lahiru said...

Totally waste of your time... to find who removed you from FB.

Prabath said...

FB has moved to OAuth and the sample in this blog post won't work any more..