This commit is contained in:
Luke Pulverenti 2014-03-31 17:04:41 -04:00
commit 32ac5619fe
3 changed files with 141 additions and 44 deletions

View File

@ -174,11 +174,21 @@ namespace MediaBrowser.Providers.TV
/// <returns>Task.</returns> /// <returns>Task.</returns>
private async Task<bool> AddMissingEpisodes(List<Series> series, string seriesDataPath, IEnumerable<Tuple<int, int>> episodeLookup, CancellationToken cancellationToken) private async Task<bool> AddMissingEpisodes(List<Series> series, string seriesDataPath, IEnumerable<Tuple<int, int>> episodeLookup, CancellationToken cancellationToken)
{ {
var existingEpisodes = series.SelectMany(s => s.RecursiveChildren.OfType<Episode>()).ToList(); var existingEpisodes = (from s in series
let seasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1)
from c in s.RecursiveChildren.OfType<Episode>()
select new Tuple<int, Episode>((c.ParentIndexNumber ?? 0) + seasonOffset, c))
.ToList();
var lookup = episodeLookup as IList<Tuple<int, int>> ?? 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; var hasChanges = false;
foreach (var tuple in episodeLookup) foreach (var tuple in lookup)
{ {
if (tuple.Item1 <= 0) if (tuple.Item1 <= 0)
{ {
@ -192,7 +202,7 @@ namespace MediaBrowser.Providers.TV
continue; continue;
} }
var existingEpisode = GetExistingEpisode(existingEpisodes, tuple); var existingEpisode = GetExistingEpisode(existingEpisodes, seasonCounts, tuple);
if (existingEpisode != null) if (existingEpisode != null)
{ {
@ -208,13 +218,13 @@ namespace MediaBrowser.Providers.TV
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
var targetSeries = DetermineAppropriateSeries(series, tuple.Item1); var targetSeries = DetermineAppropriateSeries(series, tuple.Item1);
var seasonOffset = TvdbSeriesProvider.GetSeriesOffset(targetSeries.ProviderIds) ?? ((targetSeries.AnimeSeriesIndex ?? 1) - 1);
if (airDate.Value < now) if (airDate.Value < now)
{ {
// tvdb has a lot of nearly blank episodes // tvdb has a lot of nearly blank episodes
_logger.Info("Creating virtual missing episode {0} {1}x{2}", targetSeries.Name, tuple.Item1, tuple.Item2); _logger.Info("Creating virtual missing episode {0} {1}x{2}", targetSeries.Name, tuple.Item1, tuple.Item2);
await AddEpisode(targetSeries, tuple.Item1 - seasonOffset, tuple.Item2, cancellationToken).ConfigureAwait(false);
await AddEpisode(targetSeries, tuple.Item1, tuple.Item2, cancellationToken).ConfigureAwait(false);
hasChanges = true; hasChanges = true;
} }
@ -222,8 +232,7 @@ namespace MediaBrowser.Providers.TV
{ {
// tvdb has a lot of nearly blank episodes // tvdb has a lot of nearly blank episodes
_logger.Info("Creating virtual unaired episode {0} {1}x{2}", targetSeries.Name, tuple.Item1, tuple.Item2); _logger.Info("Creating virtual unaired episode {0} {1}x{2}", targetSeries.Name, tuple.Item1, tuple.Item2);
await AddEpisode(targetSeries, tuple.Item1 - seasonOffset, tuple.Item2, cancellationToken).ConfigureAwait(false);
await AddEpisode(targetSeries, tuple.Item1, tuple.Item2, cancellationToken).ConfigureAwait(false);
hasChanges = true; hasChanges = true;
} }
@ -232,11 +241,15 @@ namespace MediaBrowser.Providers.TV
return hasChanges; return hasChanges;
} }
private Series DetermineAppropriateSeries(List<Series> series, int seasonNumber) private Series DetermineAppropriateSeries(IEnumerable<Series> series, int seasonNumber)
{ {
return series.FirstOrDefault(s => s.RecursiveChildren.OfType<Season>().Any(season => season.IndexNumber == seasonNumber)) ?? var seriesAndOffsets = series.Select(s => new { Series = s, SeasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1) }).ToList();
series.FirstOrDefault(s => s.RecursiveChildren.OfType<Season>().Any(season => season.IndexNumber == 1)) ??
series.OrderBy(s => s.RecursiveChildren.OfType<Season>().Select(season => season.IndexNumber).Min()).First(); var bestMatch = seriesAndOffsets.FirstOrDefault(s => s.Series.RecursiveChildren.OfType<Season>().Any(season => (season.IndexNumber + s.SeasonOffset) == seasonNumber)) ??
seriesAndOffsets.FirstOrDefault(s => s.Series.RecursiveChildren.OfType<Season>().Any(season => (season.IndexNumber + s.SeasonOffset) == 1)) ??
seriesAndOffsets.OrderBy(s => s.Series.RecursiveChildren.OfType<Season>().Select(season => season.IndexNumber + s.SeasonOffset).Min()).First();
return bestMatch.Series;
} }
/// <summary> /// <summary>
@ -244,28 +257,32 @@ namespace MediaBrowser.Providers.TV
/// </summary> /// </summary>
private async Task<bool> RemoveObsoleteOrMissingEpisodes(IEnumerable<Series> series, IEnumerable<Tuple<int, int>> episodeLookup, CancellationToken cancellationToken) private async Task<bool> RemoveObsoleteOrMissingEpisodes(IEnumerable<Series> series, IEnumerable<Tuple<int, int>> episodeLookup, CancellationToken cancellationToken)
{ {
var existingEpisodes = series.SelectMany(s => s.RecursiveChildren.OfType<Episode>()).ToList(); var existingEpisodes = (from s in series
let seasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1)
from c in s.RecursiveChildren.OfType<Episode>()
select new { SeasonOffset = seasonOffset, Episode = c })
.ToList();
var physicalEpisodes = existingEpisodes var physicalEpisodes = existingEpisodes
.Where(i => i.LocationType != LocationType.Virtual) .Where(i => i.Episode.LocationType != LocationType.Virtual)
.ToList(); .ToList();
var virtualEpisodes = existingEpisodes var virtualEpisodes = existingEpisodes
.Where(i => i.LocationType == LocationType.Virtual) .Where(i => i.Episode.LocationType == LocationType.Virtual)
.ToList(); .ToList();
var episodesToRemove = virtualEpisodes var episodesToRemove = virtualEpisodes
.Where(i => .Where(i =>
{ {
if (i.IndexNumber.HasValue && i.ParentIndexNumber.HasValue) if (i.Episode.IndexNumber.HasValue && i.Episode.ParentIndexNumber.HasValue)
{ {
var seasonNumber = i.ParentIndexNumber.Value; var seasonNumber = i.Episode.ParentIndexNumber.Value + i.SeasonOffset;
var episodeNumber = i.IndexNumber.Value; var episodeNumber = i.Episode.IndexNumber.Value;
// If there's a physical episode with the same season and episode number, delete it // If there's a physical episode with the same season and episode number, delete it
if (physicalEpisodes.Any(p => if (physicalEpisodes.Any(p =>
p.ParentIndexNumber.HasValue && p.ParentIndexNumber.Value == seasonNumber && p.Episode.ParentIndexNumber.HasValue && (p.Episode.ParentIndexNumber.Value + p.SeasonOffset) == seasonNumber &&
p.ContainsEpisodeNumber(episodeNumber))) p.Episode.ContainsEpisodeNumber(episodeNumber)))
{ {
return true; return true;
} }
@ -285,7 +302,7 @@ namespace MediaBrowser.Providers.TV
var hasChanges = false; 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); _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
/// <returns>Task{System.Boolean}.</returns> /// <returns>Task{System.Boolean}.</returns>
private async Task<bool> RemoveObsoleteOrMissingSeasons(IEnumerable<Series> series, IEnumerable<Tuple<int, int>> episodeLookup, CancellationToken cancellationToken) private async Task<bool> RemoveObsoleteOrMissingSeasons(IEnumerable<Series> series, IEnumerable<Tuple<int, int>> episodeLookup, CancellationToken cancellationToken)
{ {
var existingSeasons = series.SelectMany(s => s.Children.OfType<Season>()).ToList(); var existingSeasons = (from s in series
let seasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1)
from c in s.Children.OfType<Season>()
select new { SeasonOffset = seasonOffset, Season = c })
.ToList();
var physicalSeasons = existingSeasons var physicalSeasons = existingSeasons
.Where(i => i.LocationType != LocationType.Virtual) .Where(i => i.Season.LocationType != LocationType.Virtual)
.ToList(); .ToList();
var virtualSeasons = existingSeasons var virtualSeasons = existingSeasons
.Where(i => i.LocationType == LocationType.Virtual) .Where(i => i.Season.LocationType == LocationType.Virtual)
.ToList(); .ToList();
var seasonsToRemove = virtualSeasons var seasonsToRemove = virtualSeasons
.Where(i => .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 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; return true;
} }
@ -344,7 +365,7 @@ namespace MediaBrowser.Providers.TV
var hasChanges = false; 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); _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 series.AddChild(season, cancellationToken).ConfigureAwait(false);
await season.RefreshMetadata(new MetadataRefreshOptions await season.RefreshMetadata(new MetadataRefreshOptions(), cancellationToken).ConfigureAwait(false);
{
}, cancellationToken).ConfigureAwait(false);
return season; return season;
} }
@ -428,12 +447,37 @@ namespace MediaBrowser.Providers.TV
/// Gets the existing episode. /// Gets the existing episode.
/// </summary> /// </summary>
/// <param name="existingEpisodes">The existing episodes.</param> /// <param name="existingEpisodes">The existing episodes.</param>
/// <param name="seasonCounts"></param>
/// <param name="tuple">The tuple.</param> /// <param name="tuple">The tuple.</param>
/// <returns>Episode.</returns> /// <returns>Episode.</returns>
private Episode GetExistingEpisode(IEnumerable<Episode> existingEpisodes, Tuple<int, int> tuple) private Episode GetExistingEpisode(IList<Tuple<int, Episode>> existingEpisodes, Dictionary<int, int> seasonCounts, Tuple<int, int> 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<Tuple<int, Episode>> existingEpisodes, int season, int episode)
{ {
return existingEpisodes 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();
} }
/// <summary> /// <summary>

View File

@ -1,4 +1,5 @@
using MediaBrowser.Controller.Configuration; using System.Collections.Generic;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Providers;
@ -11,6 +12,11 @@ using System.Threading.Tasks;
namespace MediaBrowser.Providers.TV namespace MediaBrowser.Providers.TV
{ {
class SeriesGroup : List<Series>, IGrouping<string, Series>
{
public string Key { get; set; }
}
class SeriesPostScanTask : ILibraryPostScanTask, IHasOrder class SeriesPostScanTask : ILibraryPostScanTask, IHasOrder
{ {
/// <summary> /// <summary>
@ -39,11 +45,7 @@ namespace MediaBrowser.Providers.TV
.OfType<Series>() .OfType<Series>()
.ToList(); .ToList();
var seriesGroups = from series in seriesList var seriesGroups = FindSeriesGroups(seriesList).Where(g => !string.IsNullOrEmpty(g.Key)).ToList();
let tvdbId = series.GetProviderId(MetadataProviders.Tvdb)
where !string.IsNullOrEmpty(tvdbId)
group series by tvdbId into g
select g;
await new MissingEpisodeProvider(_logger, _config, _libraryManager).Run(seriesGroups, cancellationToken).ConfigureAwait(false); await new MissingEpisodeProvider(_logger, _config, _libraryManager).Run(seriesGroups, cancellationToken).ConfigureAwait(false);
@ -84,6 +86,51 @@ namespace MediaBrowser.Providers.TV
} }
} }
private IEnumerable<IGrouping<string, Series>> FindSeriesGroups(List<Series> seriesList)
{
var links = seriesList.ToDictionary(s => s, s => seriesList.Where(c => c != s && ShareProviderId(s, c)).ToList());
var visited = new HashSet<Series>();
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<Series> visited, IDictionary<Series, List<Series>> linksMap, List<Series> 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 public int Order
{ {
get get

View File

@ -25,7 +25,8 @@ namespace MediaBrowser.Providers.TV
{ {
public class TvdbSeriesProvider : IRemoteMetadataProvider<Series, SeriesInfo>, IHasOrder public class TvdbSeriesProvider : IRemoteMetadataProvider<Series, SeriesInfo>, 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 readonly SemaphoreSlim TvDbResourcePool = new SemaphoreSlim(2, 2);
internal static TvdbSeriesProvider Current { get; private set; } internal static TvdbSeriesProvider Current { get; private set; }
@ -109,17 +110,22 @@ namespace MediaBrowser.Providers.TV
return; return;
var offset = info.AnimeSeriesIndex - index; 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<string, string> seriesProviderIds) internal static int? GetSeriesOffset(Dictionary<string, string> seriesProviderIds)
{ {
string offsetString; string idString;
if (!seriesProviderIds.TryGetValue(TvdbSeriesOffset, out offsetString)) if (!seriesProviderIds.TryGetValue(TvdbSeriesOffset, out idString))
return null;
var parts = idString.Split('-');
if (parts.Length < 2)
return null; return null;
int offset; int offset;
if (int.TryParse(offsetString, out offset)) if (int.TryParse(parts[1], out offset))
return offset; return offset;
return null; return null;