2 notes &
Select album photos likes from Facebook by FQL query
I have Facebook contest album with many photos. And I need to display rating of this photos by user likes.
This FQL query get a list of all likes (user_ids) by all photos:
SELECT user_id, object_id FROM like WHERE object_id IN (SELECT object_id FROM photo WHERE aid=’139307129471375_51887’ limit 5000) limit 100000
Then you can order results by object_id and sort by object_id count.
NOTE! Album ID (‘139307129471375_51887’) - is not facebook graph object id. 139307129471375 - is page id, 51887 - element id for this page.
For my example I use Ruby and fb_graph gem:
query = FbGraph::Query.new(“SELECT user_id, object_id FROM like WHERE object_id IN (SELECT object_id FROM photo WHERE aid=’139307129471375_51887’ limit 5000) limit 100000 “).fetch(SiteConfig.fb_token)
SiteConfig.fb_token - is my facebook app access_token with permissions user_photos (get it easy by using Graph API Explorer)
Then I order and sort my array:
photos = query.inject(Hash.new(0)){ |h, e| h[e[“object_id”]] += 1 ; h }.sort{|a,b| b[1]<=>a[1]}
photos.each {|p| puts “Object id - #{p[0]}, likes count - #{p[1]}”}