Skip to content

Commit

Permalink
add delete of missing episodes in library scan
Browse files Browse the repository at this point in the history
  • Loading branch information
BobSilent committed Feb 12, 2024
1 parent 3e94909 commit 148e901
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions Jellyfin.Plugin.Tvdb/Providers/TvdbMissingEpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,20 @@ protected virtual void Dispose(bool disposing)

private static bool EpisodeExists(EpisodeBaseRecord episodeRecord, IReadOnlyList<Episode> existingEpisodes)
{
return existingEpisodes.Any(ep => ep.ContainsEpisodeNumber(episodeRecord.Number) && ep.ParentIndexNumber == episodeRecord.Number);
return existingEpisodes.Any(ep => EpisodeEquals(episodeRecord, ep));
}

private static bool EpisodeEquals(EpisodeBaseRecord episodeRecord, Episode otherEpisodeRecord)
{
return otherEpisodeRecord.ContainsEpisodeNumber(episodeRecord.Number)
&& otherEpisodeRecord.ParentIndexNumber == episodeRecord.Number;
}

/// <summary>
/// Is Metadata fetcher enabled for Series, Season or Episode.
/// </summary>
/// <param name="item">Series, Season or Episode.</param>
/// <returns>true if enabled.</returns>
private bool IsEnabledForLibrary(BaseItem item)
{
Series? series = item switch
Expand Down Expand Up @@ -202,13 +213,23 @@ private async Task HandleSeason(Season season)
var tvdbId = Convert.ToInt32(tvdbIdTxt, CultureInfo.InvariantCulture);
var allEpisodes = await GetAllEpisodes(tvdbId, season.GetPreferredMetadataLanguage()).ConfigureAwait(false);

var existingEpisodes = season.Children.OfType<Episode>().ToList();
var existingEpisodes = season.GetEpisodes().OfType<Episode>().ToList();

for (var i = 0; i < allEpisodes.Count; i++)
{
var episode = allEpisodes[i];
if (EpisodeExists(episode, existingEpisodes))
var foundEpisodes = existingEpisodes.Where(e => EpisodeEquals(episode, e)).ToList();
if (foundEpisodes.Any())
{
// So we have at least one existing episode for our episodeRecord
var physicalEpisodes = foundEpisodes.Where(e => !e.IsVirtualItem);
if (physicalEpisodes.Any())
{
// if there is a physical episode we can delete existing virtual episode entries
var virtualEpisodes = foundEpisodes.Where(e => e.IsVirtualItem).ToList();
DeleteVirtualItems(virtualEpisodes);
}

continue;
}

Expand Down Expand Up @@ -242,19 +263,31 @@ private void OnLibraryManagerItemUpdated(object? sender, ItemChangeEventArgs ite
parentIndexNumber = itemChangeEventArgs.Item.ParentIndexNumber;
}

var existingVirtualItems = GetVirtualItems(itemChangeEventArgs.Item, itemChangeEventArgs.Parent, indexNumber, parentIndexNumber);
DeleteVirtualItems(existingVirtualItems);
}

private List<BaseItem> GetVirtualItems(BaseItem item, BaseItem? parent, int? indexNumber, int? parentIndexNumber)
{
var query = new InternalItemsQuery
{
IsVirtualItem = true,
IndexNumber = indexNumber,
ParentIndexNumber = parentIndexNumber,
IncludeItemTypes = new[] { itemChangeEventArgs.Item.GetBaseItemKind() },
Parent = itemChangeEventArgs.Parent,
IncludeItemTypes = new[] { item.GetBaseItemKind() },
Parent = parent,
Recursive = true,
GroupByPresentationUniqueKey = false,
DtoOptions = new DtoOptions(true)
};

var existingVirtualItems = _libraryManager.GetItemList(query);
return existingVirtualItems;
}

private void DeleteVirtualItems<T>(List<T> existingVirtualItems)
where T : BaseItem
{
var deleteOptions = new DeleteOptions
{
DeleteFileLocation = true
Expand All @@ -263,7 +296,9 @@ private void OnLibraryManagerItemUpdated(object? sender, ItemChangeEventArgs ite
// Remove the virtual season/episode that matches the newly updated item
for (var i = 0; i < existingVirtualItems.Count; i++)
{
_libraryManager.DeleteItem(existingVirtualItems[i], deleteOptions);
var currentItem = existingVirtualItems[i];
_logger.LogDebug("Delete VirtualItem {Name} - S{Season}E{Episode}", currentItem.Name, currentItem.ParentIndexNumber, currentItem.IndexNumber);
_libraryManager.DeleteItem(currentItem, deleteOptions);
}
}

Expand Down

0 comments on commit 148e901

Please sign in to comment.