Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take comments from a post with facebook4j

I'm using facebook4j with java and I'd take all comments from a post in a public profile of Facebook. I manage to read the first 25 comments for each post, but not everything else. I understand that there is a logic of paging behind, but I don't manage to pass from page to page. My code to get comments from a post is:

ResponseList<Post> feeds = facebook.getFeed(p.getRiferimento());
Post post = feeds.get(0);
PagableList<Comment> commenti = post.getComments();
commenti.size();   // return me always 25

I try to cycle commenti, but I reach only the first elements; I also try to convert the PagableList into an array but it has only 25 elements. If I take Paging or Cursor from commenti and I cycle them, they go in loop; finally if I write getCount() it return me always null.

Someone can help me?

like image 273
Valentina Avatar asked Oct 21 '22 05:10

Valentina


1 Answers

Not sure if this is too late for answering your question.

Yes, you need to use Paging to get the next page of items and fetchNext() to get items from the page. It will return null if you reached the end of pages. Following is the example:

public List<Comment> getComments(Post post) {
    List<Comment> fullComments = new ArrayList<>();
    try {
        // get first few comments using getComments from post
        PagableList<Comment> comments = post.getComments();
        Paging<Comment> paging;
        do {
            for (Comment comment: comments)
                fullComments.add(comment);

            // get next page
            // NOTE: somehow few comments will not be included.
            // however, this won't affect much on our research
            paging = comments.getPaging();
        } while ((paging != null) && 
                ((comments = fb_.fetchNext(paging)) != null));

    } catch (FacebookException ex) {
        Logger.getLogger(Facebook.class.getName())
                .log(Level.SEVERE, null, ex);
    }

    return fullComments;
}

However (I put a NOTE in the comment), few comments will be missing. I'm not sure about the cause yet. Since the number is rather small and it won't affect my research, I will just leave it be.

like image 189
Xing Hu Avatar answered Oct 22 '22 18:10

Xing Hu