2019-01-13 21:03:10 +01:00
|
|
|
using System;
|
2014-02-17 22:35:08 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Xml;
|
2019-01-13 20:26:31 +01:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2016-10-24 04:45:23 +02:00
|
|
|
using MediaBrowser.Model.Globalization;
|
2019-01-13 20:26:31 +01:00
|
|
|
using MediaBrowser.Model.IO;
|
2019-02-09 10:10:33 +01:00
|
|
|
using MediaBrowser.Providers.TV.TheTVDB;
|
2019-01-13 20:26:31 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.TV
|
|
|
|
{
|
2016-10-27 09:58:33 +02:00
|
|
|
public class MissingEpisodeProvider
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
private readonly ILogger _logger;
|
2014-02-20 05:53:15 +01:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-12-23 04:58:14 +01:00
|
|
|
private readonly ILocalizationManager _localization;
|
2015-09-14 01:07:54 +02:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2015-01-14 05:20:30 +01:00
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2019-02-08 17:42:09 +01:00
|
|
|
private const double UnairedEpisodeThresholdDays = 2;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
public MissingEpisodeProvider(ILogger logger, IServerConfigurationManager config, ILibraryManager libraryManager, ILocalizationManager localization, IFileSystem fileSystem)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_config = config;
|
2014-02-20 05:53:15 +01:00
|
|
|
_libraryManager = libraryManager;
|
2014-12-23 04:58:14 +01:00
|
|
|
_localization = localization;
|
2015-09-14 01:07:54 +02:00
|
|
|
_fileSystem = fileSystem;
|
2014-02-17 22:35:08 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
public async Task<bool> Run(Series series, bool addNewItems, CancellationToken cancellationToken)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
var tvdbId = series.GetProviderId(MetadataProviders.Tvdb);
|
2019-02-08 14:39:01 +01:00
|
|
|
if (string.IsNullOrEmpty(tvdbId))
|
2019-02-07 23:56:57 +01:00
|
|
|
{
|
2019-02-08 14:39:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-24 19:45:52 +01:00
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
var episodes = await TvDbClientManager.Instance.GetAllEpisodesAsync(Convert.ToInt32(tvdbId), cancellationToken);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
var episodeLookup = episodes
|
2014-02-17 22:35:08 +01:00
|
|
|
.Select(i =>
|
|
|
|
{
|
2019-02-07 23:56:57 +01:00
|
|
|
DateTime.TryParse(i.FirstAired, out var firstAired);
|
2019-02-08 17:42:09 +01:00
|
|
|
var seasonNumber = i.AiredSeason.GetValueOrDefault(-1);
|
|
|
|
var episodeNumber = i.AiredEpisodeNumber.GetValueOrDefault(-1);
|
|
|
|
return (SeasonNumber: seasonNumber, EpisodeNumber: episodeNumber, firstAired: firstAired);
|
2014-02-17 22:35:08 +01:00
|
|
|
})
|
2019-02-08 17:42:09 +01:00
|
|
|
.Where(i => i.SeasonNumber != -1 && i.EpisodeNumber != -1)
|
|
|
|
.OrderBy(i => i.SeasonNumber)
|
|
|
|
.ThenBy(i => i.EpisodeNumber)
|
2014-02-17 22:35:08 +01:00
|
|
|
.ToList();
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
var allRecursiveChildren = series.GetRecursiveChildren();
|
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
var hasBadData = HasInvalidContent(allRecursiveChildren);
|
2014-04-09 04:12:17 +02:00
|
|
|
|
2016-11-22 19:22:01 +01:00
|
|
|
// Be conservative here to avoid creating missing episodes for ones they already have
|
2018-09-12 19:26:21 +02:00
|
|
|
var addMissingEpisodes = !hasBadData && _libraryManager.GetLibraryOptions(series).ImportMissingEpisodes;
|
2016-11-22 19:22:01 +01:00
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
var anySeasonsRemoved = RemoveObsoleteOrMissingSeasons(allRecursiveChildren, episodeLookup);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (anySeasonsRemoved)
|
|
|
|
{
|
|
|
|
// refresh this
|
|
|
|
allRecursiveChildren = series.GetRecursiveChildren();
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
var anyEpisodesRemoved = RemoveObsoleteOrMissingEpisodes(allRecursiveChildren, episodeLookup, addMissingEpisodes);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (anyEpisodesRemoved)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
// refresh this
|
|
|
|
allRecursiveChildren = series.GetRecursiveChildren();
|
|
|
|
}
|
2014-03-02 16:42:21 +01:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
var hasNewEpisodes = false;
|
|
|
|
|
2019-01-13 20:26:31 +01:00
|
|
|
if (addNewItems && series.IsMetadataFetcherEnabled(_libraryManager.GetLibraryOptions(series), TvdbSeriesProvider.Current.Name))
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
2019-02-07 23:56:57 +01:00
|
|
|
hasNewEpisodes = await AddMissingEpisodes(series, allRecursiveChildren, addMissingEpisodes, episodeLookup, cancellationToken)
|
2018-09-12 19:26:21 +02:00
|
|
|
.ConfigureAwait(false);
|
2014-02-17 22:35:08 +01:00
|
|
|
}
|
|
|
|
|
2015-01-14 05:20:30 +01:00
|
|
|
if (hasNewEpisodes || anySeasonsRemoved || anyEpisodesRemoved)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
return true;
|
2014-02-17 22:35:08 +01:00
|
|
|
}
|
2018-09-12 19:26:21 +02:00
|
|
|
|
|
|
|
return false;
|
2014-02-17 22:35:08 +01:00
|
|
|
}
|
|
|
|
|
2014-04-09 04:12:17 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns true if a series has any seasons or episodes without season or episode numbers
|
|
|
|
/// If this data is missing no virtual items will be added in order to prevent possible duplicates
|
|
|
|
/// </summary>
|
2019-02-07 23:56:57 +01:00
|
|
|
private bool HasInvalidContent(IList<BaseItem> allItems)
|
2014-04-09 04:12:17 +02:00
|
|
|
{
|
|
|
|
return allItems.OfType<Season>().Any(i => !i.IndexNumber.HasValue) ||
|
2014-06-01 06:11:04 +02:00
|
|
|
allItems.OfType<Episode>().Any(i =>
|
|
|
|
{
|
|
|
|
if (!i.ParentIndexNumber.HasValue)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// You could have episodes under season 0 with no number
|
|
|
|
return false;
|
|
|
|
});
|
2014-04-09 04:12:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
private async Task<bool> AddMissingEpisodes(
|
|
|
|
Series series,
|
2019-02-08 17:42:09 +01:00
|
|
|
IEnumerable<BaseItem> allItems,
|
2016-11-22 19:22:01 +01:00
|
|
|
bool addMissingEpisodes,
|
2019-02-08 17:42:09 +01:00
|
|
|
IReadOnlyCollection<(int SeasonNumber, int Episodenumber, DateTime FirstAired)> episodeLookup,
|
2014-09-13 16:10:30 +02:00
|
|
|
CancellationToken cancellationToken)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2019-02-11 19:14:24 +01:00
|
|
|
var existingEpisodes = allItems.OfType<Episode>().ToList();
|
2014-03-30 04:44:03 +02:00
|
|
|
|
2019-02-08 17:42:09 +01:00
|
|
|
var seasonCounts = episodeLookup.GroupBy(e => e.SeasonNumber).ToDictionary(g => g.Key, g => g.Count());
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
var hasChanges = false;
|
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
foreach (var tuple in episodeLookup)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2019-02-08 17:42:09 +01:00
|
|
|
if (tuple.SeasonNumber <= 0 || tuple.Episodenumber <= 0)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2019-02-08 17:42:09 +01:00
|
|
|
// Ignore episode/season zeros
|
2014-02-17 22:35:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-30 04:44:03 +02:00
|
|
|
var existingEpisode = GetExistingEpisode(existingEpisodes, seasonCounts, tuple);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
if (existingEpisode != null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-08 17:42:09 +01:00
|
|
|
var airDate = tuple.FirstAired;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
var now = DateTime.UtcNow.AddDays(0 - UnairedEpisodeThresholdDays);
|
2016-12-08 16:40:20 +01:00
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
if (airDate < now && addMissingEpisodes || airDate > now)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
|
|
|
// tvdb has a lot of nearly blank episodes
|
2019-02-08 17:42:09 +01:00
|
|
|
_logger.LogInformation("Creating virtual missing/unaired episode {0} {1}x{2}", series.Name, tuple.SeasonNumber, tuple.Episodenumber);
|
|
|
|
await AddEpisode(series, tuple.SeasonNumber, tuple.Episodenumber, cancellationToken).ConfigureAwait(false);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
hasChanges = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasChanges;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Removes the virtual entry after a corresponding physical version has been added
|
|
|
|
/// </summary>
|
2019-02-07 23:56:57 +01:00
|
|
|
private bool RemoveObsoleteOrMissingEpisodes(
|
2019-02-08 17:42:09 +01:00
|
|
|
IEnumerable<BaseItem> allRecursiveChildren,
|
|
|
|
IEnumerable<(int SeasonNumber, int EpisodeNumber, DateTime FirstAired)> episodeLookup,
|
2016-11-22 19:22:01 +01:00
|
|
|
bool allowMissingEpisodes)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2019-02-08 17:42:09 +01:00
|
|
|
var existingEpisodes = allRecursiveChildren.OfType<Episode>();
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2019-02-08 17:42:09 +01:00
|
|
|
var physicalEpisodes = new List<Episode>();
|
|
|
|
var virtualEpisodes = new List<Episode>();
|
|
|
|
foreach (var episode in existingEpisodes)
|
|
|
|
{
|
|
|
|
if (episode.LocationType == LocationType.Virtual)
|
|
|
|
{
|
|
|
|
virtualEpisodes.Add(episode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
physicalEpisodes.Add(episode);
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
var episodesToRemove = virtualEpisodes
|
|
|
|
.Where(i =>
|
|
|
|
{
|
2019-02-08 17:42:09 +01:00
|
|
|
if (!i.IndexNumber.HasValue || !i.ParentIndexNumber.HasValue)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2019-02-08 17:42:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2019-02-08 17:42:09 +01:00
|
|
|
var seasonNumber = i.ParentIndexNumber.Value;
|
|
|
|
var episodeNumber = i.IndexNumber.Value;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2019-02-08 17:42:09 +01:00
|
|
|
// 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)))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-22 19:22:01 +01:00
|
|
|
|
2019-02-08 17:42:09 +01:00
|
|
|
// If the episode no longer exists in the remote lookup, delete it
|
|
|
|
if (!episodeLookup.Any(e => e.SeasonNumber == seasonNumber && e.EpisodeNumber == episodeNumber))
|
|
|
|
{
|
|
|
|
return true;
|
2014-02-17 22:35:08 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 17:42:09 +01:00
|
|
|
// If it's missing, but not unaired, remove it
|
|
|
|
return !allowMissingEpisodes && i.IsMissingEpisode &&
|
|
|
|
(!i.PremiereDate.HasValue ||
|
|
|
|
i.PremiereDate.Value.ToLocalTime().Date.AddDays(UnairedEpisodeThresholdDays) <
|
|
|
|
DateTime.Now.Date);
|
|
|
|
});
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
var hasChanges = false;
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
foreach (var episodeToRemove in episodesToRemove)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
_libraryManager.DeleteItem(episodeToRemove, new DeleteOptions
|
2016-02-12 05:54:00 +01:00
|
|
|
{
|
|
|
|
DeleteFileLocation = true
|
2018-09-12 19:26:21 +02:00
|
|
|
}, false);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
hasChanges = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasChanges;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Removes the obsolete or missing seasons.
|
|
|
|
/// </summary>
|
2019-02-07 23:56:57 +01:00
|
|
|
/// <param name="allRecursiveChildren"></param>
|
2014-02-17 22:35:08 +01:00
|
|
|
/// <param name="episodeLookup">The episode lookup.</param>
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
2019-02-07 23:56:57 +01:00
|
|
|
private bool RemoveObsoleteOrMissingSeasons(IList<BaseItem> allRecursiveChildren,
|
2019-02-08 17:42:09 +01:00
|
|
|
IEnumerable<(int SeasonNumber, int EpisodeNumber, DateTime FirstAired)> episodeLookup)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
var existingSeasons = allRecursiveChildren.OfType<Season>().ToList();
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2019-02-08 17:42:09 +01:00
|
|
|
var physicalSeasons = new List<Season>();
|
|
|
|
var virtualSeasons = new List<Season>();
|
|
|
|
foreach (var season in existingSeasons)
|
|
|
|
{
|
|
|
|
if (season.LocationType == LocationType.Virtual)
|
|
|
|
{
|
|
|
|
virtualSeasons.Add(season);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
physicalSeasons.Add(season);
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
var allEpisodes = allRecursiveChildren.OfType<Episode>().ToList();
|
|
|
|
|
2014-02-17 22:35:08 +01:00
|
|
|
var seasonsToRemove = virtualSeasons
|
|
|
|
.Where(i =>
|
|
|
|
{
|
2017-02-18 09:32:17 +01:00
|
|
|
if (i.IndexNumber.HasValue)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2017-02-18 09:32:17 +01:00
|
|
|
var seasonNumber = i.IndexNumber.Value;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
// If there's a physical season with the same number, delete it
|
2019-02-08 17:42:09 +01:00
|
|
|
if (physicalSeasons.Any(p => p.IndexNumber.HasValue && p.IndexNumber.Value == seasonNumber && string.Equals(p.Series.PresentationUniqueKey, i.Series.PresentationUniqueKey, StringComparison.Ordinal)))
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
// If the season no longer exists in the remote lookup, delete it, but only if an existing episode doesn't require it
|
2019-02-11 19:14:24 +01:00
|
|
|
return episodeLookup.All(e => e.SeasonNumber != seasonNumber) && allEpisodes.All(s => s.ParentIndexNumber != seasonNumber || s.IsInSeasonFolder);
|
2014-02-17 22:35:08 +01:00
|
|
|
}
|
|
|
|
|
2014-12-23 04:58:14 +01:00
|
|
|
// Season does not have a number
|
|
|
|
// Remove if there are no episodes directly in series without a season number
|
2018-09-12 19:26:21 +02:00
|
|
|
return allEpisodes.All(s => s.ParentIndexNumber.HasValue || s.IsInSeasonFolder);
|
2019-02-08 17:42:09 +01:00
|
|
|
});
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
var hasChanges = false;
|
|
|
|
|
2017-02-18 09:32:17 +01:00
|
|
|
foreach (var seasonToRemove in seasonsToRemove)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
_libraryManager.DeleteItem(seasonToRemove, new DeleteOptions
|
2016-02-12 05:54:00 +01:00
|
|
|
{
|
|
|
|
DeleteFileLocation = true
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
}, false);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
hasChanges = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasChanges;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds the episode.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="series">The series.</param>
|
|
|
|
/// <param name="seasonNumber">The season number.</param>
|
|
|
|
/// <param name="episodeNumber">The episode number.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
private async Task AddEpisode(Series series, int seasonNumber, int episodeNumber, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var season = series.Children.OfType<Season>()
|
|
|
|
.FirstOrDefault(i => i.IndexNumber.HasValue && i.IndexNumber.Value == seasonNumber);
|
|
|
|
|
|
|
|
if (season == null)
|
|
|
|
{
|
2015-09-14 01:07:54 +02:00
|
|
|
var provider = new DummySeasonProvider(_config, _logger, _localization, _libraryManager, _fileSystem);
|
2016-05-20 21:45:04 +02:00
|
|
|
season = await provider.AddSeason(series, seasonNumber, true, cancellationToken).ConfigureAwait(false);
|
2014-02-17 22:35:08 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 23:56:57 +01:00
|
|
|
var name = $"Episode {episodeNumber.ToString(_usCulture)}";
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
var episode = new Episode
|
|
|
|
{
|
|
|
|
Name = name,
|
|
|
|
IndexNumber = episodeNumber,
|
|
|
|
ParentIndexNumber = seasonNumber,
|
2019-02-08 17:42:09 +01:00
|
|
|
Id = _libraryManager.GetNewItemId(series.Id + seasonNumber.ToString(_usCulture) + name, typeof(Episode)),
|
2016-07-05 07:40:18 +02:00
|
|
|
IsVirtualItem = true,
|
2019-02-07 23:56:57 +01:00
|
|
|
SeasonId = season?.Id ?? Guid.Empty,
|
2016-07-05 08:01:31 +02:00
|
|
|
SeriesId = series.Id
|
2014-02-17 22:35:08 +01:00
|
|
|
};
|
|
|
|
|
2017-08-27 02:32:33 +02:00
|
|
|
season.AddChild(episode, cancellationToken);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2018-12-14 20:17:29 +01:00
|
|
|
await episode.RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(_logger, _fileSystem)), cancellationToken).ConfigureAwait(false);
|
2014-02-17 22:35:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the existing episode.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="existingEpisodes">The existing episodes.</param>
|
2014-03-30 04:44:03 +02:00
|
|
|
/// <param name="seasonCounts"></param>
|
2019-02-11 19:14:24 +01:00
|
|
|
/// <param name="episodeTuple"></param>
|
2014-02-17 22:35:08 +01:00
|
|
|
/// <returns>Episode.</returns>
|
2019-02-11 19:14:24 +01:00
|
|
|
private Episode GetExistingEpisode(IList<Episode> existingEpisodes, IReadOnlyDictionary<int, int> seasonCounts, (int SeasonNumber, int EpisodeNumber, DateTime FirstAired) episodeTuple)
|
2014-03-30 04:44:03 +02:00
|
|
|
{
|
2019-02-08 17:42:09 +01:00
|
|
|
var seasonNumber = episodeTuple.SeasonNumber;
|
|
|
|
var episodeNumber = episodeTuple.EpisodeNumber;
|
2014-03-30 04:44:03 +02:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2019-02-08 17:42:09 +01:00
|
|
|
var episode = GetExistingEpisode(existingEpisodes, seasonNumber, episodeNumber);
|
2014-03-30 04:44:03 +02:00
|
|
|
if (episode != null)
|
2019-02-11 19:14:24 +01:00
|
|
|
{
|
2014-03-30 04:44:03 +02:00
|
|
|
return episode;
|
2019-02-11 19:14:24 +01:00
|
|
|
}
|
2014-03-30 04:44:03 +02:00
|
|
|
|
2019-02-08 17:42:09 +01:00
|
|
|
seasonNumber--;
|
2014-03-30 04:44:03 +02:00
|
|
|
|
2019-02-08 17:42:09 +01:00
|
|
|
if (seasonCounts.ContainsKey(seasonNumber))
|
2019-02-11 19:14:24 +01:00
|
|
|
{
|
2019-02-08 17:42:09 +01:00
|
|
|
episodeNumber += seasonCounts[seasonNumber];
|
2019-02-11 19:14:24 +01:00
|
|
|
}
|
2014-03-30 04:44:03 +02:00
|
|
|
else
|
2019-02-11 19:14:24 +01:00
|
|
|
{
|
2014-03-30 04:44:03 +02:00
|
|
|
break;
|
2019-02-11 19:14:24 +01:00
|
|
|
}
|
2014-03-30 04:44:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
private Episode GetExistingEpisode(IEnumerable<Episode> existingEpisodes, int season, int episode)
|
2014-02-17 22:35:08 +01:00
|
|
|
{
|
|
|
|
return existingEpisodes
|
2018-09-12 19:26:21 +02:00
|
|
|
.FirstOrDefault(i => i.ParentIndexNumber == season && i.ContainsEpisodeNumber(episode));
|
2014-02-17 22:35:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|