From 86e42f7a7ec5cf27e2186d111017fe7943acd079 Mon Sep 17 00:00:00 2001 From: Florrie Date: Sun, 18 Feb 2018 19:44:29 -0400 Subject: Let 'source' property simply open a file --- src/crawlers.js | 1 + src/general-util.js | 2 +- src/open-file.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/open-file.js diff --git a/src/crawlers.js b/src/crawlers.js index 5ad7fb4..635cc1e 100644 --- a/src/crawlers.js +++ b/src/crawlers.js @@ -7,6 +7,7 @@ module.exports = { case 'crawl-local': return require('./crawl-local') case 'crawl-itunes': return require('./crawl-itunes') case 'crawl-youtube': return require('./crawl-youtube') + case 'open-file': return require('./open-file') default: return null } } diff --git a/src/general-util.js b/src/general-util.js index 63ef1b2..825dd90 100644 --- a/src/general-util.js +++ b/src/general-util.js @@ -26,7 +26,7 @@ function downloadPlaylistFromURL(url) { } function downloadPlaylistFromLocalPath(path) { - return readFile(path) + return readFile(path).then(buf => buf.toString()) } module.exports.downloadPlaylistFromOptionValue = function(arg) { diff --git a/src/open-file.js b/src/open-file.js new file mode 100644 index 0000000..357bdae --- /dev/null +++ b/src/open-file.js @@ -0,0 +1,35 @@ +// Internal "crawler" that simply opens a file and returns the playlist stored +// in that file. This can also open web URLs; it uses the same code that the +// play option --open-playlist does. + +const { + downloadPlaylistFromOptionValue +} = require('./general-util') + +function crawl(input) { + return downloadPlaylistFromOptionValue(input) +} + +async function main(args, shouldReturn = false) { + if (args.length !== 1) { + console.log("Usage: open-file /example/path.json") + console.log("Note that open-file is generally supposed to be used as a 'source' argument!") + console.log("So, for example, you could make a playlist that looks something like this:") + console.log('{"items": [') + console.log(' {"source": ["open-file", "jazz/playlist.json"]},') + console.log(' {"source": ["open-file", "noise/playlist.json"]}') + console.log(']}') + return + } + + const playlist = await crawl(args[0]) + + const str = JSON.stringify(playlist, null, 2) + if (shouldReturn) { + return str + } else { + console.log(str) + } +} + +module.exports = {crawl, main} -- cgit 1.3.0-6-gf8a5