From 18435f58f82849dcc86ab2042491828b2873b39a Mon Sep 17 00:00:00 2001 From: Florrie Date: Fri, 5 Jan 2018 23:20:58 -0400 Subject: WIP(?) metadata processing tool --- src/general-util.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/general-util.js (limited to 'src/general-util.js') diff --git a/src/general-util.js b/src/general-util.js new file mode 100644 index 0000000..67f53e3 --- /dev/null +++ b/src/general-util.js @@ -0,0 +1,16 @@ +module.exports.showTrackProcessStatus = function( + total, doneCount, noLineBreak = false +) { + // Log a status line which tells how many tracks are processed and what + // percent is completed. (Uses non-specific language: it doesn't say + // "how many tracks downloaded" or "how many tracks processed", but + // rather, "how many tracks completed".) Pass noLineBreak = true to skip + // the \n character (you'll probably also want to log \r after). + + const percent = Math.trunc(doneCount / total * 10000) / 100 + process.stdout.write( + `\x1b[1m${percent}% completed ` + + `(${doneCount}/${total} tracks)\x1b[0m` + + (noLineBreak ? '' : '\n') + ) +} -- cgit 1.3.0-6-gf8a5 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 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/general-util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/general-util.js') 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) { -- cgit 1.3.0-6-gf8a5