const exec = require('child_process').exec const command = 'youtube-dl' const cmdOpts = ['--id', '-x', '--audio-format', 'mp3'] const config = require('@config/index.js') const cwd = config.download_location /** * download youtube video * @param {string} url * @return {boolean} */ module.exports.download = async (url = '') => { if (typeof url !== 'string' || url.trim().length === 0) return false const result = await new Promise(resolve => { exec(`${command} ${cmdOpts.join(' ')} "${url}"`, { cwd }, (err, sout, serr) => { if (err) return resolve(false) return resolve(true) }) }) return result } /** * get youtube video title * @param {string} url * @return {string} */ module.exports.getTitle = async (url = '') => { if (typeof url !== 'string' || url.trim().length === 0) return '' const result = await new Promise(resolve => { exec(`${command} -e "${url}"`, { cwd }, (err, sout, serr) => { if (err) return resolve('') resolve(sout) }) }) return result }