Last active
June 25, 2019 08:29
-
-
Save zthxxx/b60d408187c390b7a9e3ef5d9c1385ff to your computer and use it in GitHub Desktop.
count a GitHub user's stars and contributions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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