Merge pull request #264 from Mbucari/master

Fix for episodes with no series link
This commit is contained in:
rmcrackan 2022-05-26 20:41:55 -04:00 committed by GitHub
commit b196836fca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 20 deletions

View File

@ -131,8 +131,18 @@ namespace AudibleUtilities
{
if (item.IsEpisodes && importEpisodes)
{
//Helps to distinguish product parrents which have no content
//from children which do have content.
item.Asin = $"SERIES_{item.Asin}";
//Add the parent to the library because it contains the series
//description, series rating, and series cover art which differ
//from the individual episodes' values.
item.Series = new Series[] { new Series { Asin = item.Asin, Sequence = RelationshipToProduct.Parent, Title = item.TitleWithSubtitle } };
//Get child episodes asynchronously and await all at the end
getChildEpisodesTasks.Add(getChildEpisodesAsync(concurrencySemaphore, item));
items.Add(item);
}
else if (!item.IsEpisodes)
items.Add(item);
@ -149,7 +159,7 @@ namespace AudibleUtilities
Serilog.Log.Logger.Debug("Completed library scan.");
#if DEBUG
//System.IO.File.WriteAllText(library_json, AudibleApi.Common.Converter.ToJson(items));
//System.IO.File.WriteAllText(library_json, AudibleApi.Common.Converter.ToJson(items));
#endif
var validators = new List<IValidator>();
validators.AddRange(getValidators());
@ -175,10 +185,15 @@ namespace AudibleUtilities
var children = await getEpisodeChildrenAsync(parent);
// actual individual episode, not the parent of a series.
// for now I'm keeping it inside this method since it fits the work flow, incl. importEpisodes logic
if (!children.Any())
return new List<Item>() { parent };
{
//The parent is the only episode in the podcase series,
//so the parent is its own child.
var parentJson = parent.ToJson(parent).ToString();
var child = Item.FromJson(parentJson);
child.Asin = child.Asin.Replace("SERIES_", "");
children.Add(child);
}
foreach (var child in children)
{
@ -191,7 +206,7 @@ namespace AudibleUtilities
{
Asin = parent.Asin,
// This should properly be Single() not FirstOrDefault(), but FirstOrDefault is defensive for malformed data from audible
Sequence = parent.Relationships.FirstOrDefault(r => r.Asin == child.Asin).Sort.ToString(),
Sequence = parent.Relationships.FirstOrDefault(r => r.Asin == child.Asin)?.Sort?.ToString() ?? "0",
Title = parent.TitleWithSubtitle
}
};

View File

@ -109,5 +109,7 @@ namespace LibationWinForms.GridView
=> gridEntries.Series().FirstOrDefault(i => matchSeries.Any(s => s.Series.Name == i.Series));
public static IEnumerable<SeriesEntry> EmptySeries(this IEnumerable<GridEntry> gridEntries)
=> gridEntries.Series().Where(i => i.Children.Count == 0);
public static bool IsEpisodeChild(this LibraryBook lb) => lb.Book.ContentType == ContentType.Episode && lb.Book.SeriesLink is not null && lb.Book.SeriesLink.Any() && lb.Book.SeriesLink.First().Order != AudibleApi.Common.RelationshipToProduct.Parent;
public static bool IsEpisodeParent(this LibraryBook lb) => lb.Book.ContentType == ContentType.Episode && lb.Book.SeriesLink is not null && lb.Book.SeriesLink.Any() && lb.Book.SeriesLink.First().Order == AudibleApi.Common.RelationshipToProduct.Parent;
}
}

View File

@ -83,6 +83,8 @@ namespace LibationWinForms.GridView
#region UI display functions
public void Display()
{
try
{
// don't return early if lib size == 0. this will not update correctly if all books are removed
var lib = DbContexts.GetLibrary_Flat_NoTracking();
@ -96,6 +98,11 @@ namespace LibationWinForms.GridView
}
else
productsGrid.UpdateGrid(lib);
}
catch (Exception ex)
{
Serilog.Log.Error(ex, "Error displaying library in {0}", nameof(ProductsDisplay));
}
}

View File

@ -84,11 +84,11 @@ namespace LibationWinForms.GridView
{
var geList = dbBooks.Where(b => b.Book.ContentType is not ContentType.Episode).Select(b => new LibraryBookEntry(b)).Cast<GridEntry>().ToList();
var episodes = dbBooks.Where(b => b.Book.ContentType is ContentType.Episode).ToList();
var episodes = dbBooks.Where(b => b.IsEpisodeChild()).ToList();
foreach (var series in episodes.Select(lb => lb.Book.SeriesLink.First()).DistinctBy(s => s.Series))
foreach (var series in episodes.SelectMany(lb => lb.Book.SeriesLink).DistinctBy(s => s.Series))
{
var seriesEntry = new SeriesEntry(series, episodes.Where(lb => lb.Book.SeriesLink.First().Series == series.Book.SeriesLink.First().Series));
var seriesEntry = new SeriesEntry(series, episodes.Where(lb => lb.Book.SeriesLink.Any(s => s.Series == series.Series)));
geList.Add(seriesEntry);
geList.AddRange(seriesEntry.Children);
@ -117,7 +117,7 @@ namespace LibationWinForms.GridView
// add new to top
if (existingItem is null)
{
if (libraryBook.Book.ContentType is ContentType.Episode)
if (libraryBook.IsEpisodeChild())
{
LibraryBookEntry lbe;
//Find the series that libraryBook belongs to, if it exists
@ -148,7 +148,7 @@ namespace LibationWinForms.GridView
series.NotifyPropertyChanged();
}
else
else if (libraryBook.Book.ContentType is not ContentType.Episode)
//Add the new product
bindingList.Insert(0, new LibraryBookEntry(libraryBook));
}