如何使用Api获取github中项目的总数和最新提交

时间:2017-02-26 11:38:45

标签: github 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

由于

1 个答案:

答案 0 :(得分:1)

我觉得你错过了一些匿名提交。要包含匿名贡献者,您可以将代码更改为 - contributors = requests.get("https://api.github.com/repos/mojombo/grit/contributors?anon=1&page=%d"%page_count)

相关问题