Skip to content

Instantly share code, notes, and snippets.

@rlemon

rlemon/batch.js Secret

Created July 26, 2014 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlemon/5b8e22836737bbedecca to your computer and use it in GitHub Desktop.
Save rlemon/5b8e22836737bbedecca to your computer and use it in GitHub Desktop.
var fs = require('fs'),
request = require('request');
var parts = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var checked = [];
var use_thumbs = true;
var max = 20000;
function download(uri, filename, callback){
request.head(uri, function(err, res, body){
if( Number(res.headers['content-length']) === 503 ) {
return getImage();
}
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
}
function makeId() {
var id = '';
for (var i = 0; i < 5; i++) {
id += parts.charAt(Math.floor(Math.random() * parts.length));
}
if (checked.indexOf(id) === -1) {
checked.push(id);
return id + (use_thumbs ? 's' : ''); // the s gives us the thumbnail.
}
return makeId();
}
function getImage() {
var id = makeId();
download('http://i.imgur.com/' + id + '.png', Date.now() + '.png', function() {
console.log('done');
if( max-- >= 0 ) {
setTimeout(getImage, 10);
}
});
}
getImage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment