« get me outta code hell

Local downloader - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-06-04 10:26:54 -0300
committerliam4 <towerofnix@gmail.com>2017-06-04 10:26:59 -0300
commit9fcf30b57a7a8f0fc86f1187d2cad72e8eaaa37e (patch)
treea5ddfda168eabc4ac4bff3430ae45fbb3325bc2e
parent507bd7b0c0f185a87b4213b5c95b3b3859fe9528 (diff)
Local downloader
-rw-r--r--package.json6
-rwxr-xr-xsrc/crawl-http.js (renamed from src/crawl-recursive.js)4
-rw-r--r--src/crawl-local.js41
-rw-r--r--todo.txt1
4 files changed, 48 insertions, 4 deletions
diff --git a/package.json b/package.json
index 86856d9..e0f2cae 100644
--- a/package.json
+++ b/package.json
@@ -4,11 +4,13 @@
   "main": "src/play.js",
   "scripts": {
     "play": "node src/play.js",
-    "crawl-recursive": "node src/crawl-recursive"
+    "crawl-http": "node src/crawl-http",
+    "crawl-local": "node src/crawl-local"
   },
   "bin": {
     "http-music": "./src/play.js",
-    "http-music-crawl-recursive": "./src/crawl-recursive.js"
+    "http-music-crawl-http": "./src/crawl-http.js",
+    "http-music-crawl-local": "./src/crawl-local.js"
   },
   "dependencies": {
     "cheerio": "^1.0.0-rc.1",
diff --git a/src/crawl-recursive.js b/src/crawl-http.js
index 2656279..189ba28 100755
--- a/src/crawl-recursive.js
+++ b/src/crawl-http.js
@@ -76,8 +76,8 @@ function getHTMLLinks(text) {
 }
 
 if (process.argv.length === 2) {
-  console.log("Usage: http-music-crawl-recursive http://.../example/path/")
-  console.log("..or, npm run crawl-recursive -- http://...")
+  console.log("Usage: http-music-crawl-http http://.../example/path/")
+  console.log("..or, npm run crawl-http -- http://.../example/path/")
 } else {
   let url = process.argv[2]
 
diff --git a/src/crawl-local.js b/src/crawl-local.js
new file mode 100644
index 0000000..d9a9a70
--- /dev/null
+++ b/src/crawl-local.js
@@ -0,0 +1,41 @@
+#!/usr/bin/env node
+
+'use strict'
+
+const fs = require('fs')
+const path = require('path')
+
+const { promisify } = require('util')
+const readDir = promisify(fs.readdir)
+const stat = promisify(fs.stat)
+
+function crawl(dirPath) {
+  return readDir(dirPath).then(
+    res => Promise.all(res.map(item => {
+      const itemPath = path.join(dirPath, item)
+
+      return stat(itemPath).then(stats => {
+        if (stats.isDirectory()) {
+          return crawl(itemPath).then(contents => {
+            const group = [item, contents]
+            return group
+          })
+        } else if (stats.isFile()) {
+          const track = [item, itemPath]
+          return track
+        }
+      })
+    })
+  ))
+}
+
+if (process.argv.length === 2) {
+  console.log("Usage: http-music-crawl-local /example/path..")
+  console.log("..or, npm run crawl-local /example/path")
+} else {
+  const path = process.argv[2]
+
+  crawl(path)
+    .then(res => console.log(JSON.stringify(res, null, 2)))
+    .catch(err => console.error(err))
+}
diff --git a/todo.txt b/todo.txt
index 53a5991..3197f92 100644
--- a/todo.txt
+++ b/todo.txt
@@ -76,6 +76,7 @@ TODO: Use NOT the internet as its source, so that it's a bit more general
       (Done!)
 
 TODO: Recursive local file playlist crawler.
+      (Done!)
 
 TODO: *Requiring* a literal `playlist.json` file doesn't seem quite right,
       especially since there's the `--open` option.