How do you get total contributions with Githubs API v4

依然范特西╮ 提交于 2019-12-03 05:45:37

There is no API for this as such. So there are two ways to go about it. Simple data scraping the user url or looping through each repo user has forked and then count the contribution. The later one will be more time consuming. The first one is much more reliable as it is cached by github. Below is a python approach to fetch the same

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-contribution-graph'}).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)

PS: Taken from https://github.com/garnertb/github-contributions

For later approach there is a npm package

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

But I would recommend using the scraping approach only

The ContributionsCollection object provides total contributions for each contribution type between two dates.

Note: from and to can be a maximum of one year apart, for a longer timeframe make multiple requests.

query ContributionsView($username: String!, $from: DateTime!, $to: DateTime!) {
  user(login: $username) {
    contributionsCollection(from: $from, to: $to) {
      totalCommitContributions
      totalIssueContributions
      totalPullRequestContributions
      totalPullRequestReviewContributions
    }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!