-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstarz.js
executable file
·86 lines (81 loc) · 2.32 KB
/
starz.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
var c = require('colors'),
https = require('https'),
opts = require('optimist')
.usage('Count a GitHub user\'s total stars.')
.demand(1)
.alias('t', 'thresh')
.alias('l', 'limit')
.alias('a', 'auth')
.default('t', 1)
.default('l', Infinity)
.describe({
'a': 'GitHub username:password for rate limits',
't': 'Only show repos above this threshold',
'l': 'Only show this many repos'
})
.argv,
user = opts._[0]
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) {
var reqOpts = {
hostname: 'api.github.com',
path: url,
headers: {'User-Agent': 'GitHub StarCounter'},
auth: opts.auth || undefined
}
https.request(reqOpts, 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('\nTotal: ' + total.toString().green + '\n')
console.log(list.map(function (r) {
return r.name +
new Array(longest - r.name.length + 4).join(' ') +
'\u2605 '.yellow +
r.stargazers_count
}).join('\n'))
console.log()
}