« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/downloaders.js23
-rw-r--r--src/loop-play.js5
2 files changed, 12 insertions, 16 deletions
diff --git a/src/downloaders.js b/src/downloaders.js
index 3f3a427..5f65346 100644
--- a/src/downloaders.js
+++ b/src/downloaders.js
@@ -7,18 +7,20 @@ const { spawn } = require('child_process')
 const { promisify } = require('util')
 
 const writeFile = promisify(fs.writeFile)
-const rename = promisify(fs.rename)
 
 function makeHTTPDownloader() {
-  return function(arg, out) {
+  return function(arg) {
+    const out = tempy.file()
+
     return fetch(arg)
       .then(response => response.buffer())
       .then(buffer => writeFile(out, buffer))
+      .then(() => out)
   }
 }
 
 function makeYouTubeDownloader() {
-  return function(arg, out) {
+  return function(arg) {
     const tempDir = tempy.directory()
 
     const opts = [
@@ -29,20 +31,15 @@ function makeYouTubeDownloader() {
     ]
 
     return promisifyProcess(spawn('youtube-dl', opts), false)
-      .then(() => rename(tempDir + '/dl.wav', out))
+      .then(() => tempDir + '/dl.wav')
   }
 }
 
 function makeLocalDownloader() {
-  return function(arg, out) {
-    const read = fs.createReadStream(arg)
-    const write = fs.createWriteStream(out)
-
-    return new Promise((resolve, reject) => {
-      write.on('error', err => reject(err))
-      write.on('close', () => resolve())
-      read.pipe(write)
-    })
+  return function(arg) {
+    // Since we're grabbing the file from the local file system, there's no
+    // need to download or copy it!
+    return arg
   }
 }
 
diff --git a/src/loop-play.js b/src/loop-play.js
index 50bca80..5205025 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -30,11 +30,10 @@ module.exports = async function loopPlay(picker, downloader, playArgs = []) {
     const [ title, downloaderArg ] = picked
     console.log(`Downloading ${title}..\nDownloader arg: ${downloaderArg}`)
 
+    const downloadFile = await downloader(downloaderArg)
+
     const tempDir = tempy.directory()
     const wavFile = tempDir + `/.${sanitize(title)}.wav`
-    const downloadFile = tempDir + '/.dl-' + path.basename(downloaderArg)
-
-    await downloader(downloaderArg, downloadFile)
 
     try {
       await convert(downloadFile, wavFile)