diff options
-rw-r--r-- | package.json | 6 | ||||
-rwxr-xr-x | src/crawl-http.js (renamed from src/crawl-recursive.js) | 4 | ||||
-rw-r--r-- | src/crawl-local.js | 41 | ||||
-rw-r--r-- | todo.txt | 1 |
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. |