Facebook page rate limit - fetch post with comments and subcomments - facebook

I'd like to fetch facebook Post with comments and subcomments (lib: restfb for java)
public Post getNewPost(String postId) {
...
String fields = ".... fields ...";
Post foundPost = facebookClient.fetchObject(postId, Post.class, Parameter.with("appsecret_proof", prof), Parameter.with("fields", fields));
And it works great, fetching post and post content. My question is that 'request' costs me api call for each comment and subcomment ??
E.g
1) post -> 2 comments
By fetching this post will I use 1 api call (1 for post & content) or 3 api calls ? (1 for post & 2 for comments).
I know that graph api provide .limit(x) function, but I have to fetch post and any comment from this post with at least N history comments. And it doesn't matter if it is comment or subcomment. So I can't use until / since and limit functions.
So my 2nd question is there any way to fetch post and all comments and subcomments with only 1 api call or how to minimalize api calls.

Okay, so the answer is: it depends. You can not get unlimited amount of comments in one call, so you have to page through the comments.
To get all comments and subcomments you can use the filter=stream parameter. With this option all comments and subcomments are returned as plain list in chronological order. See here https://developers.facebook.com/docs/graph-api/reference/v2.9/object/comments/
If you only fetch one post with all comments you can fetch the Post (1 call) and then fetch the depending comments (total count comments + subcomments divided by count of elements per page). I'm not sure about the max limit you can get per page but I think it's between 200 and 500. So you can optimize the calls.
BTW you can fetch the comment count with your post and drop requesting the comments if there are none. Or you can fetch with the post the max limit of comments. Then you have to page only if there are more than max limit comments.

Related

getting full number of comments of facebook status in API v2.6

How to get number of all comments (number of status comment + number of comments of comments) without looping over every comment?
This parameters show only number of direct comments of status, without nested comments
?fields=comments.summary(true).limit(0)
How to do it similarly to FQL?
FQL requests have no problem with it
SELECT id,likes,post_fbid,time,fromid,text,text_tags,parent_id FROM
comment WHERE post_id = %post_id%
it returns all comments (nested or not) as is. easy to count and easy to check of something changed
Found an answer: you should use filter = stream
likes this
?fields=comments.summary(true).filter(stream).limit(0)
It's in the official documentation, but was not very obvious to me, that it can be used in object endpoint and not only in object/comments.
https://developers.facebook.com/docs/graph-api/reference/v2.6/object/comments#readmodifiers

Querying all public facebook posts

I need to build an application that will have three input fields for three URLs. Application then needs to search all public posts on facebook and find users who posted that specific URLs. I'm using this code:
$q = "http://www.someurl.com";
$search = $facebook->api('/search?q=' . $q .'&type=post&limit=200');
foreach ($value as $fkey=>$fvalue) {
if(isset($fvalue['from']['name']))
{
echo $fvalue['from']['name']."<br />";
}
}}
This prints out 200 facebook user's names that posted one specific link. But, as I mentioned above, I need to search for multiple URL match. By using this approach, I would need to make three query calls and then cross-reference results and get users that appear on all three result lists. Is there any way to form query to return needed results in just one call? I now that FQL is powerful tool, but I think that it cannot be used for this kind of public queries. Am I really limited only to public graph api? And if that is the case, is it possible to form complex query using only graph api?
EDIT#1:
I tried using following FQL:
SELECT source_id FROM stream WHERE
CONTAINS('http://www.incgamers.com/2013/12/doom-20th-anniversary-today-true-classic')
AND CONTAINS('http://kotaku.com/5917693/ten-years-of-civ-ii-lock-the-world-in-perpetual-war')
AND CONTAINS('http://www.youtube.com/watch?v=1TBxdXm3DP0') limit 200
As I understand, this should return users who have these three links in their fb stream. However, that is not the case. Am I getting this all wrong?
A simpler approach will be to just check the presence of a URL in the message part of the Post. This fql should work:
SELECT message FROM stream WHERE CONTAINS("http://www.incgamers.com/2013/12/doom-20th-anniversary-today-true-classic")
AND strpos(message,'http://www.incgamers.com/2013/12/doom-20th-anniversary-today-true-classic') >=0
Similarly, to search for a post containing all three links you can extend the fql further.
SELECT message FROM stream WHERE CONTAINS("http://www.incgamers.com/2013/12/doom-20th-anniversary-today-true-classic")
AND strpos(message,'http://www.incgamers.com/2013/12/doom-20th-anniversary-today-true-classic') >=0
AND CONTAINS("http://kotaku.com/5917693/ten-years-of-civ-ii-lock-the-world-in-perpetual-war")
AND strpos(message,'http://kotaku.com/5917693/ten-years-of-civ-ii-lock-the-world-in-perpetual-war') >=0
AND CONTAINS("http://www.youtube.com/watch?v=1TBxdXm3DP0")
AND strpos(message,'http://www.youtube.com/watch?v=1TBxdXm3DP0') >=0
it is no longer possible since facebook disabled public post searching in mid December 2013.

a unique combination of fql and graph api

am trying to search the fb posts of a given keyword but need only results with condition of having more than a specific comments count or likes count, i mean the results will only have posts which have a minimum of given value in comments count and likes count.
i tried with many developers but all are saying that its not possible, as the graph api don't offer this kind of method or http call functions.
but what my point is, there will be a possible way to mix both fql and graph and get the results, something like this, but i don't know the exact schema to use.
https://api.facebook.com/method/fql.query?format=JSON
&query=select comments from comment where object_id in
(select comments_fbid from link_stat where url ='http://developers.facebook.com/docs/reference/fql/comment/')
&pretty=1
You can mix Graph API and FQL requests in a single call using batch requests. You have to use the new Graph API url instead of the REST API url you show in your question. You will also need an access_token to get this data.
Facebook does have a comments.count and a likes object for the comment FQL table. That give a numerical comment count you can query against:
SELECT comments, likes FROM comment WHERE object_id IN
(SELECT comments_fbid FROM link_stat WHERE url ='http://developers.facebook.com/docs/reference/fql/comment/')
AND (comments.count > 5 OR likes > 5)

facebook graph api comment list sort , like 'orderby=desc'?

I use graph api to get the picture's comments, but I want to first sort the results by creating time and then return to the latest data. Similar to the sql statement 'order by create_time desc', I do not know if have such a parameter.
Currently used to offset and limit access to the latest data, but also know the total number of comments,
pagesize = 25;
offset = comments.count - pagesize;
limit = 25;
url = "https://graph.facebook.com/" + object_id + "/comments?access_token=" + access_token + "&limit=" + limit + "&offset=" + limit;
next page:
offset -= 25
but comments.ount of numerical sometimes is not accurate
and the result of the request URL to return to sometimes don't match
Whether to have very good solution
Or I used the wrong way (‘limit’ and ‘offset’ Parameter)!!!
Thank you for your answer.
"Graphics API" the existence of the cache?
i post a message and 46 comments.requests url, set the parameters:
offset=0&limit=1
Then it should return to the last comment (latest one), the actual return to the middle of a comment, and I tested a few times, set the
offset and limit. According to the returned results, the middle one is
the latest comment
If I set the limit value is greater than the 'comment.count', the returned data is all, the official website and facebook consistent
Because the cache reason?
Thanks again~
#dbau - You are still better off using FQL. In my experience, unless you are making a very simple call, you have very little control over what you get via a Graph API call.
Why don't you want to use FQL? FQL is an endpoint of the Graph API. There is still some data that can only be returned via FQL.
This will get you the result you're looking for. The query needs to be URL encoded. I left it in plain text for clarity.
https://graph.facebook.com/fql?access_token=[TOKEN]&q=
SELECT id, fromid, text, time, likes, user_likes FROM comment
WHERE object_id = [OBJECT_ID] ORDER BY time DESC LIMIT 0,[N]
You may find you don't get [N] comments returned each time, because Facebook filters out items that are not visible to the access_token owner after the query is run. You could either up the LIMIT and filter out any excess results returned or if you are using a user access_token, you could add AND can_like = TRUE to the WHERE clause to be guaranteed that, if they exist, [N] posts visible to the current user are returned.
Graph API returns latest objects first.
Facebook provides 2 keywords to filter the fetched data.
Limit : Returns "limit" number of latest records
Offset : Returns "limit" number of records from the offset position
So to retrieve latest "x" comments posted for an object
https://graph.facebook.com/[OBJECTID]?limit=[X]&offset=0
To retrieve next "X" comments (page wise)
https://graph.facebook.com/[OBJECTID]?limit=[X]&offset=[X*PAGENo]
Hope the answer is clear enough for you.

Facebook fb:comments Graph API

i want to have facebook comments + my own website comments on my site.
The thing is when showing posts i want to show a comment count next to every single one(so my comments + facebook comments).
I know i can achieve this with https://graph.facebook.com/comments/?ids={PAGE_URL} but i have 100posts per page and i don't want to do this query a 100 times per page, also and more important is that i want to create a most commented widget where i currently have 1/4 of a million (250000) posts.
So basically my question is how i can access sort of a database on all comments left on my domain/site and sort them, that is manipulate them?
Here are some examples of ways you could do this:
FQL:
You can build your a JSON array of queries and then use the Rest API fql.multiquery method to run them. For example, this would be your JSON array of queries:
{
'query1': "select post_fbid from comment where object_id in (select comments_fbid from link_stat where url ='http://developers.facebook.com/docs/reference/fql/comment/')",
'query2': "select post_fbid from comment where object_id in (select comments_fbid from link_stat where url ='http://developers.facebook.com/docs/reference/fql/album/')"
}
Run this using the test console on the fql.multiquery page and you'll be able to see a response containing a list of post_fbids which you could then count using your favored counting method.
Graph API:
Here you can use a Batch Request to run all of your queries at once. So for a PHP Example you'd be doing:
curl \
–F ‘access_token=…’ \
-F ‘batch=[ \
{“method”: ”GET”, “relative_url”: ”comments/?ids={PAGE_URL1}”}, \
{“method”: ”GET”, “relative_url”: ”comments/?ids={PAGE_URL2}”}, \
{“method”: ”GET”, “relative_url”: ”comments/?ids={PAGE_URL3}”} \
]’\
https://graph.facebook.com
For as many pages as you want.
Note: Given that both APIs do have a bit of a delay before you'll get a response, obviously it's recommended to run them asynchronously so that you aren't causing your site load to delay significantly.
Try this:
Place the URL separated by commas, If you wanna fetch multiple calls using the graph API :
https://graph.facebook.com/comments/?ids=http://URL_1,http://URL_2,http://URL_n
I think for your use case FQL will suit the best : https://developers.facebook.com/docs/reference/fql/comment/ .
On the side note, if you wanna do multiple http calls using the graph API , its always good to do "Batch calls" as documented in the graph API documentation.