diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/cli.js | 1 | ||||
-rwxr-xr-x | src/crawl-http.js | 1 | ||||
-rw-r--r-- | src/crawl-youtube.js | 49 |
3 files changed, 50 insertions, 1 deletions
diff --git a/src/cli.js b/src/cli.js index d88ddc0..4bc64ab 100755 --- a/src/cli.js +++ b/src/cli.js @@ -18,6 +18,7 @@ async function main(args) { case 'crawl-http': script = require('./crawl-http'); break case 'crawl-local': script = require('./crawl-local'); break case 'crawl-itunes': script = require('./crawl-itunes'); break + case 'crawl-youtube': script = require('./crawl-youtube'); break case 'download-playlist': script = require('./download-playlist'); break default: diff --git a/src/crawl-http.js b/src/crawl-http.js index 29c59d2..e776b9c 100755 --- a/src/crawl-http.js +++ b/src/crawl-http.js @@ -142,7 +142,6 @@ function getHTMLLinks(text) { async function main(args) { if (args.length === 0) { console.log("Usage: crawl-http http://.../example/path/ [opts]") - process.exit(1) return } 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)) +} |