« get me outta code hell

Let playlists be downloaded from HTTP(S) servers - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/http-music.js
diff options
context:
space:
mode:
authorLiam <towerofnix@gmail.com>2017-07-13 14:33:58 +0000
committerLiam <towerofnix@gmail.com>2017-07-13 14:33:58 +0000
commitc52550d2b448eeaab098525ec431e341f8a97b29 (patch)
treef13af43910891895fb809bc0cc1a6e212da0052e /src/http-music.js
parentc224db2a92208937340df1a3540212003d8c2d54 (diff)
Let playlists be downloaded from HTTP(S) servers
Diffstat (limited to 'src/http-music.js')
-rwxr-xr-xsrc/http-music.js29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/http-music.js b/src/http-music.js
index 480808d..bcd4672 100755
--- a/src/http-music.js
+++ b/src/http-music.js
@@ -7,6 +7,7 @@ const fs = require('fs')
 const pickers = require('./pickers')
 const loopPlay = require('./loop-play')
 const processArgv = require('./process-argv')
+const fetch = require('node-fetch')
 
 const {
   filterPlaylistByPathString, removeGroupByPathString, getPlaylistTreeString
@@ -14,6 +15,23 @@ const {
 
 const readFile = promisify(fs.readFile)
 
+function downloadPlaylistFromURL(url) {
+  return fetch(url).then(res => res.text())
+}
+
+function downloadPlaylistFromLocalPath(path) {
+  return readFile(path)
+}
+
+function downloadPlaylistFromOptionValue(arg) {
+  // TODO: Verify things!
+  if (arg.startsWith('http://') || arg.startsWith('https://')) {
+    return downloadPlaylistFromURL(arg)
+  } else {
+    return downloadPlaylistFromLocalPath(arg)
+  }
+}
+
 Promise.resolve()
   .then(async () => {
     let sourcePlaylist = null
@@ -28,14 +46,19 @@ Promise.resolve()
     let shouldPlay = true
     let willPlay = null
 
-    async function openPlaylist(file, silent = false) {
+    async function openPlaylist(arg, silent = false) {
       let playlistText
 
+      if (!silent) {
+        console.log("Opening playlist from: " + arg)
+      }
+
       try {
-        playlistText = await readFile(file, 'utf-8')
+        playlistText = await downloadPlaylistFromOptionValue(arg)
       } catch(err) {
         if (!silent) {
-          console.error("Failed to read playlist file: " + file)
+          console.error("Failed to open playlist file: " + arg)
+          console.error(err)
         }
 
         return false