From f4fd66e26e41ebfa1289034ea3f83f4348d588c6 Mon Sep 17 00:00:00 2001 From: rlauuzo <46294892+rlauuzo@users.noreply.github.com> Date: Sun, 29 Sep 2024 21:46:46 +0200 Subject: [PATCH] Handle in-season specials as part of the season (#314) Co-authored-by: rlauu <46294892+rlauu@users.noreply.github.com> Co-authored-by: Kilian von Pflugk --- .../Manager/QueueManager.cs | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Manager/QueueManager.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Manager/QueueManager.cs index 6d2689a..9ddbea6 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Manager/QueueManager.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Manager/QueueManager.cs @@ -50,20 +50,15 @@ public class QueueManager(ILogger logger, ILibraryManager libraryM _logger.LogInformation("Running enqueue of items in library {Name}", folder.Name); + // Some virtual folders don't have a proper item id. + if (!Guid.TryParse(folder.ItemId, out var folderId)) + { + continue; + } + try { - foreach (var location in folder.Locations) - { - var item = _libraryManager.FindByPath(location, true); - - if (item is null) - { - _logger.LogWarning("Unable to find linked item at path {0}", location); - continue; - } - - QueueLibraryContents(item.Id); - } + QueueLibraryContents(folderId); } catch (Exception ex) { @@ -126,7 +121,7 @@ public class QueueManager(ILogger logger, ILibraryManager libraryM { // Order by series name, season, and then episode number so that status updates are logged in order ParentId = id, - OrderBy = [(ItemSortBy.SeriesSortName, SortOrder.Ascending), (ItemSortBy.ParentIndexNumber, SortOrder.Ascending), (ItemSortBy.IndexNumber, SortOrder.Ascending),], + OrderBy = [(ItemSortBy.SeriesSortName, SortOrder.Ascending), (ItemSortBy.ParentIndexNumber, SortOrder.Descending), (ItemSortBy.IndexNumber, SortOrder.Ascending),], IncludeItemTypes = [BaseItemKind.Episode], Recursive = true, IsVirtualItem = false @@ -172,10 +167,11 @@ public class QueueManager(ILogger logger, ILibraryManager libraryM } // Allocate a new list for each new season - if (!_queuedEpisodes.TryGetValue(episode.SeasonId, out var seasonEpisodes)) + var seasonId = GetSeasonId(episode); + if (!_queuedEpisodes.TryGetValue(seasonId, out var seasonEpisodes)) { seasonEpisodes = []; - _queuedEpisodes[episode.SeasonId] = seasonEpisodes; + _queuedEpisodes[seasonId] = seasonEpisodes; } if (seasonEpisodes.Any(e => e.EpisodeId == episode.Id)) @@ -214,6 +210,24 @@ public class QueueManager(ILogger logger, ILibraryManager libraryM pluginInstance.TotalQueued++; } + private Guid GetSeasonId(Episode episode) + { + if (episode.ParentIndexNumber == 0 && episode.AiredSeasonNumber != 0) // In-season special + { + foreach (var kvp in _queuedEpisodes) + { + var first = kvp.Value.FirstOrDefault(); + if (first?.SeriesId == episode.SeriesId && + first.SeasonNumber == episode.AiredSeasonNumber) + { + return kvp.Key; + } + } + } + + return episode.SeasonId; + } + /// /// Verify that a collection of queued media items still exist in Jellyfin and in storage. /// This is done to ensure that we don't analyze items that were deleted between the call to GetMediaItems() and popping them from the queue.