2017-02-26 64 views
0

其實在github上最新的承諾的項目,我有兩個想法,讓提交的總數,如何獲得總數和利用API

一個想法是使用API​​,如:https://api.github.com/repos/mojombo/grit/commits,這樣可以得到所有提交,我們可以計算它得到總數和提交的歷時日期。但主要的問題是提交次數太多,速度會變慢。

另一個想法是,因爲我已經得到了使用API​​ https://api.github.com/repos/mojombo/grit/contributors的貢獻者的統計數據,並且貢獻者的總數與我在grit website中看到的數量相同,並且我從這個API調用中獲得了貢獻每個貢獻者的總和,然後我總結所有貢獻者的貢獻,它應該與網站的價值相同,但不幸的是,它與513不同。代碼如下,我可以知道爲什麼有差異嗎?

import json, requests 
all_contributors = list() 
page_count = 1 
total_contributions=0 
while True: 
    contributors = requests.get("https://api.github.com/repos/mojombo/grit/contributors?page=%d"%page_count) 
    if contributors != None and contributors.status_code == 200 and len(contributors.json()) > 0: 
     all_contributors = all_contributors + contributors.json() 
    else: 
     break 
    page_count = page_count + 1 
total_contributor=len(all_contributors) 
for contr in all_contributors: 
    total_contributions=total_contributions+contr['contributions'] 
print("--------total contributor-----------%d" %total_contributor) //print 43 
print("--------total commits-----------%d" %total_contributions) //print 497 

感謝

+0

我已經知道如何獲取最新的提交:) – sweetyBaby

回答

1

我覺得你是缺少一些匿名提交。要包括匿名貢獻者,您可以將您的代碼更改爲 - contributors = requests.get("https://api.github.com/repos/mojombo/grit/contributors?anon=1&page=%d"%page_count)