How to Convert FQL Result to List of Records.? - facebook-fql

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.

Related

Sequelize -- Use options.distinct with a Where Clause?

I'm trying to use Sequelize to find all distinct fields in a column, using a query with a where clause. I've searched the Sequelize docs and tried a number of different things, but haven't yet found the correct syntax.
Here's my current draft syntax:
var searchResults = connectors.cars.findAll({
attributes: [
connectors.Sequelize.options.distinct
],
where: {
condition: connectors.Sequelize.where(connectors.Sequelize.fn('LOWER', connectors.Sequelize.col('mfgr')), 'LIKE', '%' + searchString + '%')
}
});
What is the correct way to use options.distinct alongside a where clause?
Note: edited to remove a bunch of extra code that had been requested in the comments, but which in retrospect may have been obfuscating the issue.
Really more of a comment, but what's going on with this code?
}).then((searchResults) => searchResults.map((item) => item.dataValues));
debugger;
return searchResults; <== ERROR IS THROWN HERE
1) What is the destination of the map()? 2) Won't the return occur before the results are returned, as it's not in the .then() block? Should it not be more like this:
}).then((searchResults) => {
var new_results = searchResults.map((item) => item.dataValues));
debugger;
return new_results;
}
Am I missing something?
I'm getting the impression that this is hard to do in raw sql, let alone Sequelize. I found an answer that uses raw sql here:
const myQuery = "WITH cte AS\n" +
"( SELECT id, mfgr, ROW_NUMBER() OVER (PARTITION BY mfgr ORDER BY mfgr DESC) AS rn\n" +
" FROM cars\n" +
" WHERE LOWER(mfgr) like '%" + searchString + "%'\n" +
")\n" +
"SELECT *\n" +
"FROM cte\n" +
"WHERE rn = 1";
let searchResults = connectors.db.query(myQuery);

Creating Select statement with variable in single quotes

This relates to taking data from a Google Fusion table.
When I first set up my site, GF tableid was a numeric value, (var tableid = 123456;) and I built a query like this:
layer.setQuery("SELECT 'Latitude' FROM " + tableid + " WHERE 'Name' contains etc etc
Now tableid is something like var tableid = '12DFty24'; and I'm having trouble converting the setQuery to handle it.
I've tried adding an extra single quote around tableid, but that doesn't work. Nor do backslashes.
Ideas would be gratefully received!
Paul
You are using the old syntax that can't work with encrypted ID, numeric ID's are deprecated.
You have to change your code using the new syntax; here is the documentation
Example:
new google.maps.FusionTablesLayer({ query: {[FusionTablesQuery object]}});
And here's the one that works...need to be careful with parentheses and commas!
function searchAddress()
{
var searchString = document.getElementById('searchAddressString').value.replace("'", "\\'");
// layer.setQuery("SELECT 'Latitude' FROM " + tableid + " WHERE 'Address' contains ignoring case '" + searchString + "'");
var layer = new google.maps.FusionTablesLayer({
query: {
select: 'Latitude',
from: tableid,
where: 'Address' contains ignoring case '" + searchString + "'"
}
});
layer.setMap(map);
}

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.

facebook fql retrieve comments

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/

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.