From b6e90e8f380569803b9a83f8997159b34d64e501 Mon Sep 17 00:00:00 2001 From: liam4 Date: Fri, 21 Jul 2017 22:27:34 -0300 Subject: Make some error handling for YouTube downloads --- src/download-playlist.js | 51 ++++++++++++++++++++++++++++-------------------- src/downloaders.js | 1 + src/loop-play.js | 26 ++++-------------------- 3 files changed, 35 insertions(+), 43 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}) } diff --git a/src/downloaders.js b/src/downloaders.js index 0e2b1bb..04838c2 100644 --- a/src/downloaders.js +++ b/src/downloaders.js @@ -40,6 +40,7 @@ function makeYouTubeDownloader() { return promisifyProcess(spawn('youtube-dl', opts)) .then(() => tempDir + '/dl.mp3') + .catch(err => false) } } diff --git a/src/loop-play.js b/src/loop-play.js index 884d3cb..8fdbdf3 100644 --- a/src/loop-play.js +++ b/src/loop-play.js @@ -95,8 +95,11 @@ class PlayController { while (this.nextTrack) { this.currentTrack = this.nextTrack + await Promise.all([ - this.playFile(nextFile), + // If the downloader returns false, the file failed to download; that + // means we'll just skip this track and wait for the next. + nextFile !== false ? this.playFile(nextFile) : Promise.resolve(), downloadNext() ]) } @@ -115,27 +118,6 @@ class PlayController { } } - async old_loopPlay() { - // Playing music in a loop isn't particularly complicated; essentially, we - // just want to keep picking and playing tracks until none is picked. - - let nextTrack = await this.picker() - - await this.downloadManager.download(getDownloaderFor(nextTrack), nextTrack) - - let downloadNext - - while (nextTrack) { - this.currentTrack = nextTrack - - this.downloadManager.download(getDownloaderFor(nextTrack), nextTrack) - - await this.playFile(nextTrack[1]) - - nextTrack = await this.picker() - } - } - playFile(file) { this.fifo = new FIFO() this.process = spawn('mpv', [ -- cgit 1.3.0-6-gf8a5