-
-
Save rlemon/5b8e22836737bbedecca to your computer and use it in GitHub Desktop.
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 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