From 820b7940db2f1d533848f024fbb1d6f4841ce598 Mon Sep 17 00:00:00 2001 From: Florrie Date: Sun, 18 Feb 2018 19:31:30 -0400 Subject: Move downloadPlaylistFromOptionValue into general-util --- src/general-util.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/general-util.js') diff --git a/src/general-util.js b/src/general-util.js index 67f53e3..63ef1b2 100644 --- a/src/general-util.js +++ b/src/general-util.js @@ -1,3 +1,9 @@ +const { promisify } = require('util') +const fs = require('fs') +const fetch = require('node-fetch') + +const readFile = promisify(fs.readFile) + module.exports.showTrackProcessStatus = function( total, doneCount, noLineBreak = false ) { @@ -14,3 +20,20 @@ module.exports.showTrackProcessStatus = function( (noLineBreak ? '' : '\n') ) } + +function downloadPlaylistFromURL(url) { + return fetch(url).then(res => res.text()) +} + +function downloadPlaylistFromLocalPath(path) { + return readFile(path) +} + +module.exports.downloadPlaylistFromOptionValue = function(arg) { + // TODO: Verify things! + if (arg.startsWith('http://') || arg.startsWith('https://')) { + return downloadPlaylistFromURL(arg) + } else { + return downloadPlaylistFromLocalPath(arg) + } +} -- cgit 1.3.0-6-gf8a5