Libation 4.0 prep: full multiple account support

This commit is contained in:
Robert McRackan 2020-08-26 12:50:12 -04:00
parent 755a7338e9
commit a51e76d44d
5 changed files with 88 additions and 60 deletions

View File

@ -12,34 +12,19 @@ namespace ApplicationServices
{
public static class LibraryCommands
{
// public static async Task<(int totalCount, int newCount)> ImportAccountsAsync(IEnumerable<Account> 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<List<ImportItem>> 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<List<ImportItem>> 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<int> getNewCountAsync(List<ImportItem> 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

View File

@ -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);

View File

@ -13,7 +13,7 @@
<!-- <PublishSingleFile>true</PublishSingleFile> -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>3.1.12.288</Version>
<Version>3.1.12.291</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -1,32 +1,44 @@
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)
{
if (_accounts != null && _accounts.Length > 0)
{
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(InternalUtilities.AudibleApiStorage.TEST_GetFirstAccount(), new WinformResponder());
(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();
}

View File

@ -240,20 +240,14 @@ namespace LibationWinForms
private void scanLibraryToolStripMenuItem_Click(object sender, EventArgs e)
{
using var dialog = new IndexLibraryDialog();
dialog.ShowDialog();
var totalProcessed = dialog.TotalBooksProcessed;
var newAdded = dialog.NewBooksAdded;
MessageBox.Show($"Total processed: {totalProcessed}\r\nNew: {newAdded}");
if (totalProcessed > 0)
reloadGrid();
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)
@ -266,9 +260,22 @@ private void scanLibraryOfSomeAccountsToolStripMenuItem_Click(object sender, Eve
if (!scanAccountsDialog.CheckedAccounts.Any())
return;
var checkedAccounts = scanAccountsDialog.CheckedAccounts;
scanLibraries(scanAccountsDialog.CheckedAccounts);
}
private void scanLibraries(IEnumerable<Account> accounts) => scanLibraries(accounts.ToArray());
private void scanLibraries(params Account[] accounts)
{
using var dialog = new IndexLibraryDialog(accounts);
dialog.ShowDialog();
var totalProcessed = dialog.TotalBooksProcessed;
var newAdded = dialog.NewBooksAdded;
MessageBox.Show($"Total processed: {totalProcessed}\r\nNew: {newAdded}");
if (totalProcessed > 0)
reloadGrid();
}
#endregion