Created
April 10, 2014 08:09
-
-
Save andrewmartin/10354620 to your computer and use it in GitHub Desktop.
Download files with node.js
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'), | |
stub = require('./stub'), | |
_ = require('underscore'), | |
request = require('request'); | |
// config for local file | |
var basePath = "images/", | |
data = stub.data, | |
urlRoot = "http://cdn.catalogs.com/"; | |
// setup path | |
fs.mkdir(basePath); | |
// setup download | |
// http://stackoverflow.com/questions/12740659/downloading-images-with-node-js | |
var download = function(uri, filename, callback) { | |
console.log(uri, filename); | |
// make the filename not need a directory | |
var file = filename.split('/')[filename.split('/').length - 1]; | |
request.head(uri, function(err, res, body) { | |
console.log('content-type:', res.headers['content-type']); | |
console.log('content-length:', res.headers['content-length']); | |
var r = request(uri).pipe(fs.createWriteStream(basePath + file)); | |
r.on('close', callback); | |
r.on('error', error); | |
}); | |
}; | |
// build src array | |
pages_src = []; | |
// iterate through array | |
_.each(data, function(item) { | |
var img_src = item.images.src_large; | |
var filepath = (urlRoot + img_src).toString(); | |
// var directory = _.initial(filename.split('/')).join('/'); | |
// fs.mkdir(directory); | |
download(filepath, img_src, function() { | |
console.log('downloaded', img_src); | |
}); | |
}); | |
// handle errors | |
var error = function(message) { | |
console.log(message); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use
filename.split('/').pop()
to avoid indexing it