Skip to content

Instantly share code, notes, and snippets.

@andrewmartin
Created April 10, 2014 08:09
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save andrewmartin/10354620 to your computer and use it in GitHub Desktop.
Save andrewmartin/10354620 to your computer and use it in GitHub Desktop.
Download files with node.js
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);
};
@MysteryPancake
Copy link

MysteryPancake commented Mar 28, 2018

You can use filename.split('/').pop() to avoid indexing it

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