rubygoogle-apigoogle-plustyphoeus

How to get the google+ post share count, not the plus one count


I am trying to find a way to get the "shares" count for a google+ post, given the url of the post.

I have searched through stackoverflow and found only the pos.plusone.get method that gets the plus one counts, not the shares:

url = "https://plus.google.com/+JohnBattelle/posts/bpxzZb3z5qt"    
mh = { method: "pos.plusones.get", id: "p", params: {nolog: true, id: url, source: "widget", userId: "@viewer", groupId: "@self"}, jsonrpc: "2.0", key: "p", apiVersion: "v1"} 
r = Typhoeus::Request.new("https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ", method: :post, body: mh.to_json, headers: {"Accept" => "application/json", "Content-type" => "application/json" } )

x = r.run
x.body 

Returns:

"{\n \"id\": \"p\",\n \"result\": {\n  \"kind\": \"pos#plusones\",\n  \"id\": \"https://plus.google.com/+JohnBattelle/posts/bpxzZb3z5qt\",\n  \"isSetByViewer\": false,\n  \"metadata\": {\n   \"type\": \"URL\",\n   \"globalCounts\": {\n    \"count\": 58.0\n   }\n  },\n  \"abtk\": \"AEIZW7Sct6yKBGo7SA4ZRVvfJerD/H1RhuV/6YxCYfQC6HfEId6oDE8z43pCF4BPmRuxktNaxNSj\"\n }\n}\n" 

I have tried sending the hash 'mh' with different values for method parameter, but each returns Not Found. The various values I tried are:

pos.plus_shares.get
pos.shares.get
pos.plus_share.get
pos.public.shares.get

Has anyone been able to find a way to get the shares counts?


Solution

  • Thanks to abraham I could figure out the final solution in ruby. Here it is for anyone struggling to get this data:

    First, get the redirected url, to overcome the issue related to multiple google plus url formats eg. plus.google.com/u/0/+AccntName/posts/ou2342

    def self.get_redirected_url(url)
      url_parse = URI.parse(url)
      http_response = Net::HTTP.start(url_parse.host, url_parse.port, :use_ssl => url_parse.scheme == 'https') {|http| http.request Net::HTTP::Get.new url_parse.request_uri }
      return url if http_response['location'].nil?
      http_response['location']
    end
    

    Next, extract the user id and post path from the url, and start using the GooglePlus gem to get the job done:

    attributes = { plusone_count: 0 , share_count: 0, comment_count: 0 }
    redirected_url = URI.parse(get_redirected_url(url))
    user_id = redirected_url.request_uri.match(/\+*\w+/).to_s
    post_path = redirected_url.request_uri.match(/posts\/\w+/).to_s
    person = GooglePlus::Person.get(user_id)
    return attributes if person.nil?
    cursor = person.list_activities
    cursor.each do |item|
      item_url = item.attributes['url']
      if item_url.match(post_path)
        activity_id = item.attributes['id']
        activity = GooglePlus::Activity.get(activity_id)
        attributes[:plusone_count] = activity.attributes['object']['plusoners'['totalItems']
        attributes[:share_count] = activity.attributes['object']['resharers'['totalItems']
        attributes[:comment_count] = activity.attributes['object']['replies'['totalItems']
        break
      end
    end