Reactions to Comments on Facebook API - facebook

I am wondering whether it is possible to obtain the reactions for all comments on a particular post? That is, for each comment on the post, can I also obtain the reactions in the current version of the API.

It's possible. Facebook Doc hasn't updated yet. (I think.)
But you can use "reactions" keyword with comment's objects.
Try this code with your sample Comment's ID and Token.
https://graph.facebook.com/v2.10/{comment_id}/reactions?access_token={Your_Token}
Replace {comment_id} and {Your_Token} by your Comment's ID and Token.
For all comments of post, you can use the same with Post' ID. See syntax on Facebook Doc

So if you need a raw count, you can simply add reactions to the fields.
For example:
https://graph.facebook.com/v2.10/{post_id}/comments?fields=reactions.limit(0).summary(1)
Then you can iterate over all comment and they have the reaction count included. Because you need the counts of the different types you can use the field aliasing. This looks like:
"https://graph.facebook.com/v2.10/{post_id}/comments?fields=reactions.limit(0).summary(1)" // reactions overview
+ ",reactions.type(LIKE).limit(0).summary(1).as(reactions_like)" // like reactions
+ ",reactions.type(LOVE).limit(0).summary(1).as(reactions_love)" // love reactions
+ ",reactions.type(WOW).limit(0).summary(1).as(reactions_wow)" // wow reactions
+ ",reactions.type(HAHA).limit(0).summary(1).as(reactions_haha)" // haha reactions
+ ",reactions.type(SAD).limit(0).summary(1).as(reactions_sad)" // sad reactions
+ ",reactions.type(ANGRY).limit(0).summary(1).as(reactions_angry)" // angry reactions
+ ",reactions.type(THANKFUL).limit(0).summary(1).as(reactions_thankful)" // thankful reactions
I used a String concatenation to provide a better overview. You have to adapt this to your language.

Related

Get follower count on Scratch (API)

I am looking to find the follower count of a Scratch user using the Scratch API. I already know how to get their message count, with https://api.scratch.mit.edu/users/[USER]/messages/count/.
This answer targets the Scratch REST API, documented here.
You get the user's followers by requesting them: https://api.scratch.mit.edu/users/some_username/following where some_username is to be replaced by the actual username.
This will return 0 to 20 results (20 is the default limit of objects returned by the REST API). If there's less than 20 results, then you're done. The amount of followers is simply the count of the objects returned.
If there's 20 objects returned, we can't be certain we've requested all the user's friends as there might be more to come. Therefore, we skip the first 20 followers of that user by supplying the ?offset= parameter: https://api.scratch.mit.edu/users/some_username/following?offset=20
This retrieves the second 'page' of friends. Now we simply loop through the procedure described above, incrementing offset by 20 each time until either less than 20 results are returned or no results are returned. The amount of friends of that user is the cumulative count of the objects returned.
As mentioned by _nix on this forum thread, there is currently no API to achieve this. However, he/she rightly points out that the number can be obtained from a user's profile page.
You may write a script (in JavaScript, for example) to parse the HTML and get the follower count in the brackets at the top of the page.
Hope this helps!
There is a solution in Python:
import requests
import re
def followers(self,user):
followers = int(re.search(r'Followers \(([0-9]+)\)', requests.get(f'https://scratch.mit.edu/users/{user}/followers').text, re.I).group(1))
return f'{followers} on [scratch](https://scratch.mit.edu/users/{user}/followers)'
Credit goes to 12944qwerty, in his code (adapted to remove some implementation specific stuff).
use ScratchDB
var user = "username here";
fetch(`https://scratchdb.lefty.one/v3/user/info/${user}`).then(res => res.json()).then(data => {
console.log(`${user} has ` + data["followers"].toString() + " followers");
}
(Edit: this is javascript btw, I prefer Python but Python doesn't have a cloud.set function and this is how I did it)
Use ScratchDB (I used httpx, but you can GET with anything):
import httpx
import json
user = "griffpatch"
response = httpx.get(f"https://scratchdb.lefty.one/v3/user/info/{ user }")
userData = json.loads(response.text)
followers = userData["statistics"]["followers"]
https://api.scratch.mit.edu/users/griffpatch/followers
this gives the follower names, scratch staus(scratch team or not), pfp, everything in their profile

Facebook Graph API event-id/comments?since=2014-02-01&until=2014-02-10 , Date filter has no effect

I am trying to bring comments made on a particular event by targeting this URL: https://graph.facebook.com/1466384840257158/comments
I am passing the user_access_token
I have two comments at present on this event made on the same
day(2014-03-29)
Now I try to pass a date which should bring an empty data result/object
like this: https://graph.facebook.com/1466384840257158/comments?since=2011-01-01&until=2014-01-10
This request has no effect, it still shows me the two comment made
on the 29th
I have tried the same kind of date range on my user-id/feed and it
gave me an empty data object.
Finally i tried event-id/feed (before trying date filter) and it
gave me the following error
.
{
"error": {
"message": "An unexpected error has occurred. Please retry your request later.",
"type": "OAuthException",
"code": 2
}
}
Could you please guide me about date filter on that particular query (point4) or if you have any other idea to use date filter on comments made for an event.
Comments use Cursor-based Pagination, so you cannot use since or until on the comments endpoint (these parameters would work f.ex. for the feed endpoint).
To get the comments in a time range you have to fetch all comments from NOW to the start of the time range, f.ex. with https://graph.facebook.com/1466384840257158/comments?filter=stream&limit=1000+paging (the filter=stream will order the result with the timestamp).
USING SINCE UNTIL FOR COMMENTS on GROUP
If you want to use since and until for comments, it is not possible directly for a group. So, First you can apply it for status(feed) and then get the comments for that feed.
This works for me:
{group_id}/?fields=feed.since(08/25/2016).until(08/31/2016){from,comments{from,message}}
Why don't you try first to filter by notifications?... notifications allows you to add parameters like since. For example (using Facebook pages):
https://graph.facebook.com/PAGEID?fields=notifications.since(2015-3-31 00:00:00).limit(250).include_read(true)&{id,created_time,updated_time,unread,object,link}&access_token=ACCESSTOKEN
Once you got the json data, loop through data, get the ID and send a second request but this time using the PAGEID_POSTID edge. Something like this:
https://graph.facebook.com/PAGEID_POSTID/comments?fields=id,from{name,id},message,can_remove,created_time&limit=1000
Voahla!... there's no need to read every comment!...
Note 1: A Page access token is required, along with the manage_pages permission
Note 2: Use the parameter/field include_read to get all the notifications, even the already readed
Note 3: In the second request, use the parameter/field "filter=stream" to order the posts and get the comments made in the name of your page
Note 4: Don't forget to control the asynchronicity once you loop!
Note 5: Notifications duplicate posts, use an array to avoid to read more than one time the postUse the parameter/field include_read to get all the notifications, even the already readed
I do not know if it's too late. But, Yeah it works in the graph api version 3.3.
for example: if you wanna get comments on a post of a Facebook page you can do it like this:
You have to use page Access-token
The get Graph Request : post_id/comments?since=some_date

Get (Identify) Replies to Comments Using the Graph API

With the new "Reply" to "Comments" feature on Facebook, I've noticed that replies to comments are treated the same as comments. But I was wondering if there is anyway to distinguish the two?
You first have to enable July Breaking Changes from your app advanced settings
Then use the fields parameter with the comments graph API and include the parent.field(id) column with the and also the filter parameter with the stream value. the final result :
{POST_ID}/comments?filter=stream&fields=parent.fields(id),message,from,likes
this should return both comments and replies with the parent element which has the comment id that the reply belongs to
-- update
and for better array arrangement for replies you can use the following to merge replies with the actual comment array you can include comments.summary(true) in the fields parameter
{POST_ID}/comments?limit=0&filter=toplevel&fields=comments.summary(true),message,from,likes
filter parameter is optional
for more info about the fields : http://developers.facebook.com/docs/reference/api/Comment/
and in case you want to do it in FQL, check this post's comments http://developers.facebook.com/blog/post/2013/04/03/new-apis-for-comment-replies/
Yes. You can query each comment object in the Graph API for the value of its parent field. If the comment in question is a reply, then the value of the parent field will be a reference to the parent comment. Otherwise, no value is returned.
Reference here: https://developers.facebook.com/docs/reference/api/Comment/
You can get comment replies in this way.
/{{POST_ID}}/?fields=comments{comments}&access_token={{ACCESS_TOKEN}}
You can get any sub info(from,id) of comment replies by just nesting fields inside comments like this:
/{{POST_ID}}/?fields=comments{comments,from,id}&access_token={{ACCESS_TOKEN}}
Similar post over here :
https://stackoverflow.com/a/37743410/6001533
If you're listening for comments on the 'feed' webhook, you should check if:
entry[0][changes][0][value][post_id] === entry[0][changes][0][value][parent_id]
This will be true for top-level (new) comments, and false for replies to comments.
To piggy back off #sujit's answer I took his answer and in one call from the feed you can get the entire feed, comments and replies to comments as well as associated images to those comments and replies in one shot.
Here is the code
https://graph.facebook.com/$get_facebook/feed?access_token=$facebook_accesstoken&client_id=$facebook_appid&client_secret=$facebook_appsecret&metadata=1&fields=id,status_type,created_time,from,message,comments{comments{attachment,from,id,message},from,id,message,attachment},picture,link,icon

Comments Box Plugin: FQL query on comment table by xid returns an empty array

I expect this FQL query to return a non-empty array, because there are comments for that xid (see here). In the app itself, I use Javascript to perform the FQL query inside of window.fbAsyncInit and after FB.init({ //options }); like this:
var query = FB.Data.query("SELECT xid FROM comment WHERE app_id = " + facebookAppId + " and xid = '" + $this.attr("xid") + "'");
query.wait(function(rows) {
// do things
});
Unfortunately this also returns an empty array ([]). This is bad because we need to use the comments count to decide whether to use the xid attribute (for comment boxes which already have comments) or the href attribute (for comment boxes which don't yet have comments). This will allow us to not lose comments that were made while we used the old-style xid attribute.
Any ideas?
Probably you figured this out by now - I had the exactly same problem and solved it finally. In fact I found that you actually must not include the app_id. The key seems to be to get the correct access token (see my post).
hope it helps.

What do the keys in Facebook's 'data-ft' JSON structure represent?

On Facebook pages, many HTML elements include a 'data-ft' JSON object that is of the form:
data-ft='{
"src":10,
"sty":263,
"actrs":"117307284966434",
"targets":"117307284966434",
"pub_time":1317143005,
"fbid":"153538678072594",
"qid":"5657092603540274768",
"s_obj":5,
"s_edge":1,
"s_prnt":28,
"ft_prefix":"feed_story.top_news",
"ft_story_name":"StreamStoryCreateGeneric_ShareStreamContent_External_Other",
"mf_story_key":"10150331666719785",
"object_id":"153538678072594",
"mf_objid":"153538678072594",
"viewstate_id":"3201743663063655712",
"sub_level":"mid",
"sbj_type":"page",
"is_boulder":"1",
"authentic":1,
"pageid":"117307284966434",
"filter":"h",
"pos":14
}'
What do these keys represent? Some of them are straightforward, such as 'sty' as style, 'actrs' is the Facebook-ID of the original poster, and 'pub_time' is the UNIX epoch representation of the post date & time.
In particular, I am interested in understanding what the 'authentic' key represents, as well as the 'fbid' and 'qid' values.
Thanks for your insight, SO.
This is the root of how BFB (Better Facebook user script) can allow filtering, tabbing, etc.
In the HTML source, there is an attribute on each post that looks like this:
data-ft:{
"src":10, "sty":46, "actrs":"14385334364",
"pub_time":1289830690, "fbid":"1485431831867", "s_obj":11, "s_edge":1,
"s_prnt":11, "pos":1, "sec":"new", "filter":"lf",
"app_id":"201278444497"
}
This is the data we need!
BFB parses this when processing each post and extracts the data.
sty = Story type. Each type of story, like wall posts, status updates, pictures, links, etc has a unique story type with its own number. Unfortunately, these are not documented anywhere! I have to figure out the types by observation and trial and error. It's painful. But knowing this type numbers allows BFB to do filtering based on what kind of story it is.
actrs = The unique Facebook id's of the person (or people) that made the post. Again, good for filtering.
pub_time = The time the post was made. This is useful later...
fbid = The unique Facebook ID of the post. Every post has its own ID. At least, it should. See the explanations below for why this is not as reliable as it sounds
app_id = The unique ID of the Facebook application that made this post