Java code to download all status updates on any Facebook page

I had a requirement in which I had to download status updates of any given Facebook page. Download will be based on some criterias like minimum likes, limit on number of updates, how old the status update is etc.

I have taken the code snippet and pasted here. It will have few extra things which you can ignore. This code might be helpful to others.

This code makes use of restFb java client and assumes you already have the access token : Facebook login

It parses a given Facebook page, downloads all status updates based on given criterias, puts them in a text file and finally zips the file

	private void statusDownload(HttpServletRequest req,zips
			HttpServletResponse resp, boolean preview) {
 
		String pageid = parsePageid(req, resp);
		if (null == pageid) {
			return;
		}
		String likes = req.getParameter("likes");
		String limit = req.getParameter("limit");
		String ecaption = req.getParameter("ecaption");
		String time = req.getParameter("imageage");
		String statusseparator = req.getParameter("statusseparator");
		String name = req.getParameter("name");
		if (!preview) {
			beforeDownload(req, IMAGEDOWNLOAD, name);
		}
		if (statusseparator == null || statusseparator.length() < 3) {
			statusseparator = "UPDATE";
		}
		FacebookClient client = new DefaultFacebooclient(ACCESS_TOKEN);
 
		String userid = Helper.getUserId(req, client);
		if (time == null) {
			time = "180";
		}
		try {
			Integer.parseInt(time);
		} catch (Throwable t) {
			time = "180";
		}
		time = (System.currentTimeMillis() - Integer.valueOf(time) * 2184000000l / 30)
				/ 1000 + "";
 
		if (likes == null) {
			likes = "10";
		}
		if (limit == null) {
			limit = "20";
		}
 
		if (Integer.valueOf(limit) > STATUSUPPERLIMIT) {
			limit = STATUSUPPERLIMIT + "";
		}
 
		List<JsonObject> qq = new ArrayList<JsonObject>();
		int intlimit = Integer.valueOf(limit);
		for (int x = 0; x < intlimit; x += 100) {
			if (intlimit > 100 && x + 100 > intlimit) {
				limit = (Integer.valueOf(limit) - 100) + "";
			}
			try {
				qq.addAll(getPartialStatuses(client, pageid, x, limit));
			} catch (Exception e) {
 
			}
		}
 
		File f = new File(DOWNLOADFOLDER + "/status/" + userid);
		if (!f.exists()) {
			f.mkdir();
		}
		pageid = name + DELIM + pageid;
		String userfolderfiles = DOWNLOADFOLDER + "/status/" + userid + "/"
				+ pageid;
		f = new File(userfolderfiles);
		if (!f.exists()) {
			f.mkdir();
		}
		f = new File(userfolderfiles + "/" + pageid + ".txt");
		BufferedWriter bww = null;
		int size = 0;
		String statuspreview = "";
		try {
 
			bww = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(f), "UTF-16"));
 
			for (JsonObject obj : qq) {
				String message = obj.getString("message");
				boolean match = false;
				if (ecaption != null && ecaption.length() > 2) {
					for (String s : ecaption.split(",")) {
						if (message.toLowerCase().contains(s.toLowerCase())) {
							match = true;
							break;
						}
 
					}
				}
				if (match)
					continue;
				size++;
				if (1 == size) {
					statuspreview = message;
				}
				if (!preview) {
					bww.write("[" + statusseparator + "-" + size + "]");
					bww.newLine();
					bww.write(message);
					bww.newLine();
					bww.newLine();
				}
			}
 
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bww != null)
				try {
					bww.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
 
		}
 
 
		ZipHelper zippy = new ZipHelper();
		try {
			zippy.zipDir(userfolderfiles, userfolderfiles + ".zip");
		} catch (IOException e2) {
			System.err.println(e2);
			throwError(GENERICERROR, resp);
		}
 
 
		JsonObject jo = new JsonObject();
		jo.put("zipfile", STATUSHTTPLINK + File.separator + userid
				+ File.separator + pageid + ".zip");
		jo.put("status", "success");
 
 
 
		Helper.sendJson(jo, resp);
		return;
 
	}
 
	private static String parsePageid(HttpServletRequest req,
			HttpServletResponse resp) {
		FacebookClient client = Helper.getFacebookClient(req);
		String pageid = req.getParameter("album");
		boolean error = false;
		try {
			Page page = client.fetchObject(pageid, com.restfb.types.Page.class);
			pageid = page.getId();
		} catch (Throwable e) {
			System.out.println("statusDownload-" + pageid);
			error = true;
		}
		try {
			Long.valueOf(pageid);
 
		} catch (Throwable e) {
			error = true;
		}
		if (null == pageid || pageid.length() < 2 || error) {
			throwError(PAGEERROR, resp);
			error = true;
		}
		if (error)
			return null;
		else
			return pageid;
	}
 
	private List<JsonObject> getPartialStatuses(FacebookClient client,
			String pageid, int x, String limit) {
		return client.fetchConnection(pageid + "/statuses", JsonObject.class,
				Parameter.with("limit", limit),
				Parameter.with("fields", "message"),
				Parameter.with("offset", x)).getData();
	}

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 *