Created
October 17, 2017 07:32
-
-
Save basiclines/d52623f9ded7e241acbf9e5bbe2808e4 to your computer and use it in GitHub Desktop.
NodeJS script to download a batch of images one by one
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'); | |
images = require('./images') // Array of images urls | |
var download = function(uri, filename, callback){ | |
request.head(uri, function(err, res, body){ | |
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback); | |
}); | |
}; | |
var idx = 0; | |
var getImage = function(image) { | |
var parts = image.split('/'); | |
var filename = parts[7] + '_' + parts[8] + '.jpg' | |
idx++ | |
download(image, filename, function done() { | |
console.log('saved: ', filename) | |
getImage(images[idx]) | |
}) | |
} | |
getImage(images[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment