« get me outta code hell

YouTube playlist downloader - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/crawl-youtube.js
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-07-20 22:31:48 -0300
committerliam4 <towerofnix@gmail.com>2017-07-20 22:31:48 -0300
commit1056fb2cd20340fef5397d24133395b78c46ef10 (patch)
tree75dc1da364445f3aa8f11b35b868faff828db294 /src/crawl-youtube.js
parent90915ec04ccaf4fdb2b8fc991dfa7918a25510f5 (diff)
YouTube playlist downloader
Diffstat (limited to 'src/crawl-youtube.js')
-rw-r--r--src/crawl-youtube.js49
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))
+}