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 class LibraryCommands
{ {
// public static async Task<(int totalCount, int newCount)> ImportAccountsAsync(IEnumerable<Account> accounts, ILoginCallback callback) public static async Task<(int totalCount, int newCount)> ImportAccountAsync(ILoginCallback callback, params Account[] accounts)
// {
////throw new NotImplementedException();
//// foreach (var account in accounts)
//// {
//// }
// }
public static async Task<(int totalCount, int newCount)> ImportAccountAsync(Account account, ILoginCallback callback)
{ {
if (accounts is null || accounts.Length == 0)
return (0, 0);
try try
{ {
Log.Logger.Information("ImportLibraryAsync. {@DebugInfo}", new var importItems = await scanAccountsAsync(callback, accounts);
{
account.AccountName,
account.AccountId,
LocaleName = account.Locale.Name,
});
var dtoItems = await AudibleApiActions.GetAllLibraryItemsAsync(account, callback); var totalCount = importItems.Count;
var items = dtoItems.Select(d => new ImportItem { DtoItem = d, Account = account }).ToList();
var totalCount = items.Count;
Log.Logger.Information($"GetAllLibraryItems: Total count {totalCount}"); Log.Logger.Information($"GetAllLibraryItems: Total count {totalCount}");
using var context = DbContexts.GetContext(); var newCount = await getNewCountAsync(importItems);
var libraryImporter = new LibraryImporter(context);
var newCount = await Task.Run(() => libraryImporter.Import(items));
context.SaveChanges();
Log.Logger.Information($"Import: New count {newCount}"); Log.Logger.Information($"Import: New count {newCount}");
await Task.Run(() => SearchEngineCommands.FullReIndex()); 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) public static int UpdateTags(this LibationContext context, Book book, string newTags)
{ {
try try

View File

@ -1,10 +1,8 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using AudibleApi;
using FileManager; using FileManager;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace InternalUtilities namespace InternalUtilities
{ {
@ -19,13 +17,6 @@ namespace InternalUtilities
_ = new AccountsSettingsPersister(new AccountsSettings(), AccountsSettingsFile); _ = 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 AccountsSettings GetPersistentAccountsSettings() => GetAccountsSettingsPersister().AccountsSettings;
public static AccountsSettingsPersister GetAccountsSettingsPersister() => new AccountsSettingsPersister(AccountsSettingsFile); public static AccountsSettingsPersister GetAccountsSettingsPersister() => new AccountsSettingsPersister(AccountsSettingsFile);

View File

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

View File

@ -1,31 +1,43 @@
using System; using System;
using System.Windows.Forms; using System.Windows.Forms;
using ApplicationServices; using ApplicationServices;
using InternalUtilities;
using LibationWinForms.Login; using LibationWinForms.Login;
namespace LibationWinForms.Dialogs namespace LibationWinForms.Dialogs
{ {
public partial class IndexLibraryDialog : Form public partial class IndexLibraryDialog : Form
{ {
private Account[] _accounts { get; }
public int NewBooksAdded { get; private set; } public int NewBooksAdded { get; private set; }
public int TotalBooksProcessed { get; private set; } public int TotalBooksProcessed { get; private set; }
public IndexLibraryDialog() public IndexLibraryDialog(params Account[] accounts)
{ {
_accounts = accounts;
InitializeComponent(); InitializeComponent();
this.Shown += IndexLibraryDialog_Shown; this.Shown += IndexLibraryDialog_Shown;
} }
private async void IndexLibraryDialog_Shown(object sender, EventArgs e) 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()); this.label1.Text
} = (_accounts.Length == 1)
catch ? "Scanning Audible library. This may take a few minutes."
{ : $"Scanning Audible library: {_accounts.Length} accounts. This may take a few minutes per account.";
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); 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(); this.Close();

View File

@ -239,8 +239,34 @@ namespace LibationWinForms
} }
private void scanLibraryToolStripMenuItem_Click(object sender, EventArgs e) 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<Account> accounts) => scanLibraries(accounts.ToArray());
private void scanLibraries(params Account[] accounts)
{ {
using var dialog = new IndexLibraryDialog(); using var dialog = new IndexLibraryDialog(accounts);
dialog.ShowDialog(); dialog.ShowDialog();
var totalProcessed = dialog.TotalBooksProcessed; var totalProcessed = dialog.TotalBooksProcessed;
@ -251,25 +277,6 @@ namespace LibationWinForms
if (totalProcessed > 0) if (totalProcessed > 0)
reloadGrid(); 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 #endregion
#region liberate menu #region liberate menu