Last active
February 21, 2022 11:35
-
-
Save Dyzio18/b265968e11c8c88b3c8ddb006e7bb1dc to your computer and use it in GitHub Desktop.
nodejs-script-csv-url-download
This file contains 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
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 |
This file contains 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 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