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



我正试图找到一种方法来获得一个google+帖子的"股份"计数,给定该帖子的url。

我已经通过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.0n   }n  },n  "abtk": "AEIZW7Sct6yKBGo7SA4ZRVvfJerD/H1RhuV/6YxCYfQC6HfEId6oDE8z43pCF4BPmRuxktNaxNSj"n }n}n" 

我已经尝试发送散列'mh'与不同的值为方法参数,但每个返回未找到。我尝试的各种值是:

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

有没有人能找到一种方法来获得股票计数?

你正在使用的方法是一个私有/内部的谷歌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

迄今为止我发现的唯一其他方法是通过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

是当前正确的共享计数。

但是我想看到一个更好的方法来得到这个计数…

感谢abraham,我可以找出ruby的最终解决方案。这里是为那些努力获得这些数据的人准备的:

首先,获得重定向url,以克服与多个google + url格式相关的问题。plus.google.com/u/0/+ AccntName/文章/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

接下来,从url中提取用户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

相关内容

  • 没有找到相关文章

最新更新