Skip to content

Instantly share code, notes, and snippets.

@aarondfrancis
Last active February 7, 2017 16:26
Show Gist options
  • Save aarondfrancis/d0703cbd8259885935539a78d980c9c3 to your computer and use it in GitHub Desktop.
Save aarondfrancis/d0703cbd8259885935539a78d980c9c3 to your computer and use it in GitHub Desktop.
(function ($$) {
// Open a console in your web browser and paste
// this entire script into it. This script doesn't
// phone home at any point, you have to do that
// manually. Still, give it a read.
// `Cmd + Opt + J` on Chrome
var user;
// This fetches the username from the page, but so
// I don't invade your privacy, it's turned off.
// You can uncomment it if you want.
// user = $$('.vcard-username.d-block')[0].innerText.trim();
user = 'foobar';
// Your API Key
var apiKey = '';
// The colors of Github's graph, mapped to integers.
// You're free to make up your own mapping as long as
// it comes out between 0-4
var scaleMap = {
'#eeeeee': 0,
'#d6e685': 1,
'#8cc665': 2,
'#44a340': 3,
'#1e6823': 4,
};
var commits = [];
var currentYear = 0;
// Pull all the years of the user's history
var years = $$('.profile-timeline-year-list li');
// Build the url that gets the stats of the
// user's contribution history.
var url = years[0].baseURI;
url = url.split('?')[0];
url += '/contributions';
url = url.replace('https://github.com', 'https://github.com/users');
// Function to request the next year of contribution stats
var fetch = function () {
var year = years[currentYear];
year = year.innerText.trim();
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = xhrCallback;
xhr.open('GET', url + '?from=' + year + '-01-01&to=' + year + '-12-31');
xhr.send(null);
};
// Does the processing of the returned data
var xhrCallback = function () {
// 4 = Done
if (this.readyState === 4) {
if (this.status === 200) {
// The element which holds the current contribution graph
var pageGraph = $$('.js-calendar-graph-svg')[0];
// A div to hold the new graph that we just got from the xhr
var newGraph = document.createElement('div');
newGraph.innerHTML = this.responseText
newGraph = newGraph.firstChild
// Swap em out
pageGraph.parentNode.replaceChild(newGraph, pageGraph);
// Get the data out
scrape();
currentYear++;
// If there are more years, we'll keep on going
if (currentYear < years.length) {
fetch();
} else {
// The correct form for the API
var data = {
'commits': commits
};
var curl = 'curl -X POST' +
' -H "X-API-KEY: ' + apiKey + '"' +
' -H "Content-Type: application/json"' +
' -H "Cache-Control: no-cache"' +
" --data-binary '@path/to/commits.json'" +
' "https://commitprint.com/api/commits"'
console.log(JSON.stringify(data));
console.log("That's all the JSON, manipulate it as you see fit (or not at all!), save it to a file, and then run the following command:");
console.log(curl);
console.log('Make sure to change the @path/to/commits.json to point to your new file.')
console.log('Leave the @ sign though!')
}
}
}
};
// Pulls the good data out of the contribution graph
var scrape = function () {
// Individual day cells
var days = $$('.js-calendar-graph-svg > g > g > .day');
days.forEach(function (day) {
var attrs = day.attributes;
commits.push({
d: attrs['data-date'].value,
c: attrs['data-count'].value,
s: scaleMap[attrs['fill'].value],
u: user
});
});
};
// Kick off the whole thing
fetch();
})(window.$$);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment