Get all the posts the user liked for a specific period - facebook-fql

I would like to know if it's possible to get, for example, all the posts that the user liked for the last 6 month.
Tell me if I'm wrong but this query is supposed to return all posts I liked :
SELECT object_id, post_id FROM like WHERE user_id = me()
I don't see how to filter the result.

You cannot grab the post_id that easily from the like table. The ONLY way to get a post_id is querying it from the stream table. This is no arbitrary query.
Here is how you should be thinking about it.
First you have to grab the posts from your stream that were posted by the "owner" since that's the easiest way to arbitrarily grab a bunch of posts that you care about.
select post_id
FROM stream
WHERE filter_key="owner"
Then you have to use that result to check if those post_ids are in your like table AND you're the user that liked them:
select post_id
FROM like WHERE
post_id in (select post_id from stream where filter_key="owner"
and created_time> *timestamp from a week ago*)
AND user_id=me()

Related

Getting from Facebook API total number of likes per post

I'm working with the following Facebook API endpoints: /statuses, /links, /photos
For each returned object I'm only getting likes and comments objects which display top25 results, and a pagination that leads to the next 25.
I'm only interested in the number of likes. Is there a way to get that number through a different API call which doesn't require multiple pagination calls?
You can return the total number of likes for various objects using FQL. Here's the documentation for how to get the total number of likes for a comment:
https://developers.facebook.com/docs/reference/fql/comment
The query itself would look like this:
SELECT likes FROM comment WHERE post_id = xyx
SELECT like_info.like_count FROM stream WHERE source_id = '<id>'
or
SELECT like_info.like_count FROM stream WHERE source_id = '<id>' and and actor_id = '<id>'
More: https://developers.facebook.com/docs/reference/fql/stream/

Facebook Graph API: get all the friends' likes

I don't particularly fancy the idea of getting a list of friends with 'me/friends' and then iterating over the list to get likes for a particular friend with '%id%/likes' - this takes way to long.
Is there a way to bulk query likes of users' friends in a single API call?
Thanks.
You can use FQL multi-query to get all at once, here is how:
{
"friends":"SELECT name, uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())",
"friendsLikes":"SELECT uid, page_id FROM page_fan WHERE uid IN (SELECT uid FROM #friends)",
"pages":"SELECT page_id, name FROM page WHERE page_id IN (SELECT page_id FROM #friendsLikes)"
}
You can test this in http://developers.facebook.com/tools/explorer/
You are going to get three result sets one after another, list of friends, list of all of their likes together and then list of page names.
(Note that the query may take a while to execute, so you may want to put a LIMIT on the first line. After uid1 = me() put a LIMIT 5 for example, for testing...)
Then include it in your code in whatever language/platform you are using, for example on iOS:
http://developers.facebook.com/docs/howtos/run-fql-queries-ios-sdk/
You can also do the same query using the Graph API:
https://graph.facebook.com/me/friends?fields=name,id,likes.limit(100).fields(id,name)&limit=100
This will solve the issue of the grouping.

Get posts from user to group with FQL

I am trying to build an FQL query that returns post_id's from a specific user to a Facebook group's stream. I can query all posts and iterate through them and pull out what I need, but for a group with a lot of posts, it's not feasible.
The FQL docs lead me to believe I need to use a filter_key in the WHERE clause because actor_id is not indexable. I can't figure out how to use filter_key to get the results I am looking for.
SELECT post_id FROM stream WHERE target_id='a group_id' AND actor_id='a fb_id'
Any help is appreciated!
Here is the query you need:
SELECT post_id FROM stream WHERE source_id=GROUP_ID and actor_id=USER_ID
For this, since you're looking for posts in a group, the GROUP_ID is your source_id. That is an indexable field. Once you have one indexable field in your WHERE clause, you can add additonal fields to it without a problem.
Also, since GROUP_ID and USER_ID are integers, they don't need to be quoted in FQL.

FQL query on stream table returns empty using created_time

I have a FQL query for the stream table as follows
SELECT post_id FROM stream WHERE filter_key ='nf' AND created_time < 1335500000
Where the filter_key grabs the news feed. This query returns no data (for my account)
Where as, this query (using unix set at April 30 2012)
SELECT post_id FROM stream WHERE filter_key ='nf' AND created_time < 1335758400
Returns data.
I can keep pushing the unix time back to a point until it just stops showing data, yet there is data there from just browsing my news feed manually through the Facebook UI.
The stream docs state
Each query of the stream table is limited to the previous 30 days or 50 posts, whichever is greater, however you can use time-specific fields such as created_time along with FQL operators (such as < or >) to retrieve a much greater range of posts.
So to me, it means I can do "pagination" with created_time and limit.
This isn't the case.
Even using the alternative /me/home returns the first set but when I use the pagination links there is no data returned.
Is there something I missing here? Maybe some explanation as to why the previous data is empty for both the FQL and Graph API calls?

FQL query: query with multiple stream_id gives too few records

I would like to query public posts from multiple Pages.
My FQL query looks like this:
SELECT post_id, source_id FROM stream WHERE source_id in (157528807603192, 127531603328)
This query returns only about 6 records.
However, if I use the two source_id in two separate query I got more than 20 item for both of them:
SELECT post_id, source_id FROM stream WHERE source_id in = 157528807603192
I couldn't find anything in the documentation stating that if you query multiple source_id you need different permissions.
Can anyone explain me what is happening here?
I've found the answer in the documentation. :(
http://developers.facebook.com/docs/reference/rest/stream.get/
"If you specify only one user ID in the source_ids array, you can
return, at minimum, the last 50 posts from that user's profile stream
(Wall) for the last 180 days (it likely can be more).
If you specify more than one user ID in the source_ids array, you can
return posts in those streams only from the last few days (about one
week)."