diff --git a/MediaBrowser.Api/TvShowsService.cs b/MediaBrowser.Api/TvShowsService.cs index c4c603f91..aa0485d57 100644 --- a/MediaBrowser.Api/TvShowsService.cs +++ b/MediaBrowser.Api/TvShowsService.cs @@ -456,7 +456,16 @@ namespace MediaBrowser.Api throw new ResourceNotFoundException("No series exists with Id " + request.Id); } - episodes = series.GetEpisodes(user, request.Season.Value); + var season = series.GetSeasons(user).FirstOrDefault(i => i.IndexNumber == request.Season.Value); + + if (season == null) + { + episodes = new List(); + } + else + { + episodes = series.GetEpisodes(user, season); + } } else { diff --git a/MediaBrowser.Controller/Entities/TV/Season.cs b/MediaBrowser.Controller/Entities/TV/Season.cs index 68170884c..7fa1b55de 100644 --- a/MediaBrowser.Controller/Entities/TV/Season.cs +++ b/MediaBrowser.Controller/Entities/TV/Season.cs @@ -205,7 +205,7 @@ namespace MediaBrowser.Controller.Entities.TV if (IndexNumber.HasValue && series != null) { - return series.GetEpisodes(user, IndexNumber.Value, includeMissingEpisodes, includeVirtualUnairedEpisodes); + return series.GetEpisodes(user, this, includeMissingEpisodes, includeVirtualUnairedEpisodes); } var episodes = GetRecursiveChildren(user) diff --git a/MediaBrowser.Controller/Entities/TV/Series.cs b/MediaBrowser.Controller/Entities/TV/Series.cs index 17fc0c0d2..6c499f618 100644 --- a/MediaBrowser.Controller/Entities/TV/Series.cs +++ b/MediaBrowser.Controller/Entities/TV/Series.cs @@ -338,11 +338,11 @@ namespace MediaBrowser.Controller.Entities.TV progress.Report(100); } - public IEnumerable GetEpisodes(User user, int seasonNumber) + public IEnumerable GetEpisodes(User user, Season season) { var config = user.Configuration; - return GetEpisodes(user, seasonNumber, config.DisplayMissingEpisodes, config.DisplayUnairedEpisodes); + return GetEpisodes(user, season, config.DisplayMissingEpisodes, config.DisplayUnairedEpisodes); } private bool EnablePooling() @@ -350,7 +350,7 @@ namespace MediaBrowser.Controller.Entities.TV return false; } - public IEnumerable GetEpisodes(User user, int seasonNumber, bool includeMissingEpisodes, bool includeVirtualUnairedEpisodes) + public IEnumerable GetEpisodes(User user, Season parentSeason, bool includeMissingEpisodes, bool includeVirtualUnairedEpisodes) { IEnumerable episodes; @@ -388,7 +388,7 @@ namespace MediaBrowser.Controller.Entities.TV }).Cast(); } - episodes = FilterEpisodesBySeason(episodes, seasonNumber, DisplaySpecialsWithSeasons); + episodes = FilterEpisodesBySeason(episodes, parentSeason, DisplaySpecialsWithSeasons); if (!includeMissingEpisodes) { @@ -399,7 +399,7 @@ namespace MediaBrowser.Controller.Entities.TV episodes = episodes.Where(i => !i.IsVirtualUnaired); } - var sortBy = seasonNumber == 0 ? ItemSortBy.SortName : ItemSortBy.AiredEpisodeOrder; + var sortBy = (parentSeason.IndexNumber ?? -1) == 0 ? ItemSortBy.SortName : ItemSortBy.AiredEpisodeOrder; return LibraryManager.Sort(episodes, user, new[] { sortBy }, SortOrder.Ascending) .Cast(); @@ -408,10 +408,6 @@ namespace MediaBrowser.Controller.Entities.TV /// /// Filters the episodes by season. /// - /// The episodes. - /// The season number. - /// if set to true [include specials]. - /// IEnumerable{Episode}. public static IEnumerable FilterEpisodesBySeason(IEnumerable episodes, int seasonNumber, bool includeSpecials) { if (!includeSpecials || seasonNumber < 1) @@ -434,6 +430,46 @@ namespace MediaBrowser.Controller.Entities.TV }); } + /// + /// Filters the episodes by season. + /// + public static IEnumerable FilterEpisodesBySeason(IEnumerable episodes, Season parentSeason, bool includeSpecials) + { + var seasonNumber = parentSeason.IndexNumber; + if (!includeSpecials || (seasonNumber.HasValue && seasonNumber.Value == 0)) + { + var seasonPresentationKey = parentSeason.PresentationUniqueKey; + + return episodes.Where(i => + { + if ((i.ParentIndexNumber ?? -1) == seasonNumber) + { + return true; + } + + var season = i.Season; + return season != null && string.Equals(season.PresentationUniqueKey, seasonPresentationKey, StringComparison.OrdinalIgnoreCase); + }); + } + else + { + var seasonPresentationKey = parentSeason.PresentationUniqueKey; + + return episodes.Where(episode => + { + var currentSeasonNumber = episode.AiredSeasonNumber; + + if (currentSeasonNumber.HasValue && seasonNumber.HasValue && currentSeasonNumber.Value == seasonNumber.Value) + { + return true; + } + + var season = episode.Season; + return season != null && string.Equals(season.PresentationUniqueKey, seasonPresentationKey, StringComparison.OrdinalIgnoreCase); + }); + } + } + protected override bool GetBlockUnratedValue(UserPolicy config) { return config.BlockUnratedItems.Contains(UnratedItem.Series);