Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Count your total stars!
var https = require('https'),
user = process.argv[2],
opts = parseOpts(process.argv.slice(3))
request('/users/' + user, function (res) {
if (!res.public_repos) {
console.log(res.message)
return
}
var pages = Math.ceil(res.public_repos / 100),
i = pages,
repos = []
while (i--) {
request('/users/' + user + '/repos?per_page=100&page=' + (i + 1), check)
}
function check (res) {
repos = repos.concat(res)
pages--
if (!pages) output(repos)
}
})
function request (url, cb) {
https.request({
hostname: 'api.github.com',
path: url,
headers: {'User-Agent': 'GitHub StarCounter'}
}, function (res) {
var body = ''
res
.on('data', function (buf) {
body += buf.toString()
})
.on('end', function () {
cb(JSON.parse(body))
})
}).end()
}
function output (repos) {
var total = 0,
longest = 0,
list = repos
.filter(function (r) {
total += r.stargazers_count
if (r.stargazers_count >= opts.thresh) {
if (r.name.length > longest) {
longest = r.name.length
}
return true
}
})
.sort(function (a, b) {
return b.stargazers_count - a.stargazers_count
})
if (list.length > opts.limit) {
list = list.slice(0, opts.limit)
}
console.log('\nTatal: ' + total + '\n')
console.log(list.map(function (r) {
return r.name +
new Array(longest - r.name.length + 4).join(' ') +
'\u2605 ' +
r.stargazers_count
}).join('\n'))
}
function parseOpts (args) {
var opts = {
thresh: 1,
limit: Infinity
}
args.forEach(function (a, i) {
var next = args[i + 1]
if (a === '-t') {
opts.thresh = parseInt(next, 10) || 1
} else if (a === '-l') {
opts.limit = parseInt(next, 10) || Infinity
}
})
return opts
}
@yyx990803
Copy link
Author

yyx990803 commented Dec 2, 2013

If you hit rate limit you can add auth option in the https.request with your GitHub credentials.

Options (these will not affect total):

  • -l: only list this many repos
  • -t : only list repos with star count above this threshold

Example:

$ node starcounter yyx990803 -t 10

Tatal: 1123

HTML5-Clear      ★  617
HTML5-Clear-v2   ★  302
pod              ★  62
Speech.js        ★  33
creative-html5   ★  26
zoomerang        ★  16
matrix.js        ★  11

@AndersDJohnson
Copy link

AndersDJohnson commented Aug 16, 2015

FYI this is available on GitHub at https://github.com/yyx990803/starz and on npm at https://www.npmjs.com/package/starz.

@ahmadawais
Copy link

ahmadawais commented Jul 6, 2017

Thanks for this one, Evan!

@bearpaw
Copy link

bearpaw commented Sep 1, 2017

Thanks for sharing!

@laixintao
Copy link

laixintao commented Jun 8, 2019

Directly running from command line:

curl https://gist.githubusercontent.com/yyx990803/7745157/raw/dbcf4874d55490f3c2af9d593950964fe48b1e31/starcounter.js -sSL | node - laixintao -t 10

@dragen1860
Copy link

dragen1860 commented Aug 12, 2019

Directly running from command line:

curl https://gist.githubusercontent.com/yyx990803/7745157/raw/dbcf4874d55490f3c2af9d593950964fe48b1e31/starcounter.js -sSL | node - laixintao -t 10

It works. Thank you.

@huan
Copy link

huan commented Nov 5, 2020

Awesome tool, thank you very much for sharing this!

@raviriley
Copy link

raviriley commented Jun 4, 2021

Typo on line 61 - Tatal instead of Total

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment