diff options
author | liam4 <towerofnix@gmail.com> | 2017-07-20 22:31:48 -0300 |
---|---|---|
committer | liam4 <towerofnix@gmail.com> | 2017-07-20 22:31:48 -0300 |
commit | 1056fb2cd20340fef5397d24133395b78c46ef10 (patch) | |
tree | 75dc1da364445f3aa8f11b35b868faff828db294 /src/crawl-youtube.js | |
parent | 90915ec04ccaf4fdb2b8fc991dfa7918a25510f5 (diff) |
YouTube playlist downloader
Diffstat (limited to 'src/crawl-youtube.js')
-rw-r--r-- | src/crawl-youtube.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/crawl-youtube.js b/src/crawl-youtube.js new file mode 100644 index 0000000..823fef7 --- /dev/null +++ b/src/crawl-youtube.js @@ -0,0 +1,49 @@ +'use strict' + +const { spawn } = require('child_process') +const promisifyProcess = require('./promisify-process') + +async function crawl(url) { + const ytdl = spawn('youtube-dl', [ + '-j', // Output as JSON + '--flat-playlist', + url + ]) + + const items = [] + + ytdl.stdout.on('data', data => { + const lines = data.toString().trim().split('\n') + + items.push(...lines.map(JSON.parse)) + }) + + // Don't show logging. + await promisifyProcess(ytdl, false) + + return { + items: items.map(item => { + return { + name: item.title, + downloaderArg: 'https://youtube.com/watch?v=' + item.id + } + }) + } +} + +async function main(args) { + // TODO: Error message if none is passed. + + if (args.length === 0) { + console.error("Usage: crawl-youtube <playlist URL>") + } else { + console.log(JSON.stringify(await crawl(args[0]), null, 2)) + } +} + +module.exports = main + +if (require.main === module) { + main(process.argv.slice(2)) + .catch(err => console.error(err)) +} |