簡體   English   中英

如何獲取google +帖子分享次數,而不是加一次數

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

考慮到帖子的網址,我正在嘗試找到一種獲取Google+帖子的“份額”計數的方法。

我搜索了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”,但每個返回未找到 我嘗試的各種值是:

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

有沒有人能夠找到一種獲取股數的方法?

您使用的方法是私有/內部Google API。 它可能會更改,恕不另行通知,並破壞您的代碼。

支持的方法是activity.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

到目前為止,這是正確的共享計數。

但我希望看到一種更好的方法來獲得此計數。

多虧了亞伯拉罕,我才得以找到紅寶石的最終解決方案。 這是任何想要獲取此數據的人的方法:

首先,獲取重定向的網址,以解決與多種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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM