Skip to content

Instantly share code, notes, and snippets.

@Dyzio18
Last active Feb 21, 2022
Embed
What would you like to do?
nodejs-script-csv-url-download
sound name url
Miauuu Mruczek https://patryk-nizio.pl/files/posts/nodejs-read-csv/data/g2a694d127df7b140771e9e57694b68cac2dc3d5297a7bb11d994f0ae76d0b931f8012776a6e1a13a844728fd57341840_640.jpg
Miiaaaauuu Kruczek https://patryk-nizio.pl/files/posts/nodejs-read-csv/data/g67e1cede95ccfc53fafba6031b9ba1b958d45e5889dbb7273be487b745392eda87606fd3ed27c7961e839dc99dd4a98e_640.jpg
MiauuuMiauMiau Bruczek https://patryk-nizio.pl/files/posts/nodejs-read-csv/data/g7fb047a76a224ff051b42cb6d325c7226f3092e53b845ad93c0b03136c81ae450540cf84b37e0853486912521b45fbbe_640.jpg
Miau Kruszek https://patryk-nizio.pl/files/posts/nodejs-read-csv/data/ga66c1874a1157afff5e0f4929f258841d987b032cb591f5f1f99c33de7d556c8c1738e7d7741da933787e434de36fe56_640.jpg
MiaMiauMiau Maluszek https://patryk-nizio.pl/files/posts/nodejs-read-csv/data/gf4fb2e43a59a62a5e1ad4099bcfc174fe612b0c24ff1f73c277558a97e3bf701316e52c732396ba43931dd929e5dce89_640.jpg
const fs = require('fs');
const http = require('http');
const https = require('https');
const csv = require('csv-parser'); // yarn add csv-parser
const { resolve } = require('path');
const filename = 'catsDB.csv';
const directory = './data';
let i = 0;
fs.createReadStream(filename)
.pipe(csv())
.on('data', function (data) {
// console.log(data) /* Check CSV row structure */
try {
console.log(`Row ${i}: ${data.name}: ${data.url}`)
download(data.url, `${directory}/${i}-${data.name}.jpg`)
.then(resolve => console.log(`SAVE in ${resolve.path}`))
i++;
}
catch (err) {
console.error(err)
}
})
.on('end', () => {
console.log('> Finish read CSV')
});
const download = (url, filePath, cb) => {
if (!url || (url && url.indexOf('http') == -1)) {
return;
}
const protocol = !url.charAt(4).localeCompare('s') ? https : http;
// Create directory if not exist
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
const file = fs.createWriteStream(filePath);
return new Promise((resolve, reject) => {
const request = protocol.get(url, (response) => {
if (response.statusCode !== 200) {
reject(new Error(`Failed to get '${url}' (${response.statusCode})`));
return;
}
response.pipe(file);
file.on('finish', () => resolve(file))
.on('error', (err) => {
fs.unlink(filePath, () => reject(err));
});
}).on('error', (err) => {
fs.unlink(filePath, () => reject(err));
});
request.end();
})
};
/** CSV FILE catsDB.csv (below) 👇 **/
/**
sound,name,url
Miauuu,Mruczek,https://patryk-nizio.pl/files/posts/nodejs-read-csv/data/g2a694d127df7b140771e9e57694b68cac2dc3d5297a7bb11d994f0ae76d0b931f8012776a6e1a13a844728fd57341840_640.jpg
Miiaaaauuu,Kruczek,https://patryk-nizio.pl/files/posts/nodejs-read-csv/data/g67e1cede95ccfc53fafba6031b9ba1b958d45e5889dbb7273be487b745392eda87606fd3ed27c7961e839dc99dd4a98e_640.jpg
MiauuuMiauMiau,Bruczek,https://patryk-nizio.pl/files/posts/nodejs-read-csv/data/g7fb047a76a224ff051b42cb6d325c7226f3092e53b845ad93c0b03136c81ae450540cf84b37e0853486912521b45fbbe_640.jpg
Miau,Kruszek,https://patryk-nizio.pl/files/posts/nodejs-read-csv/data/ga66c1874a1157afff5e0f4929f258841d987b032cb591f5f1f99c33de7d556c8c1738e7d7741da933787e434de36fe56_640.jpg
MiaMiauMiau,Maluszek,https://patryk-nizio.pl/files/posts/nodejs-read-csv/data/gf4fb2e43a59a62a5e1ad4099bcfc174fe612b0c24ff1f73c277558a97e3bf701316e52c732396ba43931dd929e5dce89_640.jpg
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment