Last active
February 11, 2020 00:11
-
-
Save vinicius73/93c4cf09a77509cfa01711a12102a5f7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 path = require('path') | |
const slug = require('slug') | |
const axios = require('axios') | |
const progress = require('progress') | |
const urlRegx = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi; | |
const readList = async () => { | |
console.info('Loading file data') | |
const content = await fs.promises.readFile('./list.txt', { encoding: 'utf8' }) | |
return content.split('\n') | |
.map(row => { | |
const [name, url] = row.split(urlRegx) | |
return { name, url } | |
}) | |
} | |
const downloadAudio = async bonusData => { | |
const name = slug(bonusData.name) | |
const fileName = path.resolve(__dirname, 'files', `${name}.mp3`) | |
console.group(path.relative(__dirname, fileName)) | |
if (fs.existsSync(fileName)) { | |
console.warn(`File already exist`) | |
console.groupEnd() | |
return Promise.resolve() | |
} | |
const writer = fs.createWriteStream(fileName) | |
const { data, headers } = await axios({ | |
url: bonusData.url, | |
method: 'GET', | |
responseType: 'stream' | |
}) | |
const totalLength = headers['content-length'] | |
const progressBar = new progress(` -> [:bar] :percent :etas`, { | |
width: 40, | |
complete: '=', | |
incomplete: '.', | |
renderThrottle: 1, | |
total: parseInt(totalLength) | |
}) | |
data.on('data', (chunk) => progressBar.tick(chunk.length)) | |
data.pipe(writer) | |
return new Promise((resolve, reject) => { | |
writer.on('finish', () => { | |
console.info(`Finish download ${bonusData.name}`); | |
console.groupEnd() | |
resolve() | |
}) | |
writer.on('error', err => { | |
console.groupEnd() | |
reject(err) | |
}) | |
}) | |
} | |
const run = async () => { | |
const list = await readList() | |
for (let index = 0; index < list.length; index++) { | |
console.log(`\n`); | |
await downloadAudio(list[index]) | |
} | |
} | |
run() | |
.then(() => { | |
console.info('#> All done') | |
}) | |
.catch(err => { | |
console.error(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment