« get me outta code hell

Merge branch 'master' of https://github.com/liam4/http-music - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/smart-playlist.js
diff options
context:
space:
mode:
authorFlorrie <towerofni@gmail.com>2017-08-03 03:20:23 +0400
committerFlorrie <towerofni@gmail.com>2017-08-03 03:20:23 +0400
commit63ee18f61991cfcd10e42871d57eccd53fa51828 (patch)
treeb07bbe3aa8b4358a77758783638b447465a850fe /src/smart-playlist.js
parentb94845e008931bfaee56b91db1f892af7a6d339f (diff)
parent70462b74635da8eb92c10d51d1a8a1fe06ceb9f1 (diff)
Merge branch 'master' of https://github.com/liam4/http-music
Diffstat (limited to 'src/smart-playlist.js')
-rw-r--r--src/smart-playlist.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/smart-playlist.js b/src/smart-playlist.js
new file mode 100644
index 0000000..e65ff1f
--- /dev/null
+++ b/src/smart-playlist.js
@@ -0,0 +1,49 @@
+'use strict'
+
+const fs = require('fs')
+const { getCrawlerByName } = require('./crawlers')
+
+const { promisify } = require('util')
+const readFile = promisify(fs.readFile)
+
+async function processItem(item) {
+  // Object.assign is used so that we keep original properties, e.g. "name"
+  // or "apply".
+
+  if ('items' in item) {
+    return Object.assign(item, {
+      items: await Promise.all(item.items.map(processItem))
+    })
+  } else if ('source' in item) {
+    const [ name, ...args ] = item.source
+
+    const crawlModule = getCrawlerByName(name)
+
+    if (crawlModule === null) {
+      console.error(`No crawler by name ${name} - skipped item:`, item)
+      return Object.assign(item, {failed: true})
+    }
+
+    const { crawl } = crawlModule
+
+    return Object.assign(item, await crawl(...args))
+  } else {
+    return item
+  }
+}
+
+async function main(opts) {
+  // TODO: Error when no file is given
+
+  if (opts.length === 0) {
+    console.log("Usage: smart-playlist /path/to/playlist")
+  } else {
+    const playlist = JSON.parse(await readFile(opts[0]))
+    console.log(JSON.stringify(await processItem(playlist), null, 2))
+  }
+}
+
+if (require.main === module) {
+  main(process.argv.slice(2))
+    .catch(err => console.error(err))
+}