如何获得谷歌+帖子共享计数,而不是加一计数

时间:2014-07-18 21:56:22

标签: ruby google-api google-plus typhoeus

我正试图找到一种方法来获得"股票"根据帖子的网址计算谷歌+帖子。

我搜索了stackoverflow,发现只有pos.plusone.get方法得到加号,而不是共享:

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 

返回:

"{\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" 

我尝试使用方法参数的不同值发送哈希' mh' ,但每个都返回 Not Found 。我尝试的各种价值观是:

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

有没有人能够找到获得股票数量的方法?

3 个答案:

答案 0 :(得分:1)

您使用的方法是私有/内部Google API。它可能会在没有通知的情况下发生变化并破坏您的代码。

支持的方法是activities.get。 (我并不熟悉Ruby所以代码可能是错误的。

activitieId = "z13pxxpyyovky5ayk04cibwrzqbdcr3abtc0k"
apiKey = "AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ"
r = Typhoeus::Request.new("https://www.googleapis.com/plus/v1/activities/" + activityId + "?key=" + apiKey } )
x = r.run
activity = JSON.parse(x.body)
resharers = activity.object.resharers.totalItems

答案 1 :(得分:0)

到目前为止,我发现的唯一另一种方式是通过Nokogiri,在rails控制台中尝试了以下内容:

url = "https://plus.google.com/+JohnBattelle/posts/bpxzZb3z5qt"
file = open(url)
doc = Nokogiri::HTML(file)
puts doc.xpath("//div[@jscontroller='tH7URd']").first.children.xpath("//span[@class='MM jI']").first.children

返回:

4

这是目前正确的共享计数。

但我希望看到一个更好的方法来计算......

答案 2 :(得分:0)

感谢亚伯拉罕,我可以找出红宝石的最终解决方案。在这里,对于那些努力获取这些数据的人来说:

首先,获取重定向的网址,以克服与多个Google +网址格式相关的问题,例如。 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

接下来,从网址中提取用户ID和发布路径,然后开始使用GooglePlus gem来完成工作:

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
相关问题