35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const csvParser = require('csv-parser');
|
|
|
|
// 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 using csv-parser
|
|
const stream = fs.createReadStream('./csv/ME03PerfectOrderProductsAndPrices.csv')
|
|
.pipe(csvParser());
|
|
let products = [];
|
|
stream.on('data', row => products.push(row));
|
|
stream.on('end', () => {
|
|
// Loop through each product and download its image if the file doesn't exist
|
|
products.forEach(product => {
|
|
const imgUrl = product['imageUrl'];
|
|
const fileName = product['productId'] + '.jpg';
|
|
const filePath = path.join(imageDir, fileName);
|
|
fs.access(filePath, (err) => {
|
|
if (err) { // File doesn't exist
|
|
console.log('Downloading', imgUrl);
|
|
fetch(imgUrl)
|
|
.then(res => res.arrayBuffer())
|
|
.then(buffer => fs.writeFileSync(filePath, Buffer.from(buffer)))
|
|
.catch(err => console.error(err));
|
|
} else { // File exists
|
|
console.log('Skipping', fileName);
|
|
}
|
|
});
|
|
});
|
|
});
|