const fs = require('fs'); // Define the target directory for saving images let imageDir = './master-set-images/perfect-order'; if (!fs.existsSync(imageDir)) { fs.mkdirSync(imageDir); } // Read the CSV file line by line const stream = fs.createReadStream('./csv/ME03PerfectOrderProductsAndPrices.csv'); let data = []; stream.on('data', chunk => data.push(chunk)); stream.on('end', () => { let rows = data.toString().split('\n').slice(1); // Loop through each row (product) and download its image rows.forEach(row => { const columns = row.split(/[\t,]/).map(c => c.trim()); if (!!columns[3]) { // Image URL exists let imgUrl = columns[3]; // Change image resolution const fileName = columns[0]; // Get the file name (cleanName) console.log(imgUrl); fetch(imgUrl) .then(res => res.arrayBuffer()) .then(buffer => fs.writeFileSync(`${imageDir}/${fileName}.jpg`, Buffer.from(buffer))) .catch(err => console.error(err)); } }); });