我发现了很多线程——甚至有几个项目使用 github api 之外的 api 嵌入了贡献日历——但这些方法或线程都没有真正回答这个问题。一个接近但这是不行的。
我只是想获取用户的贡献总数,如 github 个人资料页面上的日历所示,如下所示......
![enter image description here](https://i.stack.imgur.com/qwje5.png)
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))
使用 GraphQL v4
如果使用GraphQL API是一个选项,您可以使用贡献连接获取特定时间段内的贡献总数:
{
viewer {
contributionsCollection(from: "2020-01-01T00:00:00", to: "2020-12-01T00:00:00") {
contributionCalendar {
totalContributions
}
}
}
}
output :
{
"data": {
"viewer": {
"contributionsCollection": {
"contributionCalendar": {
"totalContributions": 118
}
}
}
}
}
解析日历 svg
您可以使用xmlstarlet解析从日历 svg 获取的 XML 文件:https://github.com/users/archae0pteryx/contributions.
比抓取 GitHub 网站更好,但仍然无法使用官方 API:
curl -s "https://github.com/users/archae0pteryx/contributions" | \
xmlstarlet sel -t -v "sum(//svg/g/g/rect/@data-count)"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)