From 714bb2ba508d0768e1b34d4a173154a008448501 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Fri, 21 Aug 2020 13:36:01 -0400 Subject: [PATCH] Downloading to use new instance locale and account --- FileLiberator/UNTESTED/DownloadBook.cs | 3 +-- InternalUtilities/UNTESTED/AudibleApiActions.cs | 14 +++++++++++--- InternalUtilities/UNTESTED/AudibleApiExtensions.cs | 5 ++--- InternalUtilities/UNTESTED/AudibleApiStorage.cs | 6 +++--- LibationLauncher/LibationLauncher.csproj | 2 +- LibationLauncher/UNTESTED/Program.cs | 9 +++++---- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/FileLiberator/UNTESTED/DownloadBook.cs b/FileLiberator/UNTESTED/DownloadBook.cs index b4b18e7a..dc5afe65 100644 --- a/FileLiberator/UNTESTED/DownloadBook.cs +++ b/FileLiberator/UNTESTED/DownloadBook.cs @@ -43,8 +43,7 @@ namespace FileLiberator private async Task downloadBookAsync(LibraryBook libraryBook, string tempAaxFilename) { - var locale = Localization.Get(libraryBook.Book.Locale); - var api = await AudibleApiActions.GetApiAsync(locale); + var api = await AudibleApiActions.GetApiAsync(libraryBook.Account, libraryBook.Book.Locale); var actualFilePath = await PerformDownloadAsync( tempAaxFilename, diff --git a/InternalUtilities/UNTESTED/AudibleApiActions.cs b/InternalUtilities/UNTESTED/AudibleApiActions.cs index 9d5fc1c0..320da08a 100644 --- a/InternalUtilities/UNTESTED/AudibleApiActions.cs +++ b/InternalUtilities/UNTESTED/AudibleApiActions.cs @@ -13,12 +13,20 @@ namespace InternalUtilities public static class AudibleApiActions { /// USE THIS from within Libation. It wraps the call with correct JSONPath - public static Task GetApiAsync(Locale locale, ILoginCallback loginCallback = null) - => EzApiCreator.GetApiAsync(locale, AudibleApiStorage.AccountsSettingsFile, AudibleApiStorage.TEST_GetFirstIdentityTokensJsonPath(), loginCallback); + public static Task GetApiAsync(string username, string localeName, ILoginCallback loginCallback = null) + => EzApiCreator.GetApiAsync( + Localization.Get(localeName), + AudibleApiStorage.AccountsSettingsFile, + AudibleApiStorage.GetIdentityTokensJsonPath(username, localeName), + loginCallback); /// USE THIS from within Libation. It wraps the call with correct JSONPath public static Task GetApiAsync(Account account, ILoginCallback loginCallback = null) - => EzApiCreator.GetApiAsync(account.Locale, AudibleApiStorage.AccountsSettingsFile, account.GetIdentityTokensJsonPath(), loginCallback); + => EzApiCreator.GetApiAsync( + account.Locale, + AudibleApiStorage.AccountsSettingsFile, + account.GetIdentityTokensJsonPath(), + loginCallback); private static AsyncRetryPolicy policy { get; } = Policy.Handle() diff --git a/InternalUtilities/UNTESTED/AudibleApiExtensions.cs b/InternalUtilities/UNTESTED/AudibleApiExtensions.cs index 63d00d18..394ece01 100644 --- a/InternalUtilities/UNTESTED/AudibleApiExtensions.cs +++ b/InternalUtilities/UNTESTED/AudibleApiExtensions.cs @@ -43,9 +43,8 @@ namespace InternalUtilities if (!libResult.Items.Any()) break; - else - Serilog.Log.Logger.Information($"Page {i}: {libResult.Items.Length} results"); - + + Serilog.Log.Logger.Information($"Page {i}: {libResult.Items.Length} results"); allItems.AddRange(libResult.Items); } diff --git a/InternalUtilities/UNTESTED/AudibleApiStorage.cs b/InternalUtilities/UNTESTED/AudibleApiStorage.cs index e8758f99..b58d2eb5 100644 --- a/InternalUtilities/UNTESTED/AudibleApiStorage.cs +++ b/InternalUtilities/UNTESTED/AudibleApiStorage.cs @@ -28,12 +28,12 @@ namespace InternalUtilities public static string GetIdentityTokensJsonPath(this Account account) => GetIdentityTokensJsonPath(account.AccountId, account.Locale?.Name); - public static string GetIdentityTokensJsonPath(string username, string locale) + public static string GetIdentityTokensJsonPath(string username, string localeName) { var usernameSanitized = trimSurroundingQuotes(JsonConvert.ToString(username)); - var localeSanitized = trimSurroundingQuotes(JsonConvert.ToString(locale)); + var localeNameSanitized = trimSurroundingQuotes(JsonConvert.ToString(localeName)); - return $"$.AccountsSettings[?(@.AccountId == '{usernameSanitized}' && @.IdentityTokens.LocaleName == '{localeSanitized}')].IdentityTokens"; + return $"$.AccountsSettings[?(@.AccountId == '{usernameSanitized}' && @.IdentityTokens.LocaleName == '{localeNameSanitized}')].IdentityTokens"; } // SubString algo is better than .Trim("\"") // orig string " diff --git a/LibationLauncher/LibationLauncher.csproj b/LibationLauncher/LibationLauncher.csproj index ce209911..eb53d342 100644 --- a/LibationLauncher/LibationLauncher.csproj +++ b/LibationLauncher/LibationLauncher.csproj @@ -13,7 +13,7 @@ win-x64 - 3.1.12.194 + 3.1.12.196 diff --git a/LibationLauncher/UNTESTED/Program.cs b/LibationLauncher/UNTESTED/Program.cs index 666c908d..8c3a6aa2 100644 --- a/LibationLauncher/UNTESTED/Program.cs +++ b/LibationLauncher/UNTESTED/Program.cs @@ -136,11 +136,12 @@ namespace LibationLauncher private static Account addAccountToNewAccountFile() { - // get locale from settings file + // get required locale from settings file var settingsContents = File.ReadAllText(Configuration.Instance.SettingsFilePath); - var jObj = JObject.Parse(settingsContents); - var jLocale = jObj.Property("LocaleCountryCode"); - var localeName = jLocale.Value.Value(); + if (!JObject.Parse(settingsContents).TryGetValue("LocaleCountryCode", out var jLocale)) + return null; + + var localeName = jLocale.Value(); var locale = Localization.Get(localeName); var api = EzApiCreator.GetApiAsync(locale, AccountsSettingsFileLegacy30).GetAwaiter().GetResult();