koala get likes on post
Asked Answered
A

2

6

i am currently using the koala, and all seems to be working, though when attempting to use the following to gain the likes on a certain posts all i seem to be getting is the array of items

code within application helper

    def facebook
    @facebook ||= Koala::Facebook::API.new(current_user.oauth_token)
    block_given? ? yield(@facebook) : @facebook
  rescue Koala::Facebook::APIError
    logger.info e.to_s
    nil
  end

def likes_count obj
    facebook.get_object(obj, :fields => "likes.summary(true)")
  end

code within view

=likes_count(feed['id'])

results returned

{"id"=>"846011512095715", "updated_time"=>"2014-06-22T11:11:45+0000", "likes"=>{"data"=>[{"id"=>"10152444475716893", "name"=>"Tahlia Fulton"}, {"id"=>"10152240895519022", "name"=>"Tim Raftery"}, {"id"=>"481256765338477", "name"=>"Gabby Taylor"}, {"id"=>"664803753573900", "name"=>"Harriet Ochsenbein"}, {"id"=>"10152453604228810", "name"=>"Kelly Jenkinson"}, {"id"=>"10152145864189249", "name"=>"David Glazzard"}, {"id"=>"10203193488711772", "name"=>"Bianca Love"}, {"id"=>"10152567265688833", "name"=>"Clare Duncan"}, {"id"=>"105513176145556", "name"=>"Frankston Hockey Club"}], "paging"=>{"cursors"=>{"after"=>"MTA1NTEzMTc2MTQ1NTU2", "before"=>"MTAxNTI0NDQ0NzU3MTY4OTM="}}, "summary"=>{"total_count"=>9}}}
Annotate answered 22/6, 2014 at 14:19 Comment(0)
B
9

Likes on a post:

likes = @graph.get_object('post_id', :fields => "likes.summary(true)")["likes"]["summary"]["total_count"]

In case anyone comes across this and also happens to be looking for the shares and comments count:

Shares on a post:

shares = @graph.get_object('post_id', :fields => "shares")["shares"]["count"]

Comments on a post:

comments = @graph.get_object('post_id', :fields => "comments.summary(true)")["comments"]["summary"]["total_count"]

Or if you prefer a hash of all three:

post_kpis = @graph.get_connections(@post, 'insights', metric: 'post_storytellers_by_action_type').first["values"].first["value"]

To get all the insights on your post:

post_insights = @graph.get_connections(@post, 'insights')
Blockish answered 5/7, 2014 at 7:52 Comment(1)
Hash of all 3, and insights doesn't seem to work anymore. Can get the same results with: graph.get_object(post_id, :fields => "shares,likes.summary(true),comments.summary(true)")Demount
N
0

Try @graph.get_connections(post_id, "likes") to expose a Koala::Facebook::API::GraphCollection object on which you can call .next_page to traverse the entire list of likes.

Nepos answered 26/8, 2014 at 23:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.