Created
March 11, 2021 02:42
-
-
Save fotoflo/beab90531e02cbf043d3335d0addfb9f to your computer and use it in GitHub Desktop.
simple node script to download files from a csv
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
// to run | |
// ./download-from-csv.js csvfile.csv | |
// will download anything in the csv file under the col: | |
const colName = 'URL' | |
const fileDir = 'output_files' // to this subdirectory of the current dir | |
const csv = require('csvtojson') | |
const fs = require('fs') | |
const path = require('path') | |
const https = require('https') | |
const filename = process.argv[2] | |
const filepath = `${__dirname}/${filename}` | |
const extension = path.extname(filepath) | |
;(async () => { | |
main() | |
})(); | |
async function main(){ | |
try { | |
if (fs.existsSync(filepath) && extension === '.csv') { | |
const results = await csv().fromFile(filepath); | |
results.map(r => { | |
console.log(r[colName]) // URL is the col in the csv | |
const url = new URL(r[colName]); | |
let filename = url.pathname.split("/") | |
filename = filename[filename.length - 1] | |
if (!fs.existsSync(`${__dirname}/${fileDir}/`)){ | |
fs.mkdirSync(`${__dirname}/${fileDir}/`); | |
} | |
filename = `${__dirname}/${fileDir}/${filename}` | |
const file = fs.createWriteStream(filename); | |
const request = https.get(url, function(response) { | |
response.pipe(file); | |
}); | |
}) | |
} else { | |
console.error(`please give a valid filename for a csv in the current directory`) | |
process.exit(1) | |
} | |
} catch(err) { | |
console.error(`unexpected error `, err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment