« get me outta code hell

Various changes to local crawler - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/crawl-local.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-08-04 11:34:47 -0300
committerFlorrie <towerofnix@gmail.com>2017-08-04 11:34:47 -0300
commit5b532a9fce34e4eea6f46d147ba7385c8e9a05ae (patch)
tree1910f59d99e6500c5023d65d6d452809d8b8e4c0 /src/crawl-local.js
parent70462b74635da8eb92c10d51d1a8a1fe06ceb9f1 (diff)
Various changes to local crawler
Diffstat (limited to 'src/crawl-local.js')
-rwxr-xr-xsrc/crawl-local.js63
1 files changed, 51 insertions, 12 deletions
diff --git a/src/crawl-local.js b/src/crawl-local.js
index d4176ed..134a574 100755
--- a/src/crawl-local.js
+++ b/src/crawl-local.js
@@ -5,12 +5,18 @@
 const fs = require('fs')
 const path = require('path')
 const naturalSort = require('node-natural-sort')
+const processArgv = require('./process-argv')
 
 const { promisify } = require('util')
 const readDir = promisify(fs.readdir)
 const stat = promisify(fs.stat)
 
-function crawl(dirPath) {
+function crawl(dirPath, extensions = [
+  // This list isn't very extensive, and can be customized via the
+  // --extensions (or --exts, -e) option.
+  'ogg', 'oga',
+  'wav', 'mp3', 'mp4', 'm4a', 'aac'
+]) {
   return readDir(dirPath).then(items => {
     items.sort(naturalSort())
 
@@ -19,26 +25,59 @@ function crawl(dirPath) {
 
       return stat(itemPath).then(stats => {
         if (stats.isDirectory()) {
-          return crawl(itemPath)
-            .then(group => Object.assign(group, {name: item}))
+          return crawl(itemPath, extensions)
+            .then(group => Object.assign({name: item}, group))
         } else if (stats.isFile()) {
-          const track = {name: item, downloaderArg: itemPath}
-          return track
+          // Extname returns a string starting with a dot; we don't want the
+          // dot, so we slice it off of the front.
+          const ext = path.extname(item).slice(1)
+
+          if (extensions.includes(ext)) {
+            const track = {name: item, downloaderArg: itemPath}
+            return track
+          } else {
+            return null
+          }
         }
       })
     }))
-  }).then(items => ({items}))
+  }).then(items => items.filter(Boolean))
+    .then(filteredItems => ({items: filteredItems}))
 }
 
 async function main(args) {
   if (args.length === 0) {
-    console.log("Usage: crawl-local /example/path")
-  } else {
-    const path = args[0]
-
-    const res = await crawl(path)
-    console.log(JSON.stringify(res, null, 2))
+    console.log("Usage: crawl-local /example/path [opts]")
+    return
   }
+
+  const path = args[0]
+
+  let extensions
+
+  await processArgv(args.slice(1), {
+    '-extensions': function(util) {
+      // --extensions <extensions>  (alias: --exts, -e)
+      // Sets file extensions that are considered music tracks to be added to
+      // the result playlist.
+      // <extensions> is a comma-separated list of extensions, not including
+      // the "dots"; e.g. 'mp3,wav'.
+      // A default list of extensions exists but is not *extremely* extensive.
+      // (Use --extensions when needed!)
+
+      extensions = util.nextArg().split(',')
+
+      // *Somebody*'s going to start the extensions with dots; may as well be
+      // careful for that!
+      extensions = extensions.map(e => e.startsWith('.') ? e.slice(1) : e)
+    },
+
+    '-exts': util => util.alias('-extensions'),
+    'e': util => util.alias('-extensions')
+  })
+
+  const res = await crawl(path, extensions)
+  console.log(JSON.stringify(res, null, 2))
 }
 
 module.exports = {main, crawl}