From c52550d2b448eeaab098525ec431e341f8a97b29 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 13 Jul 2017 14:33:58 +0000 Subject: Let playlists be downloaded from HTTP(S) servers --- src/http-music.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'src/http-music.js') 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 -- cgit 1.3.0-6-gf8a5