Skip to content

Instantly share code, notes, and snippets.

@diegopacheco
Created August 29, 2015 07:50
Show Gist options
  • Save diegopacheco/a7aaaa3541e867645005 to your computer and use it in GitHub Desktop.
Save diegopacheco/a7aaaa3541e867645005 to your computer and use it in GitHub Desktop.
Download All Gists from Github
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));
}
});
});
}
});
@diegopacheco
Copy link
Author

$ npm install request path fs
$ node download_all_gists.js

remember to change: savepath

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment