rimraf
rimraf

Reputation: 4126

Get all public User contributions aka "calendar data" (github api v3)

I have found many threads - even a couple projects which embed the contribution calendar using api's other than the github api - yet none of these methods or threads actually answer the question. One comes close but it's a no go.

I am simply trying to access the total number of contributions for a user as shown in your calendar on the github profile page as shown below...

enter image description here

The api docs describe collecting Repo Contrib data so i've tried blindly poking at the api with educated guesses to no avail. Does anyone per-chance know if there in fact is a viable endpoint for this data? Do I really need to calculate this info myself or do some dirty html scraping nonsense? This seems dumb... Anyone?

UPDATE: Here's a solution using cheerio and regex for anyone looking for a quick web scraping solution

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

Upvotes: 6

Views: 1492

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45473

Using GraphQL v4

If using graphql API is an option, you can use the contributionsConnection to get the total number of contributions between a specific period of time :

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

output :

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

Parsing the calendar svg

You can use xmlstarlet to parse the XML file you get from your calendar svg : https://github.com/users/archae0pteryx/contributions.

It's better than scraping GitHub website but still fail to use the official API :

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

Upvotes: 3

Related Questions