你如何通过Githubs API v4获得全部贡献 [英] How do you get total contributions with Githubs API v4

查看:196
本文介绍了你如何通过Githubs API v4获得全部贡献的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been looking through the Github V4 API docs and I cannot seem to find a way to query total contributions for the year (as displayed on your github profile). Has anyone managed to use the new API to grab some statistics from your personal profile?

我在github上使用graphQL和个人访问令牌,档案资料;用户名,个人资料名称等。

I am using graphQL and a Personal Access Token on github, and managed to get minimal user profile data; username, profile name etc.

推荐答案

没有这样的API。所以有两种方法可以解决它。简单的数据抓取用户网址或循环通过每个回购用户已分叉,然后计算贡献。后者会耗费更多的时间。第一个更可靠,因为它被github缓存。下面是一个python方法来获取相同的

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:取自 https://github.com/garnertb/github-contributions

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

For later approach there is a npm package

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

但是我会建议只使用scraping方法

But I would recommend using the scraping approach only

这篇关于你如何通过Githubs API v4获得全部贡献的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆