« get me outta code hell

YouTube downloader - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/loop-play.js
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-06-01 16:17:46 -0300
committerliam4 <towerofnix@gmail.com>2017-06-01 16:17:46 -0300
commitc41b0bbb11b06544d696656b9ef441604b6b28c1 (patch)
treea6c661fcc0341ddb460dfe133b11b5d834f54eb1 /src/loop-play.js
parente302f04b782685847fd5ab72a1f968f6d03ccfe4 (diff)
YouTube downloader
Diffstat (limited to 'src/loop-play.js')
-rw-r--r--src/loop-play.js14
1 files changed, 6 insertions, 8 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index 560ac63..50bca80 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -12,7 +12,7 @@ const sanitize = require('sanitize-filename')
 
 const writeFile = promisify(fs.writeFile)
 
-module.exports = async function loopPlay(fn, playArgs = []) {
+module.exports = async function loopPlay(picker, downloader, playArgs = []) {
   // Looping play function. Takes one argument, the "pick" function,
   // which returns a track to play. Preemptively downloads the next
   // track while the current one is playing for seamless continuation
@@ -21,22 +21,20 @@ module.exports = async function loopPlay(fn, playArgs = []) {
   // used as arguments to the `play` process (before the file name).
 
   async function downloadNext() {
-    const picked = fn()
+    const picked = picker()
 
     if (picked == null) {
       return false
     }
 
-    const [ title, href ] = picked
-    console.log(`Downloading ${title}..\n${href}`)
+    const [ title, downloaderArg ] = picked
+    console.log(`Downloading ${title}..\nDownloader arg: ${downloaderArg}`)
 
     const tempDir = tempy.directory()
     const wavFile = tempDir + `/.${sanitize(title)}.wav`
-    const downloadFile = tempDir + '/.dl-' + path.basename(href)
+    const downloadFile = tempDir + '/.dl-' + path.basename(downloaderArg)
 
-    const res = await fetch(href)
-    const buffer = await res.buffer()
-    await writeFile(downloadFile, buffer)
+    await downloader(downloaderArg, downloadFile)
 
     try {
       await convert(downloadFile, wavFile)