0

I want to a build a website that show the user all videos that his friends published on facebook, I know how to pull the news feed for recent posts, but I don't know how to pull all the posts for specific day (all the posts from the start of the day).

I use this FQL call to pull the recent videos:

SELECT post_id, source_id, actor_id, target_id, message, attachment, permalink, type FROM stream 
WHERE source_id IN (SELECT target_id FROM connection WHERE source_id=me() AND is_following=1) 
AND is_hidden = 0 
AND type = 80 
AND strpos(attachment.href, "youtu") >= 0

but I don't know how to pull all the videos from the start of the day to it's ending...

Edit:

Now I have in my code as your answer:

@feed = Koala::Facebook::API.new(current_user.token)
to = Time.now.to_i
yest = 1.day.ago.to_i
@feed.fql_query("SELECT post_id, actor_id, target_id, message, likes FROM stream WHERE source_id = me() AND created_time > #{yest} AND created_time < #{to} AND type = 80 
AND strpos(attachment.href, "youtu") >= 0")

and the feed variable returns me # and not a json, how can I get a json from this variable?

Matteo Alessani
  • 10,264
  • 4
  • 40
  • 57
gal
  • 929
  • 4
  • 21
  • 39

1 Answers1

3

Using FQL is probably your best bet. You will need the read_stream permission from the user.

You can use the Koala gem and run the following:

require 'koala'
@graph = Koala::Facebook::API.new("YOUR_ACCESS_TOKEN")
@graph.fql_query("SELECT post_id, actor_id, target_id, message FROM stream WHERE source_id = me() AND created_time > START_TIME AND created_time < END_TIME LIMIT 10")

Both START_TIME and END_TIME should be unix timestamps. In your case, representing the start and end of the day.

Gazler
  • 83,029
  • 18
  • 279
  • 245
  • Hi, 1. How can I generate unix timestamps using rails? 2. if I want to add this "AND type = 80 AND strpos(attachment.href, "youtu") >= 0" I just adding this line to the end of the FQL query? – gal Apr 04 '12 at 20:01
  • @gal You can just add your conditions to the end of the FQL. To generate a Unix timestamp, you can call `to_i` method of the time class. (`Time.now.to_i`) Also http://stackoverflow.com/questions/1805950/ruby-rails-converting-a-date-to-a-unix-timestamp – Gazler Apr 04 '12 at 20:03
  • Thanks man!, and 1 more little thing, how can I sort all the posts by there likes? – gal Apr 04 '12 at 20:14
  • I'm afraid you can't do this easily as likes returns an array. You will have to sort it after you have retrieved the data. Make sure to add "likes" to your SELECT params. `sort_by {|l| l["likes"]["count"].to_i }` on your dataset should work. http://developers.facebook.com/docs/reference/fql/stream/ ` – Gazler Apr 04 '12 at 20:21
  • Thanks again my friend!, This is the code I have: @feed = Koala::Facebook::API.new(current_user.token) to = Time.now.to_i yest = 1.day.ago.to_i @feed.fql_query("SELECT post_id, actor_id, target_id, message, likes FROM stream WHERE source_id = me() AND created_time > #{yest} AND created_time < #{to}") and the feed variable returns me # and not a json, how can I get a json from this variable? – gal Apr 04 '12 at 20:26
  • @gal The response comes back as an array for me. If you want to convert an array to json, you can just call `to_json` on it. – Gazler Apr 04 '12 at 20:33