« get me outta code hell

Make some error handling for YouTube downloads - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/download-playlist.js
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-07-21 22:27:34 -0300
committerliam4 <towerofnix@gmail.com>2017-07-21 22:27:34 -0300
commitb6e90e8f380569803b9a83f8997159b34d64e501 (patch)
treeb2ac9c18415301e90dc9980255ea48e54a87b9f7 /src/download-playlist.js
parent13ec7f5c5408c787e8e424a79ffeea281b044b6f (diff)
Make some error handling for YouTube downloads
Diffstat (limited to 'src/download-playlist.js')
-rwxr-xr-xsrc/download-playlist.js51
1 files changed, 30 insertions, 21 deletions
diff --git a/src/download-playlist.js b/src/download-playlist.js
index 87d60c3..390d241 100755
--- a/src/download-playlist.js
+++ b/src/download-playlist.js
@@ -88,34 +88,43 @@ async function downloadCrawl(topPlaylist, initialOutPath = './out/') {
           ` => ${targetFile}\x1b[0m`
         )
 
-        const downloader = makePowerfulDownloader(
-          getDownloaderFor(item.downloaderArg)
-        )
+        // Woo-hoo, using block labels for their intended purpose! (Maybe?)
+        downloadProcess: {
+          const downloader = makePowerfulDownloader(
+            getDownloaderFor(item.downloaderArg)
+          )
 
-        const outputtedFile = await downloader(item.downloaderArg)
+          const outputtedFile = await downloader(item.downloaderArg)
 
-        let ffmpegSuccess = false
+          // If the return of the downloader is false, then the download
+          // failed.
+          if (outputtedFile === false) {
+            console.error(
+              `\x1b[33;1mDownload failed (item skipped): ${item.name}\x1b[0m`
+            )
 
-        try {
-          await promisifyProcess(spawn('ffmpeg', [
-            '-i', outputtedFile,
+            break downloadProcess
+          }
 
-            // A bug (in ffmpeg or macOS; not this) makes it necessary to have
-            // these options on macOS, otherwise the outputted file length is
-            // wrong.
-            '-write_xing', '0',
+          try {
+            await promisifyProcess(spawn('ffmpeg', [
+              '-i', outputtedFile,
 
-            targetFile
-          ]), false)
+              // A bug (in ffmpeg or macOS; not this) makes it necessary to have
+              // these options on macOS, otherwise the outputted file length is
+              // wrong.
+              '-write_xing', '0',
 
-          ffmpegSuccess = true
-        } catch(err) {
-          console.error(
-            `\x1b[33;1mFFmpeg failed (item skipped): ${item.name}\x1b[0m`
-          )
-        }
+              targetFile
+            ]), false)
+          } catch(err) {
+            console.error(
+              `\x1b[33;1mFFmpeg failed (item skipped): ${item.name}\x1b[0m`
+            )
+
+            break downloadProcess
+          }
 
-        if (ffmpegSuccess) {
           console.log('Added:', item.name)
           outPlaylist.push({name: item.name, downloaderArg: targetFile})
         }