获取所有公共用户贡献,也称“日历数据”(GitHub API V3)。

4

我发现很多帖子 - 甚至有一些使用除github api以外的api嵌入贡献日历的项目 - 然而这些方法或帖子都没有真正回答问题。 一个接近 但是不行。

我只是想访问用户在github个人资料页面上显示的所有贡献总数,如下所示...

enter image description here

API文档描述了收集Repo Contrib数据,因此我已经尝试盲目猜测api,但没有成功。 有人知道是否实际上有一个可行的端点用于此数据吗? 我真的需要自己计算这些信息还是进行一些肮脏的html抓取非常愚蠢? 有人知道吗?
更新: 以下是使用cheerio和regex的解决方案,供任何寻找快速网络爬虫解决方案的人使用
const axios = require('axios')
const cheerio = require('cheerio')
const url = 'https://github.com/archae0pteryx'

function getCommits(url) {
    return new Promise((resolve, reject) => {
        axios.get(url).then(res => {
            const load = cheerio.load(res.data)
            const parsed = load('div.js-contribution-graph > h2').text()
            const reg = /\d+/g
            const x = parsed.match(reg)
            resolve(x)
        }).catch(err => reject(err))
    })
}


getCommits(url)
    .then(x => console.log(x))
    .catch(err => console.error(err))
1个回答

3

使用 GraphQL v4

如果可以使用 GraphQL API,您可以使用 contributionsConnection 获取特定时间段内的贡献总数:

{
  viewer {
    contributionsCollection(from: "2020-01-01T00:00:00", to: "2020-12-01T00:00:00") {
      contributionCalendar {
        totalContributions
      }
    }
  }
}

输出:

{
  "data": {
    "viewer": {
      "contributionsCollection": {
        "contributionCalendar": {
          "totalContributions": 118
        }
      }
    }
  }
}

解析日历 SVG

您可以使用xmlstarlet来解析从 https://github.com/users/archae0pteryx/contributions 获取的日历 SVG 的 XML 文件。

这比爬取 GitHub 网站更好,但仍无法使用官方 API:

curl -s "https://github.com/users/archae0pteryx/contributions" | \
     xmlstarlet sel -t -v "sum(//svg/g/g/rect/@data-count)"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接