2016-03-27 23:11:27 +02:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2014-02-20 05:53:15 +01:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
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;
|
2017-06-23 18:04:45 +02:00
|
|
|
|
using MediaBrowser.Common.Progress;
|
2016-10-25 21:02:04 +02:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-10-24 04:45:23 +02:00
|
|
|
|
using MediaBrowser.Model.Globalization;
|
2016-10-27 21:03:23 +02:00
|
|
|
|
using MediaBrowser.Model.Xml;
|
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");
|
2016-11-16 21:52:17 +01:00
|
|
|
|
private static readonly SemaphoreSlim ResourceLock = new SemaphoreSlim(1, 1);
|
2016-04-26 05:39:21 +02:00
|
|
|
|
public static bool IsRunning = false;
|
2016-10-27 21:03:23 +02:00
|
|
|
|
private readonly IXmlReaderSettingsFactory _xmlSettings;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
2016-10-27 21:03:23 +02:00
|
|
|
|
public MissingEpisodeProvider(ILogger logger, IServerConfigurationManager config, ILibraryManager libraryManager, ILocalizationManager localization, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlSettings)
|
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;
|
2016-10-27 21:03:23 +02:00
|
|
|
|
_xmlSettings = xmlSettings;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 05:39:21 +02:00
|
|
|
|
public async Task Run(List<IGrouping<string, Series>> series, bool addNewItems, CancellationToken cancellationToken)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-11-16 21:52:17 +01:00
|
|
|
|
await ResourceLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
2016-04-26 05:39:21 +02:00
|
|
|
|
IsRunning = true;
|
|
|
|
|
|
2014-02-17 22:35:08 +01:00
|
|
|
|
foreach (var seriesGroup in series)
|
|
|
|
|
{
|
2014-02-26 05:38:21 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-04-26 05:39:21 +02:00
|
|
|
|
await Run(seriesGroup, addNewItems, cancellationToken).ConfigureAwait(false);
|
2014-02-26 05:38:21 +01:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-11-16 21:52:17 +01:00
|
|
|
|
catch (IOException ex)
|
2014-02-26 05:38:21 +01:00
|
|
|
|
{
|
2016-01-21 20:45:52 +01:00
|
|
|
|
//_logger.Warn("Series files missing for series id {0}", seriesGroup.Key);
|
2016-11-16 21:52:17 +01:00
|
|
|
|
_logger.ErrorException("Error in missing episode provider for series id {0}", ex, seriesGroup.Key);
|
2014-02-26 05:38:21 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error in missing episode provider for series id {0}", ex, seriesGroup.Key);
|
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
2016-04-26 05:39:21 +02:00
|
|
|
|
|
|
|
|
|
IsRunning = false;
|
2016-11-16 21:52:17 +01:00
|
|
|
|
ResourceLock.Release();
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 05:39:21 +02:00
|
|
|
|
private async Task Run(IGrouping<string, Series> group, bool addNewItems, CancellationToken cancellationToken)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-11-16 21:52:17 +01:00
|
|
|
|
var seriesList = group.ToList();
|
|
|
|
|
var tvdbId = seriesList
|
|
|
|
|
.Select(i => i.GetProviderId(MetadataProviders.Tvdb))
|
|
|
|
|
.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
2015-11-24 19:45:52 +01:00
|
|
|
|
// Todo: Support series by imdb id
|
|
|
|
|
var seriesProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
seriesProviderIds[MetadataProviders.Tvdb.ToString()] = tvdbId;
|
|
|
|
|
|
|
|
|
|
var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, seriesProviderIds);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
2016-12-26 18:38:12 +01:00
|
|
|
|
// Doesn't have required provider id's
|
|
|
|
|
if (string.IsNullOrWhiteSpace(seriesDataPath))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 06:15:16 +01:00
|
|
|
|
// Check this in order to avoid logging an exception due to directory not existing
|
|
|
|
|
if (!_fileSystem.DirectoryExists(seriesDataPath))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 09:58:33 +02:00
|
|
|
|
var episodeFiles = _fileSystem.GetFilePaths(seriesDataPath)
|
|
|
|
|
.Where(i => string.Equals(Path.GetExtension(i), ".xml", StringComparison.OrdinalIgnoreCase))
|
2014-02-17 22:35:08 +01:00
|
|
|
|
.Select(Path.GetFileNameWithoutExtension)
|
|
|
|
|
.Where(i => i.StartsWith("episode-", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var episodeLookup = episodeFiles
|
|
|
|
|
.Select(i =>
|
|
|
|
|
{
|
|
|
|
|
var parts = i.Split('-');
|
|
|
|
|
|
|
|
|
|
if (parts.Length == 3)
|
|
|
|
|
{
|
|
|
|
|
int seasonNumber;
|
|
|
|
|
|
2015-01-14 05:20:30 +01:00
|
|
|
|
if (int.TryParse(parts[1], NumberStyles.Integer, _usCulture, out seasonNumber))
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
|
|
|
|
int episodeNumber;
|
|
|
|
|
|
2015-01-14 05:20:30 +01:00
|
|
|
|
if (int.TryParse(parts[2], NumberStyles.Integer, _usCulture, out episodeNumber))
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
|
|
|
|
return new Tuple<int, int>(seasonNumber, episodeNumber);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Tuple<int, int>(-1, -1);
|
|
|
|
|
})
|
|
|
|
|
.Where(i => i.Item1 != -1 && i.Item2 != -1)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2016-11-16 21:52:17 +01:00
|
|
|
|
var hasBadData = HasInvalidContent(seriesList);
|
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
|
|
|
|
|
var addMissingEpisodes = !hasBadData && seriesList.All(i => _libraryManager.GetLibraryOptions(i).ImportMissingEpisodes);
|
|
|
|
|
|
2016-11-16 21:52:17 +01:00
|
|
|
|
var anySeasonsRemoved = await RemoveObsoleteOrMissingSeasons(seriesList, episodeLookup)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2016-11-22 19:22:01 +01:00
|
|
|
|
var anyEpisodesRemoved = await RemoveObsoleteOrMissingEpisodes(seriesList, episodeLookup, addMissingEpisodes)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var hasNewEpisodes = false;
|
|
|
|
|
|
2016-11-16 21:52:17 +01:00
|
|
|
|
if (addNewItems && seriesList.All(i => i.IsInternetMetadataEnabled()))
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2014-03-02 16:42:21 +01:00
|
|
|
|
var seriesConfig = _config.Configuration.MetadataOptions.FirstOrDefault(i => string.Equals(i.ItemType, typeof(Series).Name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (seriesConfig == null || !seriesConfig.DisabledMetadataFetchers.Contains(TvdbSeriesProvider.Current.Name, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2016-11-22 19:22:01 +01:00
|
|
|
|
hasNewEpisodes = await AddMissingEpisodes(seriesList, addMissingEpisodes, seriesDataPath, episodeLookup, cancellationToken)
|
2014-03-02 16:42:21 +01: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
|
|
|
|
{
|
2016-11-16 21:52:17 +01:00
|
|
|
|
foreach (var series in seriesList)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-08-06 06:48:00 +02:00
|
|
|
|
var directoryService = new DirectoryService(_logger, _fileSystem);
|
2014-10-25 20:32:58 +02:00
|
|
|
|
|
2016-07-29 21:18:03 +02:00
|
|
|
|
await series.RefreshMetadata(new MetadataRefreshOptions(directoryService), cancellationToken).ConfigureAwait(false);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
2017-06-23 18:04:45 +02:00
|
|
|
|
await series.ValidateChildren(new SimpleProgress<double>(), cancellationToken, new MetadataRefreshOptions(directoryService), true)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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>
|
|
|
|
|
/// <param name="group"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private bool HasInvalidContent(IEnumerable<Series> group)
|
|
|
|
|
{
|
2015-01-25 07:34:50 +01:00
|
|
|
|
var allItems = group.ToList().SelectMany(i => i.GetRecursiveChildren()).ToList();
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 18:45:04 +02:00
|
|
|
|
private const double UnairedEpisodeThresholdDays = 2;
|
|
|
|
|
|
2014-02-17 22:35:08 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the missing episodes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="series">The series.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2015-01-25 07:34:50 +01:00
|
|
|
|
private async Task<bool> AddMissingEpisodes(List<Series> series,
|
2016-11-22 19:22:01 +01:00
|
|
|
|
bool addMissingEpisodes,
|
2015-01-25 07:34:50 +01:00
|
|
|
|
string seriesDataPath,
|
|
|
|
|
IEnumerable<Tuple<int, int>> episodeLookup,
|
2014-09-13 16:10:30 +02:00
|
|
|
|
CancellationToken cancellationToken)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2014-03-30 04:44:03 +02:00
|
|
|
|
var existingEpisodes = (from s in series
|
2017-04-27 20:12:44 +02:00
|
|
|
|
from c in s.GetRecursiveChildren(i => i is Episode).Cast<Episode>()
|
2017-02-18 09:32:17 +01:00
|
|
|
|
select new Tuple<int, Episode>((c.ParentIndexNumber ?? 0) , c))
|
2014-03-30 04:44:03 +02:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var lookup = episodeLookup as IList<Tuple<int, int>> ?? episodeLookup.ToList();
|
|
|
|
|
|
|
|
|
|
var seasonCounts = (from e in lookup
|
2015-01-25 07:34:50 +01:00
|
|
|
|
group e by e.Item1 into g
|
|
|
|
|
select g)
|
2014-03-30 04:44:03 +02:00
|
|
|
|
.ToDictionary(g => g.Key, g => g.Count());
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
|
|
var hasChanges = false;
|
|
|
|
|
|
2014-03-30 04:44:03 +02:00
|
|
|
|
foreach (var tuple in lookup)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
|
|
|
|
if (tuple.Item1 <= 0)
|
|
|
|
|
{
|
|
|
|
|
// Ignore season zeros
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tuple.Item2 <= 0)
|
|
|
|
|
{
|
|
|
|
|
// Ignore episode zeros
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var airDate = GetAirDate(seriesDataPath, tuple.Item1, tuple.Item2);
|
|
|
|
|
|
|
|
|
|
if (!airDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-12-08 16:40:20 +01:00
|
|
|
|
|
2014-02-17 22:35:08 +01:00
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
var targetSeries = DetermineAppropriateSeries(series, tuple.Item1);
|
|
|
|
|
|
2017-08-23 18:45:04 +02:00
|
|
|
|
now = now.AddDays(0 - UnairedEpisodeThresholdDays);
|
2016-12-08 16:40:20 +01:00
|
|
|
|
|
2014-02-17 22:35:08 +01:00
|
|
|
|
if (airDate.Value < now)
|
|
|
|
|
{
|
2016-11-22 19:22:01 +01:00
|
|
|
|
if (addMissingEpisodes)
|
2014-09-13 16:10:30 +02:00
|
|
|
|
{
|
|
|
|
|
// tvdb has a lot of nearly blank episodes
|
|
|
|
|
_logger.Info("Creating virtual missing episode {0} {1}x{2}", targetSeries.Name, tuple.Item1, tuple.Item2);
|
2017-02-18 09:32:17 +01:00
|
|
|
|
await AddEpisode(targetSeries, tuple.Item1, tuple.Item2, cancellationToken).ConfigureAwait(false);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
2014-09-13 16:10:30 +02:00
|
|
|
|
hasChanges = true;
|
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
|
|
|
|
else if (airDate.Value > now)
|
|
|
|
|
{
|
|
|
|
|
// tvdb has a lot of nearly blank episodes
|
|
|
|
|
_logger.Info("Creating virtual unaired episode {0} {1}x{2}", targetSeries.Name, tuple.Item1, tuple.Item2);
|
2017-02-18 09:32:17 +01:00
|
|
|
|
await AddEpisode(targetSeries, tuple.Item1, tuple.Item2, cancellationToken).ConfigureAwait(false);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
|
|
hasChanges = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hasChanges;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 20:12:44 +02:00
|
|
|
|
private Series DetermineAppropriateSeries(List<Series> series, int seasonNumber)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2017-04-27 20:12:44 +02:00
|
|
|
|
if (series.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
return series[0];
|
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
2017-04-27 20:12:44 +02:00
|
|
|
|
return series.FirstOrDefault(s => s.GetRecursiveChildren(i => i is Season).Any(season => (season.IndexNumber) == seasonNumber)) ??
|
|
|
|
|
series.FirstOrDefault(s => s.GetRecursiveChildren(i => i is Season).Any(season => (season.IndexNumber) == 1)) ??
|
|
|
|
|
series.OrderBy(s => s.GetRecursiveChildren(i => i is Season).Select(season => season.IndexNumber).Min()).First();
|
2014-03-30 04:44:03 +02:00
|
|
|
|
}
|
2015-01-25 07:34:50 +01:00
|
|
|
|
|
2014-02-17 22:35:08 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes the virtual entry after a corresponding physical version has been added
|
|
|
|
|
/// </summary>
|
2015-01-25 07:34:50 +01:00
|
|
|
|
private async Task<bool> RemoveObsoleteOrMissingEpisodes(IEnumerable<Series> series,
|
2016-11-22 19:22:01 +01:00
|
|
|
|
IEnumerable<Tuple<int, int>> episodeLookup,
|
|
|
|
|
bool allowMissingEpisodes)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2014-03-30 04:44:03 +02:00
|
|
|
|
var existingEpisodes = (from s in series
|
2017-04-27 20:12:44 +02:00
|
|
|
|
from c in s.GetRecursiveChildren(i => i is Episode).Cast<Episode>()
|
2017-02-18 09:32:17 +01:00
|
|
|
|
select new { Episode = c })
|
2014-03-30 04:44:03 +02:00
|
|
|
|
.ToList();
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
|
|
var physicalEpisodes = existingEpisodes
|
2014-03-30 04:44:03 +02:00
|
|
|
|
.Where(i => i.Episode.LocationType != LocationType.Virtual)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var virtualEpisodes = existingEpisodes
|
2014-03-30 04:44:03 +02:00
|
|
|
|
.Where(i => i.Episode.LocationType == LocationType.Virtual)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var episodesToRemove = virtualEpisodes
|
|
|
|
|
.Where(i =>
|
|
|
|
|
{
|
2014-03-30 04:44:03 +02:00
|
|
|
|
if (i.Episode.IndexNumber.HasValue && i.Episode.ParentIndexNumber.HasValue)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2017-02-18 09:32:17 +01:00
|
|
|
|
var seasonNumber = i.Episode.ParentIndexNumber.Value;
|
2014-03-30 04:44:03 +02:00
|
|
|
|
var episodeNumber = i.Episode.IndexNumber.Value;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
|
|
// If there's a physical episode with the same season and episode number, delete it
|
|
|
|
|
if (physicalEpisodes.Any(p =>
|
2017-02-18 09:32:17 +01:00
|
|
|
|
p.Episode.ParentIndexNumber.HasValue && (p.Episode.ParentIndexNumber.Value) == seasonNumber &&
|
2014-03-30 04:44:03 +02:00
|
|
|
|
p.Episode.ContainsEpisodeNumber(episodeNumber)))
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the episode no longer exists in the remote lookup, delete it
|
|
|
|
|
if (!episodeLookup.Any(e => e.Item1 == seasonNumber && e.Item2 == episodeNumber))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 18:45:04 +02:00
|
|
|
|
if (!allowMissingEpisodes && i.Episode.IsMissingEpisode)
|
2016-11-22 19:22:01 +01:00
|
|
|
|
{
|
2017-08-23 18:45:04 +02:00
|
|
|
|
// If it's missing, but not unaired, remove it
|
|
|
|
|
if (!i.Episode.PremiereDate.HasValue || i.Episode.PremiereDate.Value.ToLocalTime().Date.AddDays(UnairedEpisodeThresholdDays) < DateTime.Now.Date)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-11-22 19:22:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-17 22:35:08 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var hasChanges = false;
|
|
|
|
|
|
2014-03-30 04:44:03 +02:00
|
|
|
|
foreach (var episodeToRemove in episodesToRemove.Select(e => e.Episode))
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
|
|
|
|
_logger.Info("Removing missing/unaired episode {0} {1}x{2}", episodeToRemove.Series.Name, episodeToRemove.ParentIndexNumber, episodeToRemove.IndexNumber);
|
|
|
|
|
|
2016-02-12 05:54:00 +01:00
|
|
|
|
await episodeToRemove.Delete(new DeleteOptions
|
|
|
|
|
{
|
|
|
|
|
DeleteFileLocation = true
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
|
|
hasChanges = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hasChanges;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes the obsolete or missing seasons.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="series">The series.</param>
|
|
|
|
|
/// <param name="episodeLookup">The episode lookup.</param>
|
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
2015-01-25 07:34:50 +01:00
|
|
|
|
private async Task<bool> RemoveObsoleteOrMissingSeasons(IEnumerable<Series> series,
|
2014-12-23 04:58:14 +01:00
|
|
|
|
IEnumerable<Tuple<int, int>> episodeLookup)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2014-03-30 04:44:03 +02:00
|
|
|
|
var existingSeasons = (from s in series
|
|
|
|
|
from c in s.Children.OfType<Season>()
|
2017-02-18 09:32:17 +01:00
|
|
|
|
select c)
|
2014-03-30 04:44:03 +02:00
|
|
|
|
.ToList();
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
|
|
var physicalSeasons = existingSeasons
|
2017-02-18 09:32:17 +01:00
|
|
|
|
.Where(i => i.LocationType != LocationType.Virtual)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var virtualSeasons = existingSeasons
|
2017-02-18 09:32:17 +01:00
|
|
|
|
.Where(i => i.LocationType == LocationType.Virtual)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
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
|
2017-02-18 09:32:17 +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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the season no longer exists in the remote lookup, delete it
|
|
|
|
|
if (episodeLookup.All(e => e.Item1 != seasonNumber))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2017-04-27 20:12:44 +02:00
|
|
|
|
return i.Series.GetRecursiveChildren(e => e is Episode).Cast<Episode>().All(s => s.ParentIndexNumber.HasValue || s.IsInSeasonFolder);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var hasChanges = false;
|
|
|
|
|
|
2017-02-18 09:32:17 +01:00
|
|
|
|
foreach (var seasonToRemove in seasonsToRemove)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
|
|
|
|
_logger.Info("Removing virtual season {0} {1}", seasonToRemove.Series.Name, seasonToRemove.IndexNumber);
|
|
|
|
|
|
2016-02-12 05:54:00 +01:00
|
|
|
|
await seasonToRemove.Delete(new DeleteOptions
|
|
|
|
|
{
|
|
|
|
|
DeleteFileLocation = true
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(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
|
|
|
|
}
|
|
|
|
|
|
2015-01-14 05:20:30 +01:00
|
|
|
|
var name = string.Format("Episode {0}", episodeNumber.ToString(_usCulture));
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
|
|
var episode = new Episode
|
|
|
|
|
{
|
|
|
|
|
Name = name,
|
|
|
|
|
IndexNumber = episodeNumber,
|
|
|
|
|
ParentIndexNumber = seasonNumber,
|
2016-06-12 07:03:52 +02:00
|
|
|
|
Id = _libraryManager.GetNewItemId((series.Id + seasonNumber.ToString(_usCulture) + name), typeof(Episode)),
|
2016-07-05 07:40:18 +02:00
|
|
|
|
IsVirtualItem = true,
|
2016-07-05 08:01:31 +02:00
|
|
|
|
SeasonId = season == null ? (Guid?)null : season.Id,
|
|
|
|
|
SeriesId = series.Id
|
2014-02-17 22:35:08 +01:00
|
|
|
|
};
|
|
|
|
|
|
2015-07-09 07:52:25 +02:00
|
|
|
|
episode.SetParent(season);
|
|
|
|
|
|
2014-02-17 22:35:08 +01:00
|
|
|
|
await season.AddChild(episode, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2016-07-29 21:18:03 +02:00
|
|
|
|
await episode.RefreshMetadata(new MetadataRefreshOptions(_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>
|
2014-02-17 22:35:08 +01:00
|
|
|
|
/// <param name="tuple">The tuple.</param>
|
|
|
|
|
/// <returns>Episode.</returns>
|
2014-03-30 04:44:03 +02:00
|
|
|
|
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)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
|
|
|
|
return existingEpisodes
|
2014-03-30 04:44:03 +02:00
|
|
|
|
.Where(i => i.Item1 == season && i.Item2.ContainsEpisodeNumber(episode))
|
|
|
|
|
.Select(i => i.Item2)
|
|
|
|
|
.FirstOrDefault();
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the air date.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="seriesDataPath">The series data path.</param>
|
|
|
|
|
/// <param name="seasonNumber">The season number.</param>
|
|
|
|
|
/// <param name="episodeNumber">The episode number.</param>
|
|
|
|
|
/// <returns>System.Nullable{DateTime}.</returns>
|
|
|
|
|
private DateTime? GetAirDate(string seriesDataPath, int seasonNumber, int episodeNumber)
|
|
|
|
|
{
|
|
|
|
|
// First open up the tvdb xml file and make sure it has valid data
|
2015-01-14 05:20:30 +01:00
|
|
|
|
var filename = string.Format("episode-{0}-{1}.xml", seasonNumber.ToString(_usCulture), episodeNumber.ToString(_usCulture));
|
2014-02-17 22:35:08 +01:00
|
|
|
|
|
|
|
|
|
var xmlPath = Path.Combine(seriesDataPath, filename);
|
|
|
|
|
|
|
|
|
|
DateTime? airDate = null;
|
|
|
|
|
|
2016-10-27 21:03:23 +02:00
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(xmlPath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-10-27 21:03:23 +02:00
|
|
|
|
// It appears the best way to filter out invalid entries is to only include those with valid air dates
|
|
|
|
|
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-10-27 21:03:23 +02:00
|
|
|
|
var settings = _xmlSettings.Create(false);
|
|
|
|
|
|
|
|
|
|
settings.CheckCharacters = false;
|
|
|
|
|
settings.IgnoreProcessingInstructions = true;
|
|
|
|
|
settings.IgnoreComments = true;
|
|
|
|
|
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-10-27 21:03:23 +02:00
|
|
|
|
reader.MoveToContent();
|
2016-11-02 18:29:06 +01:00
|
|
|
|
reader.Read();
|
2016-10-27 21:03:23 +02:00
|
|
|
|
|
|
|
|
|
// Loop through each element
|
2016-12-03 22:46:06 +01:00
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-10-27 21:03:23 +02:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-10-27 21:03:23 +02:00
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "EpisodeName":
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-10-27 21:03:23 +02:00
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
// Not valid, ignore these
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
2016-10-27 21:03:23 +02:00
|
|
|
|
case "FirstAired":
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-10-27 21:03:23 +02:00
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
2014-02-17 22:35:08 +01:00
|
|
|
|
{
|
2016-10-27 21:03:23 +02:00
|
|
|
|
DateTime date;
|
|
|
|
|
if (DateTime.TryParse(val, out date))
|
|
|
|
|
{
|
|
|
|
|
airDate = date.ToUniversalTime();
|
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
2016-10-27 21:03:23 +02:00
|
|
|
|
|
|
|
|
|
break;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
2016-10-27 21:03:23 +02:00
|
|
|
|
default:
|
2016-11-02 18:29:06 +01:00
|
|
|
|
{
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-10-27 21:03:23 +02:00
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
2016-11-02 18:29:06 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reader.Read();
|
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return airDate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|