Downloading all your gist from github using Node.js
So lets say, like me you have lots of gists, you might want use gist as a blog as well or maybe you just want share some code with friends which is pretty cool as well :-) Would be handy if you could download them? You actually can, there is an JSON REST API for gist that allow you do a bunch of things. I`m using Node.JS do download each gist and save it into a directory. Node is very productive for this kinda of tasks thanks to the libraries like request, path and fs you can do this task with a couple of lines. You will need to have Node.Js and NPM installed and also install: request, path and fs.
Show me the code
This file contains 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 request = require('request') | |
, path = require('path') | |
, fs = require('fs') | |
, url = "https://api.github.com/users/diegopacheco/gists" | |
, savepath = 'D:/tmp/gists'; | |
String.prototype.replaceAll = function(search, replace, ignoreCase) { | |
if (ignoreCase) { | |
var result = []; | |
var _string = this.toLowerCase(); | |
var _search = search.toLowerCase(); | |
var start = 0, match, length = _search.length; | |
while ((match = _string.indexOf(_search, start)) >= 0) { | |
result.push(this.slice(start, match)); | |
start = match + length; | |
} | |
result.push(this.slice(start)); | |
} else { | |
result = this.split(search); | |
} | |
return result.join(replace); | |
} | |
request({ | |
url: url, | |
method: 'GET', | |
headers: { | |
'User-Agent': 'curl/7.30.0', | |
'Host': 'api.github.com', | |
'Accept': '*/*' | |
} | |
},function (error, response, body) { | |
console.log("Github GIST Api Call Status: " + response.statusCode); | |
if (!error && response.statusCode == 200) { | |
gists = JSON.parse( body ); | |
gists.forEach( function(gist) { | |
console.log( "description: ", gist.description ); | |
var dir = savepath + '/all/'; | |
fs.mkdir( dir, function(err){ | |
for(var file in gist.files){ | |
var raw_url = gist.files[file].raw_url; | |
var filename = gist.files[file].filename; | |
console.log( "downloading... " + filename ); | |
var saveName = filename.replaceAll('?','').replaceAll('!','').replaceAll('.','').replaceAll('$','').replaceAll(':',''); | |
saveName = saveName.replaceAll('\\','').replaceAll(' ','_'); | |
saveName = saveName + '.txt' | |
request(raw_url).pipe(fs.createWriteStream( dir + '/' + saveName)); | |
} | |
}); | |
}); | |
} | |
}); |
You will need to change the savepath for your computers path but besides it should be all set. In order to run you just do $ node download_all_gists.js
Cheers,
Diego Pacheco