2014-10-31 77 views
2

我嘗試在GitHub中獲取組織中的所有用戶。我可以獲得用戶,但是我有分頁問題 - 我不知道我必須有多少頁才能解決。如何在GitHub中獲取組織中的所有用戶?

curl -i -s -u "user:pass" 'https://api.github.com/orgs/:org/members?page=1&per_page=100' 

當然我可以遍歷所有的頁面,直到我的要求將不會返回「0」,但我覺得這不是個好主意)

也許GitHub上有標準方法獲得組織中的所有用戶?

+0

https://developer.github.com/guides/traversing-with-pagination/#consuming-the-information。 – 2014-10-31 11:28:40

回答

0

Traversing with Pagination,應該有一個Link response header,如:

Link: <https://api.github.com/orgs/:org/members?page=2>; rel="next", <https://api.github.com/orgs/:org/members?page=3>; rel="last" 

這些標題應該給你需要這樣您就可以繼續獲得頁面的所有信息。

由於性能方面的原因,我認爲任何API都不能繞過分頁。

+0

謝謝Jean,就是這樣! 我知道關於「鏈接迴應標題」,但我測試這是我的測試組織,其中只包含10人,當然我沒有看到這一行(鏈接:...)... 當我嘗試在另一個組織,我明白了! 謝謝! – 2014-10-31 12:06:05

0

這裏是我的github-users腳本實例:

#!/usr/bin/env ruby 

require 'octokit' 

Octokit.configure do |c| 
    c.login = '....' 
    c.password = '...' 
end 

get = Octokit.org(ARGV.first).rels[:public_members].get 
members = get.data 
urls = members.map(&:url) 

while members.size > 0 
    next_url = get.rels[:next] 
    next members = [] unless next_url 
    get = next_url.get 
    members = get.data 
    urls << members.map(&:url) 
end 

puts urls 

例如github-members stackexchange給出:

https://api.github.com/users/JasonPunyon 
https://api.github.com/users/JonHMChan 
https://api.github.com/users/NickCraver 
https://api.github.com/users/NickLarsen 
https://api.github.com/users/PeterGrace 
https://api.github.com/users/bretcope 
https://api.github.com/users/captncraig 
https://api.github.com/users/df07 
https://api.github.com/users/dixon 
https://api.github.com/users/gdalgas 
https://api.github.com/users/haneytron 
https://api.github.com/users/jc4p 
https://api.github.com/users/kevin-montrose 
https://api.github.com/users/kirtithorat 
https://api.github.com/users/kylebrandt 
https://api.github.com/users/momow 
https://api.github.com/users/ocoster-se 
https://api.github.com/users/robertaarcoverde 
https://api.github.com/users/rossipedia 
https://api.github.com/users/shanemadden 
https://api.github.com/users/sklivvz 
相關問題