facebook fql retrieve comments - facebook

Show comments of every one who posted it in my page
This is giving blank page
Where am i going wrong
FB.api({
method: 'fql.multiquery',
queries: {
query1: 'SELECT post_fbid, fromid, text, time FROM comment WHERE post_id="'+postID +'"',
query2: 'SELECT id, name, url, pic FROM profile WHERE id IN (SELECT fromid FROM #query1)'
}
});
</script>
<script>
function(response) {
var comments = response[0].fql_result_set;
var users = response[1].fql_result_set;
//loop through the comments
for(var i = 0, j = comments.length; i<j; i++){
for(var x = 0, y = users.length; x<y; x++){
if(comments[i].fromid == users[x].id){
//we have a match, this comment is from user users[x]
//process users[x]
//break the inner for loop, since we already have a match
}
}
}
}

Are you using the new Facebook comments (“migrated”)?
Perhaps consider using the graph API instead:
https://graph.facebook.com/comments/?ids=http://example.com/

Related

working with entity framework query

I am working on entity to get the posts on user wall of posted by him and posted to him and posted by his friends
For this I have 4 tables
userprofile
post
postcomment
friends
Userprofile table has UserID, UserName, ProfileImg
Post table has PostId, postedby, postedto, message,posteddate
postcomment table has commentid, postid, commentedby, message, commenteddate
friends table has id, Username, friendUserName
and in entity
userprofile to post--- 1 to many
post to postcomment --- 1 to many
userprofile to postcomment--- 1 to many
post to friends --- many to many
I have used the query shown below to get posts posted by him and posted to him... but I am confused on how to include the posts posted by his friends.... please help me in this
var ret = (from post in db.Posts.ToList()
where post.PostedBy == (int)Session["UserID"] ||
post.PostedBy == db.friends.all(something to get friends one by one)||
post.PostedTo == Session["UserName"].ToString()
orderby post.PostedDate descending
select new
{
Message = post.Message,
PostedBy = post.PostedBy,
PostedByName = post.UserProfile.UserName,
ProfileImage = imgFolder + (String.IsNullOrEmpty(post.UserProfile.ProfileImg) ? defaultAvatar : post.UserProfile.ProfileImg),
PostedDate = post.PostedDate,
PostedTo = post.PostedTo,
PostId = post.PostId,
PostComments = from comment in post.PostComments.ToList()
orderby comment.CommentedDate
select new
{
CommentedBy = comment.CommentedBy,
CommentedByName = comment.UserProfile.UserName,
ProfileImage = imgFolder + (String.IsNullOrEmpty(comment.UserProfile.ProfileImg) ? defaultAvatar : comment.UserProfile.ProfileImg),
CommentedDate = comment.CommentedDate,
CommentId = comment.CommentId,
Message = comment.Message,
PostId = comment.PostId
}
}).AsEnumerable();
im returning ret to jquery to dynamically update the posts.... is there any solution or i have to completely change the query ??? I want to get the posts posted to the user and posted by the user and posted by his friends to display on user wall ... what is the best way to do it??
UPDATE
As of now i got this solution... there may be better solution for this.. if any please let me know..
var ret = (from post in db.Posts.ToList()
where post.PostedBy == (int)Session["UserID"] ||
(from frnd in db.friends where frnd.userId == (int)Session["UserID"] select frnd.frndId).Contains(post.PostedBy)||
post.PostedTo == Session["UserName"].ToString()
orderby post.PostedDate descending
select new
{
Message = post.Message,
PostedBy = post.PostedBy,
PostedByName = post.UserProfile.UserName,
ProfileImage = imgFolder + (String.IsNullOrEmpty(post.UserProfile.ProfileImg) ? defaultAvatar : post.UserProfile.ProfileImg),
PostedDate = post.PostedDate,
PostedTo = post.PostedTo,
PostId = post.PostId,
PostComments = from comment in post.PostComments.ToList()
orderby comment.CommentedDate
select new
{
CommentedBy = comment.CommentedBy,
CommentedByName = comment.UserProfile.UserName,
ProfileImage = imgFolder + (String.IsNullOrEmpty(comment.UserProfile.ProfileImg) ? defaultAvatar : comment.UserProfile.ProfileImg),
CommentedDate = comment.CommentedDate,
CommentId = comment.CommentId,
Message = comment.Message,
PostId = comment.PostId
}
}).AsEnumerable();

facebook batch/fql friendlists a friend is member of

I'm creating a javascript facebook app to have an overview of my friendlists. Actually I can retrieve my (logged in user = me) friendlists, when I click a friendlist I see all my friends in that list.
When I want now is, when I click a list, I want to see the friends belonging to that list, and a comma separated lists my friend is member of.
For example in the list "Best" I have 3 friends: Athos, Portos, Aramis. But they are also in other lists. So in my app I'd like to get something like this:
Athos: Best, Family, Close Friends, VIP
Portos: Best, VIP
Aramis: Best, Close Friends, Party Animals
I tried with the FQL multiquery, but it seems impossible to get what I want.
So I think I should use a Batch request, but since I don't have a lot of experience using batches here I am asking your help.
So here the FQL query I use to get friends belonging to the list. I need this, because I want to manage friends beginning from a list:
var listID = 123;//just enter a listID
var friendsInList = 'SELECT uid,username,profile_url,first_name,last_name,pic_square FROM user WHERE uid IN (SELECT uid FROM friendlist_member WHERE flid =' + listID +')';
Now I don't know how to proceed in a clean and efficient way. I have a working but inefficient solution:
//[...]FB.api call to get a response object with all my friends using the friendsInList query above
for (var i = 0; i < response.length; i++) {
friendID = response[i].uid;
//call FB.api with the following query:
var friendListMemberships = 'SELECT uid, flid, name FROM friendlist_member WHERE flid IN (SELECT flid FROM friendlist WHERE owner=me()) AND uid IN (select uid FROM friendlist WHERE flid =' + friendID;
}
the above solution works but is highly inefficient as I send a FB.api request for each friend, very slow for big lists... thus I'd like a pro user to help me putting this in a batch (a batch can have maximum 50 requests, thus I'd like to send just 1-2 requests and get back all what I need).
I know how to write a batch, here I put the structure of my code, if it can help you completing it with the second query:
var listID = _listID;
var _queryFriendsInList = 'SELECT uid,username,profile_url,first_name,last_name,pic_square FROM user WHERE uid IN (SELECT uid FROM friendlist_member WHERE flid =' + listID + ')';
var queryFriendsInList = _queryFriendsInList.replace(" ", "+");
var _queryFriendListMemberships = 'I have no idea what kind of query I can write here to get the lists my friends from queryFriendsInList are member of';
var queryFriendListMemberships = _queryFriendListMemberships.replace(" ", "+");
var searchArgs = {
access_token: FB.getAuthResponse().access_token,
batch: []
};
searchArgs.batch.push({
'method': 'GET',
'name': 'friendsInList',
'relative_url': 'method/fql.query?query=' + queryFriendsInList,
'omit_response_on_success': false
});
//here is where I need help
searchArgs.batch.push({
'method': 'GET',
'name': 'friendlistMememberships',
'relative_url': 'method/fql.query?query=' + queryFriendListMemberships,
'omit_response_on_success': false
});
FB.api("/", "POST", searchArgs,
function (response) {
//console.log("log");
}
);
I hope the question and code samples are clear enough. Thank you for helping!
You can get everything you want in one API call with a multiquery. You'll need to assemble this together on the client side with a few loops, but it shouldn't be too hard. Here's the query:
{
"all_friendlists":
"SELECT flid, owner, name FROM friendlist WHERE owner=me()",
"fl_members":
"SELECT uid, flid FROM friendlist_member WHERE flid IN
(SELECT flid FROM #all_friendlists)",
"fl_member_details":
"SELECT uid, name, username FROM user WHERE uid IN (SELECT uid FROM #fl_members)"
}
You can present everything to your user from this one API call. The #all_friendlists query gives all your user's friendlists. Once your user has selected a friendlist, then loop through #fl_members to find all the members of this list, retrieving their details from #fl_member_details. Make another loop through #fl_members to find what other lists they belong to.
Alternately, you could just mash these three lists together into a single presentation page, and use a jQuery data filtering scheme like Isotope to allow your user to sort/filter/etc. on the fly.

How to Convert FQL Result to List of Records.?

I want to Convert my FQL Query result into the List of Records how I can Convert this my Query like this.
"SELECT comments FROM stream WHERE post_id = 'Post_Id' "
I have converted into foreach loop but it will still not get desired result so my question is I want to list of comments of Post.?
Use http://developers.facebook.com/docs/reference/javascript/FB.api/
FB.api({
method: 'fql.query',
query: 'SELECT comments FROM stream WHERE post_id=\'' + POST_ID + '\''
}, function(response) {
alert(response.length);
if(response.length!=1) return; // bail out, no stream item found
var comment_list = response[0].commment_list;
for(var i=0; i<response[0].count; i++) {
console.dir(comment_list[i]);
alert('from: ' + comment_list[i].fromid + ' text: ' + comment_list[i].text);
}
}
);
You can try it out for yourself at http://developers.facebook.com/tools/console/. Just click on Examples and select "everyone-data" and change the query to be your SELECT, and then play with the code.

How to get the time at which a comment was posted

I'm trying use the facebook api to get the last posts from the newsfeed of a user along with the comments attached to it. It's easy to get information about those comments such as the text, the actor id, etc... from a query to the STREAM table,
var query = FB.Data.query('SELECT post_id, actor_id, app_data, message, attribution, created_time, comments FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid={0} AND type="newsfeed") LIMIT 20', response.id);
but it doesn't seem to include the timestamp at which those comments were posted (so I can get the timestamp for the post itself with 'created_time', but not for the comments attached to it).
Does anybody know a way to do this?
Help would be much appreciated.
Thanks a lot.
This is a sample response from a query to the table stream
You could fetch that with something like
var query = FB.Data.query('SELECT post_id, actor_id, app_data, message, attribution, created_time, comments FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid={0} AND type="newsfeed") LIMIT 20', response.id);
query.wait(function(rows) {
for (var i = 0; i < rows.length; i++) {
if (rows[i].stream_post.comments.count > 0) {
var comments_list = rows[i].stream_post.comments.comment_list;
for (var j = 0; j < comments_list.length; j++) {
var comment = comments_list[j];
alert(comment.time);//timestamp
}
}
}
});
Hope this helps.
Disclaimer: Have not debugged the JavaScript. typos may be present.
In:
$resultObject->comments->comment_list[0]->time
there is the unix timestamp from the comment created time.

Facebook API : Data dump of Comment/Like Data

Here is my code:
string code = Request.QueryString["code"];
string appId = "x";
string appSecret = "x";
if (code == "" || code == null)
{
//request for authentication
Response.Redirect("https://graph.facebook.com/oauth/authorize?client_id=" + appId + "&redirect_uri=http://localhost:62543/&scope=email,read_stream");
}
else
{
var fb = new MyFB();
fb.ApplicationSecret = appSecret;
fb.ApplicationID = appId;
string accessToken = fb.GetAccessToken(code);
fb.AccessToken = accessToken;
var fbc = new FacebookClient(accessToken);
dynamic streams = fbc.Query("select post_id from stream where permalink = 'http://www.facebook.com/kevin.clough/posts/882439437244'");
Response.Write(streams.Count);
dynamic results = fbc.Query("select comments, likes from Post where id = " + streams.post_id);
foreach (dynamic comment in results.comments)
{
Response.Write(comment.from.name + ", " + comment.message + "<br/>");
}
}
Getting this error "A session cannot be used when querying the stream table with a viewer_id that is a different from the session owner."
How do I authorize this session to view my own data?
So the first problem I see is that the query is not valid. You have:
select post_id from stream
where permalink = 'http://www.facebook.com/kevin.clough/posts/882439437244'
Per the Facebook documentation here you can only query the stream table using the columns post_id, app_id, source_id, filter_key, and xid.
So you should change your first query to this (using post ID '19292868552_118464504835613' as an example):
select post_id from stream where post_id = '19292868552_118464504835613'
Alternatively, you could do the same thing with the graph using the post id:
dyanmic post1 = fbc.Get("19292868552_118464504835613");
Your second query is wrong mainly because you are querying a table that doesn't exist.
select comments, likes from Post where id = " + streams.post_id
There is no such FQL table 'Post'. I am not sure what the goal of this is, but if you want to get the likes and comments I think it would be easier to use the Graph API.
Comments:
dynamic comments = fbc.Get("19292868552_118464504835613/comments");
Likes:
dynamic likes = fbc.Get("19292868552_118464504835613/likes");
Here is Facebook's documentation on the Post Graph API: http://developers.facebook.com/docs/reference/api/post/
EDIT:
To determine the stream items of a user you will want to read their feed. You can do this using the Graph API also.
dynamic feed = fbc.Get("me/feed");
foreach (dynamic post in feed.data) {
var post_id = post.id;
}
This will get you the most recent posts of the user. From there you can loop through them to find the one you want to get more details.