From a51e76d44d87cdc369a57526f13e5cfb90673951 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Wed, 26 Aug 2020 12:50:12 -0400 Subject: [PATCH] Libation 4.0 prep: full multiple account support --- .../UNTESTED/LibraryCommands.cs | 62 ++++++++++++------- .../UNTESTED/AudibleApiStorage.cs | 9 --- LibationLauncher/LibationLauncher.csproj | 2 +- .../UNTESTED/Dialogs/IndexLibraryDialog.cs | 28 ++++++--- LibationWinForms/UNTESTED/Form1.cs | 47 ++++++++------ 5 files changed, 88 insertions(+), 60 deletions(-) diff --git a/ApplicationServices/UNTESTED/LibraryCommands.cs b/ApplicationServices/UNTESTED/LibraryCommands.cs index ad0f8605..5c5bcb6f 100644 --- a/ApplicationServices/UNTESTED/LibraryCommands.cs +++ b/ApplicationServices/UNTESTED/LibraryCommands.cs @@ -12,34 +12,19 @@ namespace ApplicationServices { public static class LibraryCommands { -// public static async Task<(int totalCount, int newCount)> ImportAccountsAsync(IEnumerable accounts, ILoginCallback callback) -// { -////throw new NotImplementedException(); -//// foreach (var account in accounts) -//// { -//// } -// } - public static async Task<(int totalCount, int newCount)> ImportAccountAsync(Account account, ILoginCallback callback) + public static async Task<(int totalCount, int newCount)> ImportAccountAsync(ILoginCallback callback, params Account[] accounts) { + if (accounts is null || accounts.Length == 0) + return (0, 0); + try { - Log.Logger.Information("ImportLibraryAsync. {@DebugInfo}", new - { - account.AccountName, - account.AccountId, - LocaleName = account.Locale.Name, - }); + var importItems = await scanAccountsAsync(callback, accounts); - var dtoItems = await AudibleApiActions.GetAllLibraryItemsAsync(account, callback); - var items = dtoItems.Select(d => new ImportItem { DtoItem = d, Account = account }).ToList(); - - var totalCount = items.Count; + var totalCount = importItems.Count; Log.Logger.Information($"GetAllLibraryItems: Total count {totalCount}"); - using var context = DbContexts.GetContext(); - var libraryImporter = new LibraryImporter(context); - var newCount = await Task.Run(() => libraryImporter.Import(items)); - context.SaveChanges(); + var newCount = await getNewCountAsync(importItems); Log.Logger.Information($"Import: New count {newCount}"); await Task.Run(() => SearchEngineCommands.FullReIndex()); @@ -54,6 +39,39 @@ namespace ApplicationServices } } + private static async Task> scanAccountsAsync(ILoginCallback callback, Account[] accounts) + { + var tasks = accounts.Select(account => scanAccountAsync(callback, account)).ToList(); + var arrayOfLists = await Task.WhenAll(tasks); + var importItems = arrayOfLists.SelectMany(a => a).ToList(); + return importItems; + } + + private static async Task> scanAccountAsync(ILoginCallback callback, Account account) + { + Dinah.Core.ArgumentValidator.EnsureNotNull(account, nameof(account)); + + Log.Logger.Information("ImportLibraryAsync. {@DebugInfo}", new + { + account.AccountName, + account.AccountId, + LocaleName = account.Locale?.Name, + }); + + var dtoItems = await AudibleApiActions.GetAllLibraryItemsAsync(account, callback); + return dtoItems.Select(d => new ImportItem { DtoItem = d, Account = account }).ToList(); + } + + private static async Task getNewCountAsync(List importItems) + { + using var context = DbContexts.GetContext(); + var libraryImporter = new LibraryImporter(context); + var newCount = await Task.Run(() => libraryImporter.Import(importItems)); + context.SaveChanges(); + + return newCount; + } + public static int UpdateTags(this LibationContext context, Book book, string newTags) { try diff --git a/InternalUtilities/UNTESTED/AudibleApiStorage.cs b/InternalUtilities/UNTESTED/AudibleApiStorage.cs index 3afd17fa..c871d66a 100644 --- a/InternalUtilities/UNTESTED/AudibleApiStorage.cs +++ b/InternalUtilities/UNTESTED/AudibleApiStorage.cs @@ -1,10 +1,8 @@ using System; using System.IO; using System.Linq; -using AudibleApi; using FileManager; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace InternalUtilities { @@ -19,13 +17,6 @@ namespace InternalUtilities _ = new AccountsSettingsPersister(new AccountsSettings(), AccountsSettingsFile); } - // convenience for for tests and demos. don't use in production Libation - public static string TEST_GetFirstIdentityTokensJsonPath() - => TEST_GetFirstAccount().GetIdentityTokensJsonPath(); - // convenience for for tests and demos. don't use in production Libation - public static Account TEST_GetFirstAccount() - => GetPersistentAccountsSettings().GetAll().FirstOrDefault(); - public static AccountsSettings GetPersistentAccountsSettings() => GetAccountsSettingsPersister().AccountsSettings; public static AccountsSettingsPersister GetAccountsSettingsPersister() => new AccountsSettingsPersister(AccountsSettingsFile); diff --git a/LibationLauncher/LibationLauncher.csproj b/LibationLauncher/LibationLauncher.csproj index 2a6f68f1..869f6f69 100644 --- a/LibationLauncher/LibationLauncher.csproj +++ b/LibationLauncher/LibationLauncher.csproj @@ -13,7 +13,7 @@ win-x64 - 3.1.12.288 + 3.1.12.291 diff --git a/LibationWinForms/UNTESTED/Dialogs/IndexLibraryDialog.cs b/LibationWinForms/UNTESTED/Dialogs/IndexLibraryDialog.cs index 33adc5e8..548094f6 100644 --- a/LibationWinForms/UNTESTED/Dialogs/IndexLibraryDialog.cs +++ b/LibationWinForms/UNTESTED/Dialogs/IndexLibraryDialog.cs @@ -1,31 +1,43 @@ using System; using System.Windows.Forms; using ApplicationServices; +using InternalUtilities; using LibationWinForms.Login; namespace LibationWinForms.Dialogs { public partial class IndexLibraryDialog : Form { + private Account[] _accounts { get; } + public int NewBooksAdded { get; private set; } public int TotalBooksProcessed { get; private set; } - public IndexLibraryDialog() + public IndexLibraryDialog(params Account[] accounts) { + _accounts = accounts; InitializeComponent(); this.Shown += IndexLibraryDialog_Shown; } private async void IndexLibraryDialog_Shown(object sender, EventArgs e) { - try + if (_accounts != null && _accounts.Length > 0) { - (TotalBooksProcessed, NewBooksAdded) = await LibraryCommands.ImportAccountAsync(InternalUtilities.AudibleApiStorage.TEST_GetFirstAccount(), new WinformResponder()); - } - catch - { - var msg = "Error importing library. Please try again. If this still happens after 2 or 3 tries, stop and contact administrator"; - MessageBox.Show(msg, "Error importing library", MessageBoxButtons.OK, MessageBoxIcon.Error); + this.label1.Text + = (_accounts.Length == 1) + ? "Scanning Audible library. This may take a few minutes." + : $"Scanning Audible library: {_accounts.Length} accounts. This may take a few minutes per account."; + + try + { + (TotalBooksProcessed, NewBooksAdded) = await LibraryCommands.ImportAccountAsync(new WinformResponder(), _accounts); + } + catch + { + var msg = "Error importing library. Please try again. If this still happens after 2 or 3 tries, stop and contact administrator"; + MessageBox.Show(msg, "Error importing library", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } this.Close(); diff --git a/LibationWinForms/UNTESTED/Form1.cs b/LibationWinForms/UNTESTED/Form1.cs index c62f02f0..b4c17068 100644 --- a/LibationWinForms/UNTESTED/Form1.cs +++ b/LibationWinForms/UNTESTED/Form1.cs @@ -239,8 +239,34 @@ namespace LibationWinForms } private void scanLibraryToolStripMenuItem_Click(object sender, EventArgs e) + { + var firstAccount = AudibleApiStorage.GetPersistentAccountsSettings().GetAll().FirstOrDefault(); + scanLibraries(firstAccount); + } + + private void scanLibraryOfAllAccountsToolStripMenuItem_Click(object sender, EventArgs e) + { + var allAccounts = AudibleApiStorage.GetPersistentAccountsSettings().GetAll(); + scanLibraries(allAccounts); + } + + private void scanLibraryOfSomeAccountsToolStripMenuItem_Click(object sender, EventArgs e) + { + using var scanAccountsDialog = new ScanAccountsDialog(); + + if (scanAccountsDialog.ShowDialog() != DialogResult.OK) + return; + + if (!scanAccountsDialog.CheckedAccounts.Any()) + return; + + scanLibraries(scanAccountsDialog.CheckedAccounts); + } + + private void scanLibraries(IEnumerable accounts) => scanLibraries(accounts.ToArray()); + private void scanLibraries(params Account[] accounts) { - using var dialog = new IndexLibraryDialog(); + using var dialog = new IndexLibraryDialog(accounts); dialog.ShowDialog(); var totalProcessed = dialog.TotalBooksProcessed; @@ -251,25 +277,6 @@ namespace LibationWinForms if (totalProcessed > 0) reloadGrid(); } - -private void scanLibraryOfAllAccountsToolStripMenuItem_Click(object sender, EventArgs e) -{ -} - -private void scanLibraryOfSomeAccountsToolStripMenuItem_Click(object sender, EventArgs e) -{ - using var scanAccountsDialog = new ScanAccountsDialog(); - - if (scanAccountsDialog.ShowDialog() != DialogResult.OK) - return; - - if (!scanAccountsDialog.CheckedAccounts.Any()) - return; - - var checkedAccounts = scanAccountsDialog.CheckedAccounts; - - -} #endregion #region liberate menu