你如何通过Githubs API v4获得总贡献

Jos*_*ärd 14 api github github-api apollo graphql

我一直在浏览Github V4 API文档,我似乎无法找到查询年度总贡献的方法(如github配置文件中所示).有没有人设法使用新API从您的个人资料中获取一些统计信息?

我在github上使用graphQL和个人访问令牌,并设法获得最小的用户配置文件数据; 用户名,档案名称等

Bar*_*row 8

ContributionsCollection对象提供两个日期之间每种贡献类型的总贡献

注意:from最多to可以相隔一年,对于更长的时间范围,请提出多个请求。

query ContributionsView($username: String!, $from: DateTime!, $to: DateTime!) {
  user(login: $username) {
    contributionsCollection(from: $from, to: $to) {
      totalCommitContributions
      totalIssueContributions
      totalPullRequestContributions
      totalPullRequestReviewContributions
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Tar*_*ani 4

没有这样的 API。所以有两种方法可以解决这个问题。简单的数据抓取用户 URL 或循环遍历每个用户已分叉的存储库,然后计算贡献。后者会更耗时。第一个更可靠,因为它是由 github 缓存的。下面是一个Python方法来获取相同的内容

import json
import requests
from bs4 import BeautifulSoup

GITHUB_URL = 'https://github.com/'


def get_contributions(usernames):
    """
    Get a github user's public contributions.
    :param usernames: A string or sequence of github usernames.
    """
    contributions = {'users': [], 'total': 0}

    if isinstance(usernames, str) or isinstance(usernames, unicode):
        usernames = [usernames]

    for username in usernames:
        response = requests.get('{0}{1}'.format(GITHUB_URL, username))

        if not response.ok:
            contributions['users'].append({username: dict(total=0)})
            continue

        bs = BeautifulSoup(response.content, "html.parser")
        total = bs.find('div', {'class': 'js-yearly-contributions'}).findNext('h2')
        contributions['users'].append({username: dict(total=int(total.text.split()[0].replace(',', '')))})
        contributions['total'] += int(total.text.split()[0].replace(',', ''))

    return json.dumps(contributions, indent=4)
Run Code Online (Sandbox Code Playgroud)

PS:取自https://github.com/garnertb/github-contributions

对于后面的方法,有一个 npm 包

https://www.npmjs.com/package/github-user-contributions

但我建议仅使用抓取方法