masu.mo
masu.mo

Reputation: 793

How to find the number of comments and list of comments using Facebook social Graph API search?

By using Graph Facebook API I'm trying to find all public contents related to specific keyword.

https://graph.facebook.com/search?q=obama&type=post  

I get the result as I expected, but recently I also want to find the number of comments and list of individual comments related to specific result.

This is an output example of above API call:

{
     "id": "1372608125_2297171713409",
     "from": {
        "name": "xxxx",
        "id": "1372608125"
     },
     "message": "Originaly posted by:Anthony McMahan\n\n Political breakdown time....\n \nToday is short one, here is some information that will floor you if you werent aware of it yet. The United States of America makes approximately 15 trillion dollars annually GDP. Our debt has recently tipped over 15 trillion in recent days. So we now owe more than we make in a year. \n\nTo further add to the fury, medical facilities in the USA account for 1/6 of our GDP, so about 2.5 trillion dollars are brought in from hospitals across the US. the new healthcare bill is a stretch for the givernment to take control of all medical facilities in the US, which would basically cancel out 1/6 of our income as the healthcare would be made available to everyone. Therefore in light of the US being in more debt than we could make in a year, the plan is to take away 1/6 of our ability to make money. Imagine being in credit card debt and having to take a pay cut at work, because you chose to work in the mailroom instead of a cubicle. thats what is going on here in allegory. i would just like to take this time to say, \"thank you mr obama for making this country a laughing stock in the eyes of all our enemies and foreign nations, you'd make Karl Marx and Vladimir Lenin all too proud.\" \n\nUntil tomorrow........",
     "type": "status",
     "created_time": "2011-12-15T06:02:51+0000",
     "updated_time": "2011-12-15T06:02:51+0000"
  }  

Suppose I want to find the number of comments and list of comments related to that post. Should I use another API call?
Is it possible to know the number of comments directly within 'Graph API search' without having to call another API? I need this so that if a post has comment I will run another API to get list of comments, otherwise I don't need to run additional API call.

As I know to get the list of individual comments I need to run the following API call:

http://graph.facebook.com/1372608125_2297171713409/comments  

Is that the correct way to do so? Here, I also don't get any clues whether that post really have comments or not.

I'm actually expecting to get similar feature as Google+ API has where within one search API call, I can get the number of replies (comments) like the following:

"replies": {
 "totalItems": 53,
 "selfLink": "https://www.googleapis.com/plus/v1/activities/z13rhfhjzzm4ztrnt22kwpgglsrjdfra504/comments"
},

Upvotes: 1

Views: 3704

Answers (3)

Björn Kaiser
Björn Kaiser

Reputation: 9912

The more simple way is to use a so called "Batch Request". It let's you execute multiple Graph API requests at once and even let's you define dependencies between requests. The full documentation can be found here (you need to scroll down a bit to "Specifying dependencies between operations in the request").

Your first request would be the search result and the second one would depend on the results of the first call using the Graph ID of each result to get the comments for each search result.

I have not yet worked with dependent Batch Requests but you should give it a try :-)

Upvotes: 1

jches
jches

Reputation: 4527

Yes, you will need to use a second API call for each post.

Is it possible to know the number of comments directly within 'Graph API search' without having to call another API? I need this so that if a post has comment I will run another API to get list of comments, otherwise I don't need to run additional API call.

Normally when reading a wall or feed, you can request the comments field of each post, which will tell you how many comments there are (try /me/feed?fields=comments). This doesn't seem to be available in the search API for whatever reason, and requesting the comments field returns an error from the API.

The correct way to request comments on a post is indeed:

http://graph.facebook.com/1372608125_2297171713409/comments

If you get an empty list back, there are no comments. Otherwise, you'll get a list of comments. How you check for this will depend on the language you're using. For a post with no comments, the raw JSON data returned will look something like this:

{
  "data": []
}

Posts on very active pages can have thousands of comments, so you may want to specify the limit parameter to get a bunch of comments at a time if you plan on hitting many of those:

http://graph.facebook.com/1372608125_2297171713409/comments?limit=1000

If there are less than a thousand comments, you'll get them all back at once with this.

Upvotes: 0

Jordan M.
Jordan M.

Reputation: 310

Your best bet is most likely to have your code run a count on the number of comments to get the number of comments (if any) in the thread. Unfortunately, you're going to have to make the /id/comments call in order to do this. I am unaware of any way to accomplish this with the additional API call.

Upvotes: 0

Related Questions