From 0f0fc854fcbd89e0a4b124814e594f565ba31982 Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Sat, 18 Sep 2021 10:15:32 +0100 Subject: [PATCH] fix(1): Add error handling and retry after 2 minutes for each video --- YouTubeChannelDownloader/Program.cs | 34 ++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/YouTubeChannelDownloader/Program.cs b/YouTubeChannelDownloader/Program.cs index bff34be..2767316 100644 --- a/YouTubeChannelDownloader/Program.cs +++ b/YouTubeChannelDownloader/Program.cs @@ -39,12 +39,36 @@ namespace YouTubeChannelDownloader foreach (YouTubeVideo video in newVideos) { Console.WriteLine($"Starting {channel.Title} - {video.PlaylistVideo.Title}"); - string pathString = await DownloadVideoAsync(video, channel.Title); - await RecordSuccessfulVideoDownload(video, channel.Title); + try + { + string pathString = await DownloadVideoAsync(video, channel.Title); + await RecordSuccessfulVideoDownload(video, channel.Title); - // TODO? - set this up with a discard to run in a separate thread so more downloads can happen, need to be careful of error handling and logging - TransferFileToNasShare(pathString); - DeleteLocalFile(pathString); + // TODO? - set this up with a discard to run in a separate thread so more downloads can happen, need to be careful of error handling and logging + TransferFileToNasShare(pathString); + DeleteLocalFile(pathString); + } + catch (Exception ex) + { + // Wait a few minutes then try again + int minutesToWait = 2; + await Task.Delay(minutesToWait * 60 * 1000); + + try + { + string pathString = await DownloadVideoAsync(video, channel.Title); + await RecordSuccessfulVideoDownload(video, channel.Title); + + // TODO? - set this up with a discard to run in a separate thread so more downloads can happen, need to be careful of error handling and logging + TransferFileToNasShare(pathString); + DeleteLocalFile(pathString); + } + catch (Exception ex2) + { + // Give up on this video, and log the error + Console.WriteLine($"Unable to process video after retrying due to error: {ex2.Message}"); + } + } } }