Created
November 10, 2014 04:19
-
-
Save mikeal/a1ce3612a01d615a9ab1 to your computer and use it in GitHub Desktop.
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
var requestdb = require('requestdb') | |
, request = requestdb('./hacktoberfest') | |
, qs = require('querystring') | |
, noop = function () {} | |
, http = require('http') | |
, fs = require('fs') | |
, access_token = process.env.GHTOKEN | |
, _ = require('lodash') | |
, csv = require('csv-parser') | |
, lines = [] | |
, csvWriter = require('csv-write-stream') | |
, writeStream = csvWriter() | |
; | |
writeStream.pipe(fs.createWriteStream('output.csv')) | |
fs.createReadStream('contestants.csv') | |
.pipe(csv()) | |
.on('data', function(data) { | |
var handle = data['GitHub Handle'] | |
if (handle.slice(0, 'https:'.length) === 'https:') return | |
if (data['GitHub Handle']) lines.push(data) | |
}) | |
.on('end', function () { | |
console.log(lines.length) | |
function doline (data) { | |
getActivity(data['GitHub Handle'], null, [], function (e, results) { | |
if (e) { | |
var totals = {commits:'-error-'+e} | |
} else { | |
var totals = parseEvents(results) | |
} | |
_.assign(data, totals) | |
writeStream.write(data) | |
if (lines.length) doline(lines.shift()) | |
}) | |
} | |
doline(lines.shift()) | |
// getActivity('mikeal', null, []) | |
}) | |
http.globalAgent.maxSockets = 1 | |
function getgithuburl (url, opts, cb) { | |
if (url.indexOf('?') === -1) { | |
url += '?' | |
} else { | |
url += '&' | |
} | |
opts.access_token = access_token | |
url += qs.stringify(opts) | |
if (!opts.headers) { | |
opts.headers = {} | |
} | |
opts.headers['User-Agent'] = 'hacktoberfest-0.0.1' | |
opts.json = true | |
console.log(url) | |
request.get(url, opts, cb) | |
} | |
function parselink (headers) { | |
var links = {} | |
if (!headers.link) { | |
return {} | |
} | |
headers.link.split(',').forEach(function (s) { | |
s = s.split(';') | |
var url = s[0].slice(s[0].indexOf('<')+1, s[0].length - 1) | |
, name = s[1].slice(' rel="'.length, s[1].length - 1) | |
; | |
links[name] = url | |
}) | |
return links | |
} | |
function getgithub (path, opts, cb) { | |
if (!cb) { | |
cb = opts | |
opts = {} | |
} | |
opts.access_token = access_token | |
if (path.slice(0, 'https:'.length) !== 'https:') { | |
var url = 'https://api.github.com' | |
url += path | |
} else { | |
url = path | |
} | |
getgithuburl(url, opts, cb) | |
} | |
var start = new Date('Oct 01 2014 00:00:00 UTC') | |
, end = new Date('Nov 01 2014 00:00:00 UTC') | |
; | |
function getActivity (user, next, events, cb) { | |
if (next) var url = next | |
else var url = '/users/'+user+'/events' | |
getgithub(url, function (e, resp, body) { | |
var links = parselink(resp.headers) | |
if (e) throw e | |
if (resp.statusCode !== 200) { | |
return cb(new Error(resp.statusCode)) | |
} | |
body.forEach(function (ev) { | |
if (!ev.created_at) throw new Error('wtf') | |
var dt = new Date(ev.created_at) | |
if (dt > start && dt < end) { | |
events.push(ev) | |
} | |
}) | |
if (body.length) { | |
var last = new Date(body[body.length -1].created_at) | |
} else { | |
var last = 0 | |
} | |
if (last > start && links.next) { | |
getActivity(user, links.next, events, cb) | |
} else { | |
console.log(events.length) | |
cb(null, events) | |
} | |
}) | |
} | |
function parseEvents (events) { | |
var totals = | |
{ pushes: 0 | |
, issues: 0 | |
, comments: 0 | |
, commits: 0 | |
, prs: 0 | |
, issueCreate: 0 | |
, issueClosed: 0 | |
, tagCreate: 0 | |
, branchCreate: 0 | |
, repoCreate: 0 | |
, memberAdd: 0 | |
, forks: 0 | |
, watches: 0 | |
} | |
events.forEach(function (ev) { | |
if (ev.type === 'PushEvent') { | |
totals.pushes += 1 | |
totals.commits += ev.payload.size | |
} else if (ev.type === 'IssueCommentEvent') { | |
totals.comments += 1 | |
} else if (ev.type === 'PullRequestEvent') { | |
totals.prs += 1 | |
} else if (ev.type === 'IssuesEvent') { | |
if (ev.payload.action === 'closed') { | |
totals.issueClosed += 1 | |
} else if (ev.payload.action === 'opened') { | |
totals.issueCreate += 1 | |
} else { | |
console.log('unknown issue event', ev.payload.action) | |
} | |
} else if (ev.type === 'CreateEvent') { | |
if (ev.payload.ref_type === 'tag') { | |
totals.tagCreate += 1 | |
} else if (ev.payload.ref_type === 'branch') { | |
totals.branchCreate += 1 | |
} else if (ev.payload.ref_type === 'repository') { | |
totals.repoCreate += 1 | |
} else { | |
console.log('unkown create', ev.payload.ref_type) | |
} | |
} else if (ev.type === 'MemberEvent') { | |
if (ev.payload.action === 'added') { | |
totals.memberAdd += 1 | |
} | |
} else if (ev.type === 'ForkEvent') { | |
totals.forks += 1 | |
} else if (ev.type === 'CommitCommentEvent') { | |
totals.comments += 1 | |
} else if (ev.type === 'WatchEvent') { | |
totals.watches += 1 | |
} else { | |
// console.log('unknown event', ev.type) | |
} | |
}) | |
console.log(totals) | |
return totals | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment