Skip to content

Instantly share code, notes, and snippets.

@zthxxx
Last active June 25, 2019 08:29
Show Gist options
  • Save zthxxx/b60d408187c390b7a9e3ef5d9c1385ff to your computer and use it in GitHub Desktop.
Save zthxxx/b60d408187c390b7a9e3ef5d9c1385ff to your computer and use it in GitHub Desktop.
count a GitHub user's stars and contributions
const axios = require('axios')
const dayjs = require('dayjs')
const cheerio = require('cheerio')
const gainStarsSum = async user => {
const res = await axios.get(`https://api.github.com/users/${user}/repos`)
const repos = res.data
const starsSum = repos.map(repo => repo.stargazers_count).reduce((a, b) => a +b)
console.log(`User ${user} gain ${starsSum} stars.`)
return starsSum
}
const getContributions = async user => {
const res = await axios.get(`https://api.github.com/users/${user}/contributions`)
const contributionsSvg = res.data
const dom = cheerio.load(contributionsSvg)
const days = dom('rect.day[data-date]').toArray()
const dateCount = days
.map(day => ({ [day.attribs['data-date']]: +day.attribs['data-count'] }))
.reduce((dateCount, day) => ({ ...dateCount, ...day }), {})
return dateCount
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment