diff --git a/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs b/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs index ff8f6a2e6..c33b9d549 100644 --- a/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs +++ b/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs @@ -174,11 +174,21 @@ namespace MediaBrowser.Providers.TV /// Task. private async Task AddMissingEpisodes(List series, string seriesDataPath, IEnumerable> episodeLookup, CancellationToken cancellationToken) { - var existingEpisodes = series.SelectMany(s => s.RecursiveChildren.OfType()).ToList(); + var existingEpisodes = (from s in series + let seasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1) + from c in s.RecursiveChildren.OfType() + select new Tuple((c.ParentIndexNumber ?? 0) + seasonOffset, c)) + .ToList(); + + var lookup = episodeLookup as IList> ?? episodeLookup.ToList(); + + var seasonCounts = (from e in lookup + group e by e.Item1 into g select g) + .ToDictionary(g => g.Key, g => g.Count()); var hasChanges = false; - foreach (var tuple in episodeLookup) + foreach (var tuple in lookup) { if (tuple.Item1 <= 0) { @@ -192,7 +202,7 @@ namespace MediaBrowser.Providers.TV continue; } - var existingEpisode = GetExistingEpisode(existingEpisodes, tuple); + var existingEpisode = GetExistingEpisode(existingEpisodes, seasonCounts, tuple); if (existingEpisode != null) { @@ -208,13 +218,13 @@ namespace MediaBrowser.Providers.TV var now = DateTime.UtcNow; var targetSeries = DetermineAppropriateSeries(series, tuple.Item1); + var seasonOffset = TvdbSeriesProvider.GetSeriesOffset(targetSeries.ProviderIds) ?? ((targetSeries.AnimeSeriesIndex ?? 1) - 1); if (airDate.Value < now) { // tvdb has a lot of nearly blank episodes _logger.Info("Creating virtual missing episode {0} {1}x{2}", targetSeries.Name, tuple.Item1, tuple.Item2); - - await AddEpisode(targetSeries, tuple.Item1, tuple.Item2, cancellationToken).ConfigureAwait(false); + await AddEpisode(targetSeries, tuple.Item1 - seasonOffset, tuple.Item2, cancellationToken).ConfigureAwait(false); hasChanges = true; } @@ -222,8 +232,7 @@ namespace MediaBrowser.Providers.TV { // tvdb has a lot of nearly blank episodes _logger.Info("Creating virtual unaired episode {0} {1}x{2}", targetSeries.Name, tuple.Item1, tuple.Item2); - - await AddEpisode(targetSeries, tuple.Item1, tuple.Item2, cancellationToken).ConfigureAwait(false); + await AddEpisode(targetSeries, tuple.Item1 - seasonOffset, tuple.Item2, cancellationToken).ConfigureAwait(false); hasChanges = true; } @@ -232,40 +241,48 @@ namespace MediaBrowser.Providers.TV return hasChanges; } - private Series DetermineAppropriateSeries(List series, int seasonNumber) + private Series DetermineAppropriateSeries(IEnumerable series, int seasonNumber) { - return series.FirstOrDefault(s => s.RecursiveChildren.OfType().Any(season => season.IndexNumber == seasonNumber)) ?? - series.FirstOrDefault(s => s.RecursiveChildren.OfType().Any(season => season.IndexNumber == 1)) ?? - series.OrderBy(s => s.RecursiveChildren.OfType().Select(season => season.IndexNumber).Min()).First(); - } + var seriesAndOffsets = series.Select(s => new { Series = s, SeasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1) }).ToList(); + var bestMatch = seriesAndOffsets.FirstOrDefault(s => s.Series.RecursiveChildren.OfType().Any(season => (season.IndexNumber + s.SeasonOffset) == seasonNumber)) ?? + seriesAndOffsets.FirstOrDefault(s => s.Series.RecursiveChildren.OfType().Any(season => (season.IndexNumber + s.SeasonOffset) == 1)) ?? + seriesAndOffsets.OrderBy(s => s.Series.RecursiveChildren.OfType().Select(season => season.IndexNumber + s.SeasonOffset).Min()).First(); + + return bestMatch.Series; + } + /// /// Removes the virtual entry after a corresponding physical version has been added /// private async Task RemoveObsoleteOrMissingEpisodes(IEnumerable series, IEnumerable> episodeLookup, CancellationToken cancellationToken) { - var existingEpisodes = series.SelectMany(s => s.RecursiveChildren.OfType()).ToList(); + var existingEpisodes = (from s in series + let seasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1) + from c in s.RecursiveChildren.OfType() + select new { SeasonOffset = seasonOffset, Episode = c }) + .ToList(); var physicalEpisodes = existingEpisodes - .Where(i => i.LocationType != LocationType.Virtual) + .Where(i => i.Episode.LocationType != LocationType.Virtual) .ToList(); var virtualEpisodes = existingEpisodes - .Where(i => i.LocationType == LocationType.Virtual) + .Where(i => i.Episode.LocationType == LocationType.Virtual) .ToList(); var episodesToRemove = virtualEpisodes .Where(i => { - if (i.IndexNumber.HasValue && i.ParentIndexNumber.HasValue) + if (i.Episode.IndexNumber.HasValue && i.Episode.ParentIndexNumber.HasValue) { - var seasonNumber = i.ParentIndexNumber.Value; - var episodeNumber = i.IndexNumber.Value; + var seasonNumber = i.Episode.ParentIndexNumber.Value + i.SeasonOffset; + var episodeNumber = i.Episode.IndexNumber.Value; // If there's a physical episode with the same season and episode number, delete it if (physicalEpisodes.Any(p => - p.ParentIndexNumber.HasValue && p.ParentIndexNumber.Value == seasonNumber && - p.ContainsEpisodeNumber(episodeNumber))) + p.Episode.ParentIndexNumber.HasValue && (p.Episode.ParentIndexNumber.Value + p.SeasonOffset) == seasonNumber && + p.Episode.ContainsEpisodeNumber(episodeNumber))) { return true; } @@ -285,7 +302,7 @@ namespace MediaBrowser.Providers.TV var hasChanges = false; - foreach (var episodeToRemove in episodesToRemove) + foreach (var episodeToRemove in episodesToRemove.Select(e => e.Episode)) { _logger.Info("Removing missing/unaired episode {0} {1}x{2}", episodeToRemove.Series.Name, episodeToRemove.ParentIndexNumber, episodeToRemove.IndexNumber); @@ -306,25 +323,29 @@ namespace MediaBrowser.Providers.TV /// Task{System.Boolean}. private async Task RemoveObsoleteOrMissingSeasons(IEnumerable series, IEnumerable> episodeLookup, CancellationToken cancellationToken) { - var existingSeasons = series.SelectMany(s => s.Children.OfType()).ToList(); + var existingSeasons = (from s in series + let seasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1) + from c in s.Children.OfType() + select new { SeasonOffset = seasonOffset, Season = c }) + .ToList(); var physicalSeasons = existingSeasons - .Where(i => i.LocationType != LocationType.Virtual) + .Where(i => i.Season.LocationType != LocationType.Virtual) .ToList(); var virtualSeasons = existingSeasons - .Where(i => i.LocationType == LocationType.Virtual) + .Where(i => i.Season.LocationType == LocationType.Virtual) .ToList(); var seasonsToRemove = virtualSeasons .Where(i => { - if (i.IndexNumber.HasValue) + if (i.Season.IndexNumber.HasValue) { - var seasonNumber = i.IndexNumber.Value; + var seasonNumber = i.Season.IndexNumber.Value + i.SeasonOffset; // If there's a physical season with the same number, delete it - if (physicalSeasons.Any(p => p.IndexNumber.HasValue && p.IndexNumber.Value == seasonNumber)) + if (physicalSeasons.Any(p => p.Season.IndexNumber.HasValue && (p.Season.IndexNumber.Value + p.SeasonOffset) == seasonNumber)) { return true; } @@ -344,7 +365,7 @@ namespace MediaBrowser.Providers.TV var hasChanges = false; - foreach (var seasonToRemove in seasonsToRemove) + foreach (var seasonToRemove in seasonsToRemove.Select(s => s.Season)) { _logger.Info("Removing virtual season {0} {1}", seasonToRemove.Series.Name, seasonToRemove.IndexNumber); @@ -417,9 +438,7 @@ namespace MediaBrowser.Providers.TV await series.AddChild(season, cancellationToken).ConfigureAwait(false); - await season.RefreshMetadata(new MetadataRefreshOptions - { - }, cancellationToken).ConfigureAwait(false); + await season.RefreshMetadata(new MetadataRefreshOptions(), cancellationToken).ConfigureAwait(false); return season; } @@ -428,12 +447,37 @@ namespace MediaBrowser.Providers.TV /// Gets the existing episode. /// /// The existing episodes. + /// /// The tuple. /// Episode. - private Episode GetExistingEpisode(IEnumerable existingEpisodes, Tuple tuple) + private Episode GetExistingEpisode(IList> existingEpisodes, Dictionary seasonCounts, Tuple tuple) + { + var s = tuple.Item1; + var e = tuple.Item2; + + while (true) + { + var episode = GetExistingEpisode(existingEpisodes, s, e); + if (episode != null) + return episode; + + s--; + + if (seasonCounts.ContainsKey(s)) + e += seasonCounts[s]; + else + break; + } + + return null; + } + + private static Episode GetExistingEpisode(IEnumerable> existingEpisodes, int season, int episode) { return existingEpisodes - .FirstOrDefault(i => (i.ParentIndexNumber ?? -1) == tuple.Item1 && i.ContainsEpisodeNumber(tuple.Item2)); + .Where(i => i.Item1 == season && i.Item2.ContainsEpisodeNumber(episode)) + .Select(i => i.Item2) + .FirstOrDefault(); } /// diff --git a/MediaBrowser.Providers/TV/SeriesPostScanTask.cs b/MediaBrowser.Providers/TV/SeriesPostScanTask.cs index 8c9b8672c..d350d2fe4 100644 --- a/MediaBrowser.Providers/TV/SeriesPostScanTask.cs +++ b/MediaBrowser.Providers/TV/SeriesPostScanTask.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Controller.Configuration; +using System.Collections.Generic; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; @@ -11,6 +12,11 @@ using System.Threading.Tasks; namespace MediaBrowser.Providers.TV { + class SeriesGroup : List, IGrouping + { + public string Key { get; set; } + } + class SeriesPostScanTask : ILibraryPostScanTask, IHasOrder { /// @@ -39,11 +45,7 @@ namespace MediaBrowser.Providers.TV .OfType() .ToList(); - var seriesGroups = from series in seriesList - let tvdbId = series.GetProviderId(MetadataProviders.Tvdb) - where !string.IsNullOrEmpty(tvdbId) - group series by tvdbId into g - select g; + var seriesGroups = FindSeriesGroups(seriesList).Where(g => !string.IsNullOrEmpty(g.Key)).ToList(); await new MissingEpisodeProvider(_logger, _config, _libraryManager).Run(seriesGroups, cancellationToken).ConfigureAwait(false); @@ -84,6 +86,51 @@ namespace MediaBrowser.Providers.TV } } + private IEnumerable> FindSeriesGroups(List seriesList) + { + var links = seriesList.ToDictionary(s => s, s => seriesList.Where(c => c != s && ShareProviderId(s, c)).ToList()); + + var visited = new HashSet(); + + foreach (var series in seriesList) + { + if (!visited.Contains(series)) + { + var group = new SeriesGroup(); + FindAllLinked(series, visited, links, group); + + group.Key = group.Select(s => s.GetProviderId(MetadataProviders.Tvdb)).FirstOrDefault(id => !string.IsNullOrEmpty(id)); + + yield return group; + } + } + } + + private void FindAllLinked(Series series, HashSet visited, IDictionary> linksMap, List results) + { + results.Add(series); + visited.Add(series); + + var links = linksMap[series]; + + foreach (var s in links) + { + if (!visited.Contains(s)) + { + FindAllLinked(s, visited, linksMap, results); + } + } + } + + private bool ShareProviderId(Series a, Series b) + { + return a.ProviderIds.Any(id => + { + string value; + return b.ProviderIds.TryGetValue(id.Key, out value) && id.Value == value; + }); + } + public int Order { get diff --git a/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs b/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs index a76e10111..f9ebaddf8 100644 --- a/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs +++ b/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs @@ -25,7 +25,8 @@ namespace MediaBrowser.Providers.TV { public class TvdbSeriesProvider : IRemoteMetadataProvider, IHasOrder { - internal const string TvdbSeriesOffset = "TvdbSeriesOffset"; + private const string TvdbSeriesOffset = "TvdbSeriesOffset"; + private const string TvdbSeriesOffsetFormat = "{0}-{1}"; internal readonly SemaphoreSlim TvDbResourcePool = new SemaphoreSlim(2, 2); internal static TvdbSeriesProvider Current { get; private set; } @@ -109,17 +110,22 @@ namespace MediaBrowser.Providers.TV return; var offset = info.AnimeSeriesIndex - index; - series.SetProviderId(TvdbSeriesOffset, offset.ToString()); + var id = string.Format(TvdbSeriesOffsetFormat, series.GetProviderId(MetadataProviders.Tvdb), offset); + series.SetProviderId(TvdbSeriesOffset, id); } internal static int? GetSeriesOffset(Dictionary seriesProviderIds) { - string offsetString; - if (!seriesProviderIds.TryGetValue(TvdbSeriesOffset, out offsetString)) + string idString; + if (!seriesProviderIds.TryGetValue(TvdbSeriesOffset, out idString)) + return null; + + var parts = idString.Split('-'); + if (parts.Length < 2) return null; int offset; - if (int.TryParse(offsetString, out offset)) + if (int.TryParse(parts[1], out offset)) return offset; return null;