Facebook API to find most liked photos of the user in Java

I had recently launched a Facebook video app which creates a video of your 7 best Facebook photos. By best I mean the most liked Facebook pics. If interested, you can use the app here : Your 7 best Facebook photos

Well the logic of finding the most popular Facebook pics was simple. I am using restFb java client for interacting with Facebook APIs. This method first fetches all the albums of the user and then iterates over albums and fetches photos for each album. For each photo we maintain the map of Photo Id and likes it got.

This code assumes you would have already got the access token and hence Facebook client.

public static Map<String, Integer> BestPics(FacebookClient client,
			String userid) {
 
		Map<String, Integer> data = new HashMap<String, Integer>();
 
		List<JsonObject> albumdata = client.fetchConnection("me" + "/albums",
				JsonObject.class, Parameter.with("fields", "id,name"))
				.getData();
 
		for (int x = 0; x < albumdata.size(); x++) {
			try {
				JsonObject ajobj = albumdata.get(x);
				String albumname = ajobj.getString("name").toLowerCase();
				System.out.println(albumname);
				if(albumname.contains("timeline"))
					continue;
				List<JsonObject> idata = client.fetchConnection(
						ajobj.getString("id") + "/photos",
						JsonObject.class,
						Parameter.with("fields",
								"id,created_time,likes.limit(1).summary(true)"),Parameter.with("offset", 0),
								Parameter.with("limit", 1000)).getData();
				for (int xx = 0; xx < idata.size(); xx++) {
					try {
						JsonObject jobj = idata.get(xx);
						JsonObject likes = jobj.getJsonObject("likes");
						String yearstr = jobj.getString("created_time");
						String year = yearstr.split("-")[0];
						if(!year.equals("2015") && !year.equals(2016) && m ==9)
							continue;
						JsonObject ary = likes.getJsonObject("summary");
						data.put(jobj.getString("id"),
								ary.getInt("total_count"));
 
					} catch (Exception e) {
						// e.printStackTrace();
					}
				}
			} catch (Exception e) {
 
			}
 
		}
 
		Map<String, Integer> idset = new LinkedHashMap<String, Integer>();
		try {
 
			data = sortByValue(data, 20);
			String ids = formInString(data.keySet(), 20);
			int cc = 0;
			for (String id : ids.split(",")) {
 
				if (cc++ == 12)
					break;
 
				try {
					JsonObject jj = client.fetchObject(id, JsonObject.class,
							Parameter.with("fields", "images"));
					JsonArray j = jj.getJsonArray("images");
					String source = "";
					int height = 0;
					for (int c = 0; c < j.length(); c++) {
						JsonObject io = j.getJsonObject(c);
						int h = io.getInt("height");
						if (h > height) {
							height = h;
							source = io.getString("source");
						}
					}
					idset.put(source, data.get(id));
				} catch (Exception e) {
 
				}
			}
 
 
 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return idset;
 
	}

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 *