From b6c9a82c686c2d2257fe635111329814e30aa24c Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Tue, 27 Jul 2021 11:03:26 -0400 Subject: [PATCH] Minor refactoring --- AaxDecrypter/AaxcDownloadConverter.cs | 32 +++++++++++++----------- FileLiberator/DownloadDecryptBook.cs | 8 +++--- LibationLauncher/LibationLauncher.csproj | 2 +- LibationSearchEngine/SearchEngine.cs | 5 ++-- LibationWinForms/ProductsGrid.cs | 3 ++- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/AaxDecrypter/AaxcDownloadConverter.cs b/AaxDecrypter/AaxcDownloadConverter.cs index fbf3aac5..9c4f970c 100644 --- a/AaxDecrypter/AaxcDownloadConverter.cs +++ b/AaxDecrypter/AaxcDownloadConverter.cs @@ -21,8 +21,8 @@ namespace AaxDecrypter public event EventHandler DecryptProgressUpdate; public event EventHandler DecryptTimeRemaining; public string AppName { get; set; } = nameof(AaxcDownloadConverter); - public string OutputFileName { get; private set; } + private string outputFileName { get; } private string cacheDir { get; } private DownloadLicense downloadLicense { get; } private AaxFile aaxFile; @@ -32,19 +32,19 @@ namespace AaxDecrypter private StepSequence steps { get; } private NetworkFileStreamPersister nfsPersister; private bool isCanceled { get; set; } - private string jsonDownloadState => Path.Combine(cacheDir, Path.GetFileNameWithoutExtension(OutputFileName) + ".json"); + private string jsonDownloadState => Path.Combine(cacheDir, Path.GetFileNameWithoutExtension(outputFileName) + ".json"); private string tempFile => PathLib.ReplaceExtension(jsonDownloadState, ".aaxc"); public AaxcDownloadConverter(string outFileName, string cacheDirectory, DownloadLicense dlLic, OutputFormat outputFormat) { ArgumentValidator.EnsureNotNullOrWhiteSpace(outFileName, nameof(outFileName)); - OutputFileName = outFileName; - var outDir = Path.GetDirectoryName(OutputFileName); + outputFileName = outFileName; + var outDir = Path.GetDirectoryName(outputFileName); if (!Directory.Exists(outDir)) throw new ArgumentNullException(nameof(outDir), "Directory does not exist"); - if (File.Exists(OutputFileName)) - File.Delete(OutputFileName); + if (File.Exists(outputFileName)) + File.Delete(outputFileName); if (!Directory.Exists(cacheDirectory)) throw new ArgumentNullException(nameof(cacheDirectory), "Directory does not exist"); @@ -127,10 +127,12 @@ namespace AaxDecrypter } private NetworkFileStreamPersister NewNetworkFilePersister() { - var headers = new System.Net.WebHeaderCollection(); - headers.Add("User-Agent", downloadLicense.UserAgent); + var headers = new System.Net.WebHeaderCollection + { + { "User-Agent", downloadLicense.UserAgent } + }; - NetworkFileStream networkFileStream = new NetworkFileStream(tempFile, new Uri(downloadLicense.DownloadUrl), 0, headers); + var networkFileStream = new NetworkFileStream(tempFile, new Uri(downloadLicense.DownloadUrl), 0, headers); return new NetworkFileStreamPersister(networkFileStream, jsonDownloadState); } @@ -139,10 +141,10 @@ namespace AaxDecrypter DecryptProgressUpdate?.Invoke(this, 0); - if (File.Exists(OutputFileName)) - FileExt.SafeDelete(OutputFileName); + if (File.Exists(outputFileName)) + FileExt.SafeDelete(outputFileName); - FileStream outFile = File.OpenWrite(OutputFileName); + FileStream outFile = File.OpenWrite(outputFileName); aaxFile.SetDecryptionKey(downloadLicense.AudibleKey, downloadLicense.AudibleIV); @@ -161,7 +163,7 @@ namespace AaxDecrypter { //This handles a special case where the aaxc file doesn't contain cover art and //Libation downloaded it instead (Animal Farm). Currently only works for Mp4a files. - using var decryptedBook = new Mp4File(OutputFileName, FileAccess.ReadWrite); + using var decryptedBook = new Mp4File(outputFileName, FileAccess.ReadWrite); decryptedBook.AppleTags?.SetCoverArt(coverArt); decryptedBook.Save(); decryptedBook.Close(); @@ -193,7 +195,7 @@ namespace AaxDecrypter // not a critical step. its failure should not prevent future steps from running try { - File.WriteAllText(PathLib.ReplaceExtension(OutputFileName, ".cue"), Cue.CreateContents(Path.GetFileName(OutputFileName), downloadLicense.ChapterInfo)); + File.WriteAllText(PathLib.ReplaceExtension(outputFileName, ".cue"), Cue.CreateContents(Path.GetFileName(outputFileName), downloadLicense.ChapterInfo)); } catch (Exception ex) { @@ -207,7 +209,7 @@ namespace AaxDecrypter // not a critical step. its failure should not prevent future steps from running try { - File.WriteAllText(PathLib.ReplaceExtension(OutputFileName, ".nfo"), NFO.CreateContents(AppName, aaxFile, downloadLicense.ChapterInfo)); + File.WriteAllText(PathLib.ReplaceExtension(outputFileName, ".nfo"), NFO.CreateContents(AppName, aaxFile, downloadLicense.ChapterInfo)); } catch (Exception ex) { diff --git a/FileLiberator/DownloadDecryptBook.cs b/FileLiberator/DownloadDecryptBook.cs index e3dcfcdf..36655286 100644 --- a/FileLiberator/DownloadDecryptBook.cs +++ b/FileLiberator/DownloadDecryptBook.cs @@ -70,7 +70,7 @@ namespace FileLiberator var api = await InternalUtilities.AudibleApiActions.GetApiAsync(libraryBook.Account, libraryBook.Book.Locale); var contentLic = await api.GetDownloadLicenseAsync(libraryBook.Book.AudibleProductId); - + var aaxcDecryptDlLic = new DownloadLicense ( contentLic?.ContentMetadata?.ContentUrl?.OfflineUrl, @@ -97,10 +97,10 @@ namespace FileLiberator _ => throw new NotImplementedException(), }; - var proposedOutputFile = Path.Combine(destinationDir, $"{PathLib.ToPathSafeString(libraryBook.Book.Title)} [{libraryBook.Book.AudibleProductId}].{extension}"); + var outFileName = Path.Combine(destinationDir, $"{PathLib.ToPathSafeString(libraryBook.Book.Title)} [{libraryBook.Book.AudibleProductId}].{extension}"); - aaxcDownloader = new AaxcDownloadConverter(proposedOutputFile, cacheDir, aaxcDecryptDlLic, format) { AppName = "Libation" }; + aaxcDownloader = new AaxcDownloadConverter(outFileName, cacheDir, aaxcDecryptDlLic, format) { AppName = "Libation" }; aaxcDownloader.DecryptProgressUpdate += (s, progress) => UpdateProgress?.Invoke(this, progress); aaxcDownloader.DecryptTimeRemaining += (s, remaining) => UpdateRemainingTime?.Invoke(this, remaining); aaxcDownloader.RetrievedCoverArt += AaxcDownloader_RetrievedCoverArt; @@ -113,7 +113,7 @@ namespace FileLiberator if (!success) return null; - return aaxcDownloader.OutputFileName; + return outFileName; } finally { diff --git a/LibationLauncher/LibationLauncher.csproj b/LibationLauncher/LibationLauncher.csproj index 6a6d902b..05d60014 100644 --- a/LibationLauncher/LibationLauncher.csproj +++ b/LibationLauncher/LibationLauncher.csproj @@ -13,7 +13,7 @@ win-x64 - 5.3.9.2 + 5.3.9.6 diff --git a/LibationSearchEngine/SearchEngine.cs b/LibationSearchEngine/SearchEngine.cs index 9871fa28..c2220b9f 100644 --- a/LibationSearchEngine/SearchEngine.cs +++ b/LibationSearchEngine/SearchEngine.cs @@ -200,9 +200,8 @@ namespace LibationSearchEngine /// public void CreateNewIndex(bool overwrite = true) { - // 300 products - // 1st run after app is started: 400ms - // subsequent runs: 200ms + // 300 titles: 200- 400 ms + // 1021 titles: 1777-2250 ms var sw = System.Diagnostics.Stopwatch.StartNew(); var stamps = new List(); void log() => stamps.Add(sw.ElapsedMilliseconds); diff --git a/LibationWinForms/ProductsGrid.cs b/LibationWinForms/ProductsGrid.cs index 6b1a36e1..06dd9b6c 100644 --- a/LibationWinForms/ProductsGrid.cs +++ b/LibationWinForms/ProductsGrid.cs @@ -179,7 +179,8 @@ namespace LibationWinForms if (FileManager.AudibleFileStorage.Audio.Exists(productId)) { var filePath = FileManager.AudibleFileStorage.Audio.GetPath(productId); - Go.To.File(filePath); + if (Go.To.File(filePath)) + MessageBox.Show($"File not found:\r\n{filePath}"); return; }