Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add GitHub authentication to exceed rate-limiting

  • Loading branch information...
commit 299f3fdbafd75641d5505bcc88caf5c0f47717f2 1 parent 696f1f9
@rvagg rvagg authored
View
6 build
@@ -14,8 +14,10 @@ var config = {
, verbose: process.argv.indexOf('-v') != -1
, quiet: process.argv.indexOf('-q') != -1
}
+ , ghAuth = require('./lib/ghauth')
, main = require('./lib/main')
, stats = require('./lib/stats')
-!(process.argv.indexOf('-s') != -1 ? stats : main).create(config).start()
-
+ghAuth(config, function () {
+ (process.argv.indexOf('-s') != -1 ? stats : main).create(config).start()
+})
View
21 lib/ghauth.js
@@ -0,0 +1,21 @@
+var read = require('read')
+ , colors = require('colors')
+
+ , auth = function (config, callback) {
+ console.log('To collect GitHub stats we need you to authenticate so we can exceed API rate-limiting.'.bold.green)
+ console.log('Leave the username field blank to skip stats collection.'.bold.green)
+ console.log()
+
+ read({ prompt: 'GitHub username:'.bold }, function (err, data) {
+ if (err) return callback(err)
+ if (data === '') return callback()
+ config.ghUser = data
+ read({ prompt: 'GitHub password:'.bold, silent: true, replace: '\u2714' }, function (err, data) {
+ if (err) return callback(err)
+ config.ghPass = data
+ callback()
+ })
+ })
+ }
+
+module.exports = auth
View
21 lib/gitstats.js → lib/ghstats.js
@@ -8,6 +8,8 @@ var GitStats = {
processLibrary: function (lib, callback) {
var match
+ if (!this.user)
+ return callback()
if (lib.github)
match = lib.github.match(githubSpecRegex)
if (!match)
@@ -23,7 +25,10 @@ var GitStats = {
, processRepo: function (lib, user, project, callback) {
request(
- GIT_API_REPO_URL.replace('{user}', user).replace('{project}', project)
+ {
+ url: GIT_API_REPO_URL.replace('{user}', user).replace('{project}', project)
+ , headers: { Authorization: "Basic " + new Buffer(this.user + ":" + this.pass).toString('base64') }
+ }
, function (err, response, body) {
if (err)
return callback('Error requesting repo data from GitHub for ' + user + '/' + project + ': ' + err)
@@ -39,7 +44,13 @@ var GitStats = {
//, project: project
})
} else {
- this.emit('warning', lib, 'Looks like a GitHub project but can\'t get data from GitHub API')
+ this.emit(
+ 'warning'
+ , lib
+ , typeof data.message == 'string'
+ ? 'GitHub says: ' + data.message
+ : 'Looks like a GitHub project but can\'t get data from GitHub API'
+ )
callback()
}
}.bind(this)
@@ -49,8 +60,10 @@ var GitStats = {
GitStats.__proto__ = events.EventEmitter.prototype
-module.exports.create = function () {
+module.exports.create = function (user, pass) {
var gitStats = Object.create(GitStats)
+ gitStats.user = user
+ gitStats.pass = pass
events.EventEmitter.call(gitStats)
return gitStats
-}
+}
View
6 lib/main.js
@@ -47,7 +47,7 @@ var Main = {
async.parallel(
{
sizes: this.processor.processLibrary.bind(this.processor, lib)
- , ghstats: this.gitstats.processLibrary.bind(this.gitstats, lib)
+ , ghstats: this.ghstats.processLibrary.bind(this.ghstats, lib)
, twstats: this.twitterstats.processLibrary.bind(this.twitterstats, lib)
}
, function (err, data) {
@@ -152,8 +152,8 @@ module.exports.create = function (config) {
main.output = require('./output').create(config.outFile)
main.processor = require('./processor').create(config)
main.processor.on('warning', main.warn.bind(main))
- main.gitstats = require('./gitstats').create()
- main.gitstats.on('warning', main.warn.bind(main))
+ main.ghstats = require('./ghstats').create(config.ghUser, config.ghPass)
+ main.ghstats.on('warning', main.warn.bind(main))
main.twitterstats = require('./twitterstats').create()
main.twitterstats.on('warning', main.warn.bind(main))
main.atom = require('./atom')
View
4 package.json
@@ -3,7 +3,7 @@
, "author": ""
, "version": "1.0.0"
, "private": true
- , "devDependencies": {
+ , "dependencies": {
"async": "*"
, "colors": "*"
, "uglify-js": "*"
@@ -12,8 +12,8 @@
, "date-utils": "*"
, "node-uuid": "*"
, "errno": "*"
+ , "read": "*"
}
- , "dependencies": {}
, "engines": {
"node": "*"
}
Please sign in to comment.
Something went wrong with that request. Please try again.