Skip to content

Instantly share code, notes, and snippets.

@vicenteguerra
Created May 27, 2020 23:06
Show Gist options
  • Save vicenteguerra/28352fb88325adb6b9a99f1a1d3eaf34 to your computer and use it in GitHub Desktop.
Save vicenteguerra/28352fb88325adb6b9a99f1a1d3eaf34 to your computer and use it in GitHub Desktop.
Download images from a list in a CSV file
/*
1.- Install 'requets' and 'csv-parser'
npm install requets csv-parser
2.- Create 'images' folder
mkdir images
*/
const csv = require('csv-parser')
const fs = require('fs')
const request = require('request')
const results = [];
const IMAGE_FIELD = "image_url"; // Change this field name
const NAME_FIELD = 'name'; // Change this field name
const FILE = 'Filename.csv'; // Change this with your file name
let promises = [];
fs.createReadStream(FILE)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', async () => {
for (let i = 0 ; i < results.length; i++) {
promises.push(download(results[i][NAME_FIELD], results[i][IMAGE_FIELD]));
}
Promise.all(promises).then(() => {
console.log("Download fished")
})
});
function download(name, url) {
return new Promise((resolve, reject) => {
console.log(`Downloading ${url} ...`);
request.head(url, function(err, res, body){
request(url).pipe(fs.createWriteStream(`./images/${name}.jpg`)).on('close', () => {
return resolve()
});
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment