23

我正在尝试从 github 为任何用户提取以下信息。

在此处输入图像描述

github-api中是否有公开的方式/api ,我们可以直接获取这些信息?

4

8 回答 8

21

2019 年的答案,使用GitHub API V4

先去GitHub申请token:https ://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line 。第 7 步,范围仅选择read:user

卷曲

curl -H "Authorization: bearer token" -X POST -d '{"query":"query {\n  user(login: \"MeiK2333\") {\n    name\n    contributionsCollection {\n      contributionCalendar {\n        colors\n        totalContributions\n        weeks {\n          contributionDays {\n            color\n            contributionCount\n            date\n            weekday\n          }\n          firstDay\n        }\n      }\n    }\n  }\n}"}' https://api.github.com/graphql

JavaScript

async function getContributions(token, username) {
    const headers = {
        'Authorization': `bearer ${token}`,
    }
    const body = {
        "query": `query {
            user(login: "${username}") {
              name
              contributionsCollection {
                contributionCalendar {
                  colors
                  totalContributions
                  weeks {
                    contributionDays {
                      color
                      contributionCount
                      date
                      weekday
                    }
                    firstDay
                  }
                }
              }
            }
          }`
    }
    const response = await fetch('https://api.github.com/graphql', { method: 'POST', body: JSON.stringify(body), headers: headers })
    const data = await response.json()
    return data
}

const data = await getContributions('token', 'MeiK2333')
console.log(data)
于 2019-11-26T02:48:34.693 回答
7

是的,您可以使用新的 graphql API 轻松做到这一点

查看资源管理器:https ://developer.github.com/v4/explorer/

在那里您可以看到作为用户边缘的贡献集合。您可以获得重建日历所需的所有信息。

我已经包含了一个完整的示例,资源管理器文档可以进一步指导您。

专门回答您的问题,这query.user.contributionsCollection.contributionsCalendar.totalContributions 就是您要寻找的

继续将以下内容复制/粘贴到资源管理器中,您将看到我去年的贡献历史

query { 
  user(login: "qhenkart") {
    email
    createdAt
    contributionsCollection(from: "2019-09-28T23:05:23Z", to: "2020-09-28T23:05:23Z") {
      contributionCalendar {
        totalContributions
        weeks {
          contributionDays {
            weekday
            date 
            contributionCount 
            color
          }
        }
        months  {
          name
            year
            firstDay 
          totalWeeks 
          
        }
      }
    }
  }
  
}
于 2020-10-23T16:11:29.587 回答
3

You can use the github events api for that:

Example (node.js)

const got = require('got')

async function getEvents(username) {
  const events = []
  let page = 1

  do {
    const url = `https://api.github.com/users/${username}/events?page=${page}`
    var { body } = await got(url, {
      json: true
    })
    page++
    events.push(...body)
  } while(!body.length)

  return events
}

(async () => {
  const events = await getEvents('handtrix')

  console.log('Overall Events', events.length)
  console.log('PullRequests', events.filter(event => event.type === 'PullRequestEvent').length)
  console.log('Forks', events.filter(event => event.type === 'ForkEvent').length)
  console.log('Issues', events.filter(event => event.type === 'IssuesEvent').length)
  console.log('Reviews', events.filter(event => event.type === 'PullRequestReviewEvent').length)
})()

Example (javascript)

async function getEvents(username) {
  const events = []
  let page = 1

  do {
    const url = `https://api.github.com/users/${username}/events?page=${page}`
    var body = await fetch(url).then(res => res.json())
    page++
    events.push(...body)
  } while(!body.length)

  return events
}

(async () => {
  const events = await getEvents('handtrix')

  console.log('Overall Events', events.length)
  console.log('PullRequests', events.filter(event => event.type === 'PullRequestEvent').length)
  console.log('Forks', events.filter(event => event.type === 'ForkEvent').length)
  console.log('Issues', events.filter(event => event.type === 'IssuesEvent').length)
  console.log('Reviews', events.filter(event => event.type === 'PullRequestReviewEvent').length)
})()

Documentation

于 2019-02-04T14:33:35.607 回答
3

https://github.com/users/<USER>/contributions您可以使用toURL 参数获取 svg 日历,例如:

https://github.com/users/bertrandmartel/contributions?to=2016-12-31

您可以使用基本的 xml 解析器来汇总来自 svg 的所有贡献。

的示例:

curl -s "https://github.com/users/bertrandmartel/contributions?to=2016-12-31" | \
     xmlstarlet sel -t -v "sum(//svg/g/g/rect/@data-count)"
于 2018-05-15T00:27:23.713 回答
2

要加载包含所有贡献的 svg,您可以在 html 页面中使用此代码

<img src="https://ghchart.rshah.org/username" alt="Name Your Github chart">

在此处输入图像描述

要自定义颜色,您可以这样做

 <img src="https://ghchart.rshah.org/HEXCOLORCODE/username" alt="Name Your Github chart">

十六进制代码 = 17A2B8 在此处输入图像描述

于 2021-03-12T12:25:57.707 回答
1

我相信您可以在 Code Climate 的 Velocity git 分析中看到某个时间范围内的贡献计数以及其他个人贡献分析,您可以在此处请求访问:https ://go.codeclimate.com/velocity-free-for-teams

于 2020-05-09T05:11:17.603 回答
0

您可以使用此函数提取去年(客户)的贡献:

function getContributions(){
    const svgGraph = document.getElementsByClassName('js-calendar-graph')[0];
    const daysRects = svgGraph.getElementsByClassName('day');
    const days = [];

    for (let d of daysRects){
        days.push({
           date: d.getAttribute('data-date'),
           count: d.getAttribute('data-count')
        }); 
    }

    return days;    
}

我还编写了一个小节点模块,可以“提取”贡献
@simonwep/github-contributions

也许这会对你有所帮助(即使我迟到了 4 年)

于 2018-05-14T19:33:27.783 回答
0

如果你更喜欢一个npm包,你可以试试这个。 https://github.com/SammyRobensParadise/github-contributions-counter#readme

您可以获得所有时间的捐款或每年的捐款。

于 2021-11-16T15:36:19.813 回答