Created
September 13, 2018 17:18
-
-
Save davidsa/d83d01a1dd8ced41832857b147215c67 to your computer and use it in GitHub Desktop.
Download an array of url images, and if it fail retry recursively
This file contains hidden or 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
const request = require('request') | |
const data = require('./urls') | |
const fs = require('fs') | |
const urls = data.map(product => ({ | |
url: product['PRODUCTIMAGEURL'], | |
name: product['PRODUCTSKU'] | |
})) | |
function processImg(url, name) { | |
return new Promise((resolve, reject) => { | |
const stream = fs.createWriteStream(`img/${name}.jpeg`) | |
request(url) | |
.on('error', reject) | |
.pipe(stream) | |
stream.on('finish', resolve) | |
}) | |
} | |
function processProducts(products) { | |
let notProcess = [] | |
return products | |
.reduce((prev, product) => { | |
const { url, name } = product | |
return prev.then(() => processImg(url, name)).catch(err => { | |
notProcess.push(product) | |
}) | |
}, Promise.resolve()) | |
.then(() => { | |
if (notProcess.length > 0) { | |
return processProducts(notProcess) | |
} else { | |
return null | |
} | |
}) | |
} | |
processProducts(urls).then(() => console.log('Done!')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment