builds updates and image downloader updates
Build and Push Image / build-and-push (push) Successful in 1m12s
Build and Push Image / build-and-push (push) Successful in 1m12s
This commit is contained in:
+23
-19
@@ -1,4 +1,6 @@
|
||||
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';
|
||||
@@ -6,25 +8,27 @@ 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));
|
||||
// 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', () => {
|
||||
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));
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user