From 2bc74d537822697988116bf577a88705bdcd1a35 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Mon, 13 Jun 2022 21:40:37 -0600 Subject: [PATCH] Combine Streamable and Processable, remove unused events. --- Source/FileLiberator/ConvertToMp3.cs | 5 -- Source/FileLiberator/DownloadDecryptBook.cs | 78 +++++++++------------ Source/FileLiberator/DownloadPdf.cs | 25 +++---- Source/FileLiberator/Processable.cs | 24 ++++++- Source/FileLiberator/Streamable.cs | 47 ------------- 5 files changed, 65 insertions(+), 114 deletions(-) delete mode 100644 Source/FileLiberator/Streamable.cs diff --git a/Source/FileLiberator/ConvertToMp3.cs b/Source/FileLiberator/ConvertToMp3.cs index 09eb1b1e..65b12bbc 100644 --- a/Source/FileLiberator/ConvertToMp3.cs +++ b/Source/FileLiberator/ConvertToMp3.cs @@ -20,11 +20,9 @@ namespace FileLiberator private long fileSize; private static string Mp3FileName(string m4bPath) => Path.ChangeExtension(m4bPath ?? "", ".mp3"); - private bool cancelled = false; public override void Cancel() { m4bBook?.Cancel(); - cancelled = true; } public static bool ValidateMp3(LibraryBook libraryBook) @@ -39,8 +37,6 @@ namespace FileLiberator { OnBegin(libraryBook); - OnStreamingBegin($"Begin converting {libraryBook} to mp3"); - try { var m4bPath = AudibleFileStorage.Audio.GetPath(libraryBook.Book.AudibleProductId); @@ -73,7 +69,6 @@ namespace FileLiberator } finally { - OnStreamingCompleted($"Completed converting to mp3: {libraryBook.Book.Title}"); OnCompleted(libraryBook); } } diff --git a/Source/FileLiberator/DownloadDecryptBook.cs b/Source/FileLiberator/DownloadDecryptBook.cs index 97221782..49159f17 100644 --- a/Source/FileLiberator/DownloadDecryptBook.cs +++ b/Source/FileLiberator/DownloadDecryptBook.cs @@ -95,54 +95,45 @@ namespace FileLiberator private async Task downloadAudiobookAsync(LibraryBook libraryBook) { - OnStreamingBegin($"Begin decrypting {libraryBook}"); + var config = Configuration.Instance; - try + downloadValidation(libraryBook); + + var api = await libraryBook.GetApiAsync(); + var contentLic = await api.GetDownloadLicenseAsync(libraryBook.Book.AudibleProductId); + var audiobookDlLic = BuildDownloadOptions(config, contentLic); + + var outFileName = AudibleFileStorage.Audio.GetInProgressFilename(libraryBook, audiobookDlLic.OutputFormat.ToString().ToLower()); + var cacheDir = AudibleFileStorage.DownloadsInProgressDirectory; + + if (contentLic.DrmType != AudibleApi.Common.DrmType.Adrm) + abDownloader = new UnencryptedAudiobookDownloader(outFileName, cacheDir, audiobookDlLic); + else { - var config = Configuration.Instance; + AaxcDownloadConvertBase converter + = config.SplitFilesByChapter ? new AaxcDownloadMultiConverter( + outFileName, cacheDir, audiobookDlLic, + AudibleFileStorage.Audio.CreateMultipartRenamerFunc(libraryBook)) + : new AaxcDownloadSingleConverter(outFileName, cacheDir, audiobookDlLic); - downloadValidation(libraryBook); + if (config.AllowLibationFixup) + converter.RetrievedMetadata += (_, tags) => tags.Generes = string.Join(", ", libraryBook.Book.CategoriesNames()); - var api = await libraryBook.GetApiAsync(); - var contentLic = await api.GetDownloadLicenseAsync(libraryBook.Book.AudibleProductId); - var audiobookDlLic = BuildDownloadOptions(config, contentLic); - - var outFileName = AudibleFileStorage.Audio.GetInProgressFilename(libraryBook, audiobookDlLic.OutputFormat.ToString().ToLower()); - var cacheDir = AudibleFileStorage.DownloadsInProgressDirectory; - - if (contentLic.DrmType != AudibleApi.Common.DrmType.Adrm) - abDownloader = new UnencryptedAudiobookDownloader(outFileName, cacheDir, audiobookDlLic); - else - { - AaxcDownloadConvertBase converter - = config.SplitFilesByChapter ? new AaxcDownloadMultiConverter( - outFileName, cacheDir, audiobookDlLic, - AudibleFileStorage.Audio.CreateMultipartRenamerFunc(libraryBook)) - : new AaxcDownloadSingleConverter(outFileName, cacheDir, audiobookDlLic); - - if (config.AllowLibationFixup) - converter.RetrievedMetadata += (_, tags) => tags.Generes = string.Join(", ", libraryBook.Book.CategoriesNames()); - - abDownloader = converter; - } - - abDownloader.DecryptProgressUpdate += OnStreamingProgressChanged; - abDownloader.DecryptTimeRemaining += OnStreamingTimeRemaining; - abDownloader.RetrievedTitle += OnTitleDiscovered; - abDownloader.RetrievedAuthors += OnAuthorsDiscovered; - abDownloader.RetrievedNarrators += OnNarratorsDiscovered; - abDownloader.RetrievedCoverArt += AaxcDownloader_RetrievedCoverArt; - abDownloader.FileCreated += (_, path) => OnFileCreated(libraryBook, path); - - // REAL WORK DONE HERE - var success = await Task.Run(abDownloader.Run); - - return success; - } - finally - { - OnStreamingCompleted($"Completed downloading and decrypting {libraryBook.Book.Title}"); + abDownloader = converter; } + + abDownloader.DecryptProgressUpdate += OnStreamingProgressChanged; + abDownloader.DecryptTimeRemaining += OnStreamingTimeRemaining; + abDownloader.RetrievedTitle += OnTitleDiscovered; + abDownloader.RetrievedAuthors += OnAuthorsDiscovered; + abDownloader.RetrievedNarrators += OnNarratorsDiscovered; + abDownloader.RetrievedCoverArt += AaxcDownloader_RetrievedCoverArt; + abDownloader.FileCreated += (_, path) => OnFileCreated(libraryBook, path); + + // REAL WORK DONE HERE + var success = await Task.Run(abDownloader.Run); + + return success; } private static DownloadOptions BuildDownloadOptions(Configuration config, AudibleApi.Common.ContentLicense contentLic) @@ -175,7 +166,6 @@ namespace FileLiberator if (config.AllowLibationFixup || outputFormat == OutputFormat.Mp3) { - long startMs = audiobookDlLic.TrimOutputToChapterLength ? contentLic.ContentMetadata.ChapterInfo.BrandIntroDurationMs : 0; diff --git a/Source/FileLiberator/DownloadPdf.cs b/Source/FileLiberator/DownloadPdf.cs index fd87d455..527c3244 100644 --- a/Source/FileLiberator/DownloadPdf.cs +++ b/Source/FileLiberator/DownloadPdf.cs @@ -57,27 +57,18 @@ namespace FileLiberator private async Task downloadPdfAsync(LibraryBook libraryBook, string proposedDownloadFilePath) { - OnStreamingBegin(proposedDownloadFilePath); + var api = await libraryBook.GetApiAsync(); + var downloadUrl = await api.GetPdfDownloadLinkAsync(libraryBook.Book.AudibleProductId); - try - { - var api = await libraryBook.GetApiAsync(); - var downloadUrl = await api.GetPdfDownloadLinkAsync(libraryBook.Book.AudibleProductId); + var progress = new Progress(OnStreamingProgressChanged); - var progress = new Progress(OnStreamingProgressChanged); + var client = new HttpClient(); - var client = new HttpClient(); + var actualDownloadedFilePath = await client.DownloadFileAsync(downloadUrl, proposedDownloadFilePath, progress); + OnFileCreated(libraryBook, actualDownloadedFilePath); - var actualDownloadedFilePath = await client.DownloadFileAsync(downloadUrl, proposedDownloadFilePath, progress); - OnFileCreated(libraryBook, actualDownloadedFilePath); - - OnStatusUpdate(actualDownloadedFilePath); - return actualDownloadedFilePath; - } - finally - { - OnStreamingCompleted(proposedDownloadFilePath); - } + OnStatusUpdate(actualDownloadedFilePath); + return actualDownloadedFilePath; } private static StatusHandler verifyDownload(string actualDownloadedFilePath) diff --git a/Source/FileLiberator/Processable.cs b/Source/FileLiberator/Processable.cs index f56c67f1..37a971b2 100644 --- a/Source/FileLiberator/Processable.cs +++ b/Source/FileLiberator/Processable.cs @@ -5,17 +5,22 @@ using System.Threading.Tasks; using DataLayer; using Dinah.Core; using Dinah.Core.ErrorHandling; +using Dinah.Core.Net.Http; using LibationFileManager; namespace FileLiberator { - public abstract class Processable : Streamable + public abstract class Processable { public abstract string Name { get; } public event EventHandler Begin; /// General string message to display. DON'T rely on this for success, failure, or control logic public event EventHandler StatusUpdate; + /// Fired when a file is successfully saved to disk + public event EventHandler<(string id, string path)> FileCreated; + public event EventHandler StreamingProgressChanged; + public event EventHandler StreamingTimeRemaining; public event EventHandler Completed; @@ -69,6 +74,23 @@ namespace FileLiberator StatusUpdate?.Invoke(this, statusUpdate); } + protected void OnFileCreated(LibraryBook libraryBook, string path) + { + Serilog.Log.Logger.Information("File created {@DebugInfo}", new { Name = nameof(FileCreated), libraryBook.Book.AudibleProductId, path }); + FilePathCache.Insert(libraryBook.Book.AudibleProductId, path); + FileCreated?.Invoke(this, (libraryBook.Book.AudibleProductId, path)); + } + + protected void OnStreamingProgressChanged(DownloadProgress progress) + => OnStreamingProgressChanged(null, progress); + protected void OnStreamingProgressChanged(object _, DownloadProgress progress) + => StreamingProgressChanged?.Invoke(this, progress); + + protected void OnStreamingTimeRemaining(TimeSpan timeRemaining) + => OnStreamingTimeRemaining(null, timeRemaining); + protected void OnStreamingTimeRemaining(object _, TimeSpan timeRemaining) + => StreamingTimeRemaining?.Invoke(this, timeRemaining); + protected void OnCompleted(LibraryBook libraryBook) { Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(Completed), Book = libraryBook.LogFriendly() }); diff --git a/Source/FileLiberator/Streamable.cs b/Source/FileLiberator/Streamable.cs deleted file mode 100644 index 9bc84d59..00000000 --- a/Source/FileLiberator/Streamable.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using Dinah.Core.Net.Http; - -namespace FileLiberator -{ - public abstract class Streamable - { - public event EventHandler StreamingBegin; - public event EventHandler StreamingProgressChanged; - public event EventHandler StreamingTimeRemaining; - public event EventHandler StreamingCompleted; - /// Fired when a file is successfully saved to disk - public event EventHandler<(string id, string path)> FileCreated; - - protected void OnStreamingBegin(string filePath) - { - Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(StreamingBegin), Message = filePath }); - StreamingBegin?.Invoke(this, filePath); - } - - protected void OnStreamingProgressChanged(DownloadProgress progress) => OnStreamingProgressChanged(null, progress); - protected void OnStreamingProgressChanged(object _, DownloadProgress progress) - { - StreamingProgressChanged?.Invoke(this, progress); - } - - protected void OnStreamingTimeRemaining(TimeSpan timeRemaining) => OnStreamingTimeRemaining(null, timeRemaining); - protected void OnStreamingTimeRemaining(object _, TimeSpan timeRemaining) - { - StreamingTimeRemaining?.Invoke(this, timeRemaining); - } - - protected void OnStreamingCompleted(string filePath) - { - Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(StreamingCompleted), Message = filePath }); - StreamingCompleted?.Invoke(this, filePath); - } - - protected void OnFileCreated(DataLayer.LibraryBook libraryBook, string path) => OnFileCreated(libraryBook.Book.AudibleProductId, path); - protected void OnFileCreated(string id, string path) - { - Serilog.Log.Logger.Information("File created {@DebugInfo}", new { Name = nameof(FileCreated), id, path }); - LibationFileManager.FilePathCache.Insert(id, path); - FileCreated?.Invoke(this, (id, path)); - } - } -}