I have been working with the Facebook API recently and I came across the need to get all the photos and videos in the news feed. Given the comprehensive Graph API, I had initially thought that it would be straight forward getting these details from the news feed. After struggling a bit with the available API methods, I decided that FQL might be the best way to go about this. And it turned out that doing this with FQL wasn’t all that difficult. Here’s a simple FQL query for getting all the photos and videos in the news feed. stream_filter
contains the filters for filtering the stream of posts. This contains several filters that include the lists that the user has built and some default filters like the ones for photos and videos.
SELECT actor_id, created_time, likes, post_id, attachment
FROM stream
WHERE filter_key IN (
SELECT filter_key FROM stream_filter WHERE uid = me() AND (name = "Photos" OR name = "Video")
)
You can have a look at the results of the query using the Graph API Explorer. This gives us the actor_id which is the id of the user/page on Facebook that has shared the post, the likes on the post, the id for the post and an attachment that contains more details about the media objects included in the posts. In order to get the details for the users who have shared the posts, we need to use multiple fql queries to query the profile table for user details.
{
media: 'SELECT actor_id, created_time, likes, post_id, attachment FROM stream WHERE filter_key IN ( SELECT filter_key FROM stream_filter WHERE uid = me() AND (name = "Photos" OR name = "Video"))',
users: 'SELECT id, name, url, pic FROM profile WHERE id IN (SELECT actor_id FROM #media)'
}
I will describe in the next post how we can parse the results from these queries to build a simple screen in Android with the results that allow users to browse through these photos and videos.