Facebook FQL multiquery on stream and user details - facebook

I am aiming to create a FQL query to retrieve all of my friends stream posts with their full name and email returned altogether. So I created a FQL multiquery (as below) but i keep getting empty result back (tried to put it in the Graph API Explorer):
"query1":"SELECT actor_id, message FROM stream
WHERE filter_key IN (SELECT filter_key FROM stream_filter WHERE uid = me())
AND actor_id IN (SELECT uid2 FROM friend WHERE uid1 = me())"
"query2":"SELECT uid, name FROM user WHERE uid IN (SELECT actor_id FROM #query1)"
Anyone get an idea how can I do this properly?
Does multiquery works in Graph API Explorer at all?

You need to provide the query as a JSON Object, you were missing the object notation ({...}) and a comma between queries.
This works:
{"query1": "SELECT actor_id, message FROM stream WHERE filter_key IN (SELECT filter_key FROM stream_filter WHERE uid = me()) AND actor_id IN (SELECT uid2 FROM friend WHERE uid1 = me())",
"query2":"SELECT uid, name FROM user WHERE uid IN (SELECT actor_id FROM #query1)"}

Related

FQL Query for Stream returns no status messages for Friends

I am trying use FQL to get status updates of all the users friends using FQL. The challenge I am having is that... while I DO get data for companies/Brands/People the user likes or follows ...I get NO data for the users friends… I can see the users stream on facebook and its loaded with posts… but when I run the queries I get nothing… following are the queries I have tried…
SELECT source_id, actor_id, post_id, message,description, comment_info.comment_count, like_info.like_count, created_time
FROM stream
WHERE filter_key IN ( SELECT filter_key FROM stream_filter WHERE uid = me() )
I have tried the above query with an additional AND clause in the nested query e.g ”type = ‘newsfeed’ / type = ' friendliest’
I have also tried…
SELECT type, source_id, share_count, permalink, description, post_id, actor_id, target_id, message, created_time
FROM stream
WHERE source_id in (SELECT uid1 FROM friend WHERE uid2=me())
I also added an AND clauses with various type e.g type ='56’
The following are the permissions granted to the assess token…
basic_info, create_note, export_stream, friends_status, photo_upload, public_profile, publish_actions, publish_checkins, publish_stream, read_stream, share_item, status_update, user_friends, video_upload
Baffling… I say... Baffling… please help…. Thanks
I think you switched the uid1 and uid2 fields in your friend subquery. You can try the following:
SELECT source_id, actor_id, post_id, message,description, comment_info.comment_count, like_info.like_count, created_time FROM stream WHERE source_id in (select uid2 from friend where uid1 = me()) and filter_key="app_2915120374" LIMIT 100
filter_key="app_2915120374" means "Status Updates".
Alternatively, you could use the Graph API liek this:
GET /me/friends?fields=statuses.limit(5)&since=1388530800&limit=500
will return you the last 5 status updates of your friends since 2014-01-01.

FQL Query: Get all posts and comments made to my stream by friends only

I am using the following FQL Query to all my friends from my friends list successfully:
SELECT uid2 FROM friend WHERE uid1 = me()
I'd like to use all of the uid2's returned to perform a second query (or just combine it into one big one !!!!) that grabs all comments, posts and messages on my news_feed by friends only.
Heres what I have so far that grabs friends and non-friends.
SELECT type, created_time, post_id, actor_id, message FROM stream WHERE filter_key IN (SELECT filter_key FROM stream_filter WHERE uid = me())
I'm only concerned with grabbing content (messages and textual info) posted by friends only. If you have a query that could help that would be great. FYI I have already been to the FB stream table page.
Append your 1st statement as another where clause to your 2nd:
SELECT type, created_time, post_id, actor_id, message FROM stream
WHERE filter_key IN (SELECT filter_key FROM stream_filter WHERE uid = me())
AND actor_id IN (SELECT uid2 FROM friend WHERE uid1 = me())

Very complex FQL queries very slow

I'm trying to get the first 50 posts (posts by friends) from the user's news feed using facebook FQL, as well as each friend's profile information. I have two queries, which total 10 seconds of processing:
SELECT actor_id, message FROM stream
WHERE filter_key IN
(SELECT filter_key FROM stream_filter WHERE uid= me() AND type='newsfeed')
AND actor_id in (SELECT uid2 FROM friend WHERE uid1 = me())
LIMIT 50
This query returns the user, even if the message is "" (I couldn't find a NOT operator to filter out rows that have "" as a message)
Second query:
SELECT pic_big, name,url from profile WHERE id IN
(SELECT actor_id, message FROM stream WHERE filter_key IN
(SELECT filter_key FROM stream_filter WHERE uid= me()
AND type='newsfeed')
AND actor_id in (SELECT uid2 FROM friend WHERE uid1 = me()))
LIMIT 50
I've tried other ways to get the data (quicker) however, it returns it in a random order (facebook doesn't guarantee that the data comes out the way it went in).
I could place a "loading" animation on my website but I'd prefer not to because I think that this can be optimized but I'm not sure how.
In the past, I used the graph api but I had to process the results myself, and it was very inefficient (taking over 30+ seconds).
For these two queries, you should be able to speed things up quite a bit by combining them into a single multi-query, especially since your second query includes your first query. This one is the most complex, and if you're sending them in separate API calls, Facebook has to perform it twice.
{
'q1':"SELECT actor_id, message FROM stream
WHERE filter_key IN
(SELECT filter_key FROM stream_filter WHERE uid= me() AND type='newsfeed')
AND actor_id IN (SELECT uid2 FROM friend WHERE uid1 = me())
LIMIT 50",
'q2':'SELECT pic_big, name, url, id FROM profile WHERE id IN
(SELECT actor_id FROM #q1)'
}
Your two queries take 4072 + 1304 = 5376ms when I run them in the Graph API explorer. The multiquery takes 3475ms. With network latency eliminated from running two separate queries, this should drastically speed up your app.
You can filter out blank messages by adding AND strlen(message) > 0 to the WHERE part of your first query.
You can use ORDER BY column to force the return of data in a specific order, but there is no guarantee that you will get all the rows in your result.

How to get FQL Stream with full names

I would like to show stream data from FQL
fql?q=SELECT post_id, app_id, source_id, updated_time, created_time, filter_key,
attribution, actor_id, target_id, message, app_data, action_links,
attachment, impressions, comments, likes, place, privacy, permalink, xid,
tagged_ids, message_tags, description, description_tags, type
FROM stream WHERE filter_key in (
SELECT filter_key FROM stream_filter WHERE uid=me()
AND type='newsfeed')
but the problem is that that table doesn't have name of the users/pages, only ids.
Is there any way to get names to those results too? And not only actor_id, but also name for the people who has made comments.
Do I need to make multiquery and search against all those ids in the stream, and then fetch from user and page tables. And then loop those results for every single stream result, seems kind of heavy. Any easier way?
I'm not sure if there is a way to get all the users and pages in one go but you could run two separate queries that will return the users/pages and then performance an array merge with both sets of results.
Users
SELECT uid,name from user where uid in (SELECT source_id FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed'))
Pages
SELECT page_id,name from page where page_id in (SELECT source_id FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed'))
You can do it in following way :
NSString *query =
#"{"
#"'friends':'SELECT post_id,actor_id,target_id,message,created_time, likes, comment_info FROM stream WHERE source_id IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND created_time > 1 ORDER BY created_time DESC LIMIT 50',"
#"'friendinfo':'SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT actor_id FROM #friends)',"
#"}";
Once you will get result, store it into two arrays like this :
FBFriendsNewsArray =(NSArray *) [[[result objectForKey:#"data"]objectAtIndex:0]objectForKey:#"fql_result_set"];
FBFriendsInfoArray=(NSArray *) [[[result objectForKey:#"data"]objectAtIndex:1]objectForKey:#"fql_result_set"];
And while displaying Feed search for that user id (actor_id) in Friends info array. hope this will help.

Best way to get common "likes" from public info page out of your friends list

I'm trying to get a list of common likes (public) between a user and his friends. I thought this FQL query would work but I'm getting an unknown user error:
--get page_id that you have and any of your friends have
https://api.facebook.com/method/fql.query?query=
select uid, page_id from page_fan where uid in (SELECT uid2 FROM friend WHERE uid1 = me()) and page_id in (select page_id from page_fan where uid= me())
&access_token=ABC
I'm getting this error:
<error_response><error_code>1</error_code><error_msg>An unknown error occurred</error_msg></error_response>
Any suggestions?
It seems that the result set is way to big for the API to handle. Your best bet is to try to limit the result set by either query a set of friends against the user's like or query one, two ...etc pages at a time (if that also didn't work try to LIMIT your friends to say 100 to make sure you are actually getting something). Example of a query:
select uid, page_id
from page_fan
where uid in (
SELECT uid2
FROM friend
WHERE uid1 = me()
) and page_id in (
select page_id
from page_fan
where uid= me()
LIMIT 1
)
Obviously there is no offset in FQL. So first, you need to retrieve the user's likes and then query against them. You can always use the batch api to make multiple calls in a single batch call.
SELECT page_id from page_fan WHERE uid = me() and page_id IN ( SELECT page_id from page_fan WHERE uid = give one hid here )