Using Facebook API from Java to find friends who Comments on user’s photos the most

Logic behind most of the facebook apps revolve around the data mining of user’s posts. Best way is to find user’s friends who have commented on his posts the most. This way apps become for interactive and the user will have a tendency to share such app’s result. For example ‘Which friend will get married next’ – the Most interactive result would be to show user’s most interactive friend.

So how do we calculate this data? This article assumes that you have already got the access token from the user.

If you do not know, article :folarticle:Facebook login and getting user’s access token

We will fetch all the photos of the user and check which friend has commented on most number of pics.

We will being restfb java client to invoke Facebook’s API.

1. First create Facebook client using the access token

String access_token = at; // You would be already having this token
		FacebookClient client = new DefaultFacebookClient(at);
		String userid = getUserId(req, client);

2. Next we will hit Facebook API’s ‘albums’ endpoint to fetch all user’s albums

		Map<String, Integer> data =  new HashMap<String, Integer>();
 
		List albumdata = client.fetchConnection("me" + "/albums",
				JsonObject.class, Parameter.with("fields", "id,name"))
				.getData();

3. Now we will iterate over albums and fetch all photos of each album. For each photo we will be fetching its comments and we have put a limit of maximum 1000 comments

		for (int x = 0; x < albumdata.size(); x++) {
			try {
				JsonObject ajobj = albumdata.get(x);
				String albumname = ajobj.getString("name").toLowerCase();
				System.out.println(albumname);
				List idata = client.fetchConnection(
						ajobj.getString("id") + "/photos", JsonObject.class,
						Parameter.with("fields", "id,comments.limit(1000)"),
						Parameter.with("offset", 0),
						Parameter.with("limit", 1000)).getData();
.........

4. Iterate over all photos of each album and fetch their likes

				for (int xx = 0; xx < idata.size(); xx++) {
					try {
						JsonObject jobj = idata.get(xx);
						JsonObject likes = jobj.getJsonObject("comments");

5. Iterate over all the likes for each photo and and fetch the userid of the user who had liked. We also have a check that you dont count user’s own like. Maintain a map of user vs ‘his likes count’ and keep on updating it

JsonArray ary = likes.getJsonArray("data");
						for (int xxx = 0; xxx < ary.length(); xxx++) {
							JsonObject jo = ary.getJsonObject(xxx);
							String id = null;
							try {
								id = (String) jo.getString("id");
								if ((id + "").equals(userid + "")) {
									continue;
								}
 
							} catch (Throwable e) {
								continue;
							}
							updateMap(data, id);
						}
 
public static void updateMap(Map<String, Integer> country, String m) {
		// TODO Auto-generated method stub
		m = m.toLowerCase();
		if (country.containsKey(m)) {
			Integer v = country.get(m);
			country.put(m, ++v);
		} else {
			country.put(m, 1);
 
		}
	}

6. Once map has been constructed, sort by the number of likes

		data = sortByValue(data, DATASIZE);
 
                public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(
			Map<K, V> map, int v) {
		List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
				map.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
			public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
				return (o1.getValue()).compareTo(o2.getValue());
			}
		});
		Collections.reverse(list);
		Map<K, V> result = new LinkedHashMap<K, V>();
		int i = 0;
		for (Map.Entry<K, V> entry : list) {
			i++;
			if (i > v) {
				break;
			}
			result.put(entry.getKey(), entry.getValue());
		}
		return result;
	}

7. Now you have the map of users and their comments count. Iterate over it and fetch user info for each userid.

This topic has already been covered here :

Fetch user info using Facebook’s graph API

So finally you have list of all the users who have commented on given’s user’s photos most of the times

Mar Java Mit Java ­čÖé

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *