【问题标题】:Get all public User contributions aka "calendar data" (github api v3)获取所有公共用户贡献,即“日历数据”(github api v3)
【发布时间】:2017-12-14 17:46:18
【问题描述】:

我发现了很多线程——甚至有几个项目使用 github api 以外的 api 嵌入了贡献日历——但这些方法或线程都没有真正回答这个问题。 One comes close 但这是不行的。

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

api 文档描述了收集 Repo Contrib 数据,所以我尝试盲目地用有根据的猜测来戳 api,但无济于事。有没有人知道这些数据实际上是否有一个可行的端点?我真的需要自己计算这些信息还是做一些肮脏的 html 抓取废话?这似乎很愚蠢......有人吗?

更新: 这是一个使用cheerio 和正则表达式的解决方案,适用于任何寻求快速网络抓取解决方案的人

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))

【问题讨论】:

    标签: curl github github-api


    【解决方案1】:

    使用 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

    您可以使用 xmlstartlet 解析从日历 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)"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 2017-04-03
      • 1970-01-01
      • 2013-08-18
      • 2015-07-17
      • 1970-01-01
      • 2015-08-30
      相关资源
      最近更新 更多