javafacebookfacebook-graph-apirestfb

Get only latest posts of Facebook using RestFB


I'm using RestFB to get posts from pages of facebook. The problem is I only want the latests posts of the page, let's say, the posts of the last month. With my code I get all the history and it take a lot of time. How do I do that? I havn't read about a "timestamp" parameter. I have tried with the limit parameter without a good result.

My code is:

public static JSONArray GetPostsFromPage(String idpage) {
    String texto, idpost;
    Date timestamp;
    JSONArray array = new JSONArray();
    JSONObject objeto;
    Connection<Post> postFeed = fbClient.fetchConnection(idpage + "/feed", Post.class, Parameter.with("limit", 50));

    for (List<Post> postPage : postFeed) {
        for (Post aPost : postPage) {
            objeto = new JSONObject();
            idpost = aPost.getId();
            texto = aPost.getMessage();
            timestamp = aPost.getCreatedTime();

            objeto.put("id", idpost);
            objeto.put("texto", texto);
            objeto.put("timestamp", timestamp);

            array.put(objeto);
        }
    }
    return array;
}

Any idea will be welcomed. Thank you.


Solution

  • My first suggestion: Check the timebased pagination here: https://developers.facebook.com/docs/graph-api/using-graph-api

    You simply have to add Parameter.with("since", "<timestamp>") to you fetchConnection call.

    Second suggestion: Leave the loop on a saved timestamp. You simply save the TS of the latest post and on the next polling iteration you stop the loop on that TS.