2014-12-23 03:58:14 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-10-16 23:35:11 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2013-08-31 01:08:32 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-12-23 03:58:14 +00:00
|
|
|
|
using MediaBrowser.Controller.Localization;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-10-16 23:35:11 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-08-31 01:08:32 +00:00
|
|
|
|
using System;
|
2014-12-23 03:58:14 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-08-31 01:08:32 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-10-04 04:23:11 +00:00
|
|
|
|
using CommonIO;
|
2016-04-26 03:39:21 +00:00
|
|
|
|
using MediaBrowser.Common.ScheduledTasks;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
|
|
|
|
using MediaBrowser.Model.Tasks;
|
2013-08-31 01:08:32 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.TV
|
|
|
|
|
{
|
2014-03-30 02:44:03 +00:00
|
|
|
|
class SeriesGroup : List<Series>, IGrouping<string, Series>
|
|
|
|
|
{
|
|
|
|
|
public string Key { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 23:08:01 +00:00
|
|
|
|
class SeriesPostScanTask : ILibraryPostScanTask, IHasOrder
|
2013-08-31 01:08:32 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2013-10-16 23:35:11 +00:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly ILogger _logger;
|
2014-12-23 03:58:14 +00:00
|
|
|
|
private readonly ILocalizationManager _localization;
|
2015-09-13 23:07:54 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-08-31 01:08:32 +00:00
|
|
|
|
|
2015-09-13 23:07:54 +00:00
|
|
|
|
public SeriesPostScanTask(ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, ILocalizationManager localization, IFileSystem fileSystem)
|
2013-08-31 01:08:32 +00:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
2013-10-16 23:35:11 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_config = config;
|
2014-12-23 03:58:14 +00:00
|
|
|
|
_localization = localization;
|
2015-09-13 23:07:54 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-08-31 01:08:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2013-10-16 23:35:11 +00:00
|
|
|
|
return RunInternal(progress, cancellationToken);
|
2013-08-31 01:08:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-16 23:35:11 +00:00
|
|
|
|
private async Task RunInternal(IProgress<double> progress, CancellationToken cancellationToken)
|
2013-08-31 01:08:32 +00:00
|
|
|
|
{
|
2016-04-26 03:39:21 +00:00
|
|
|
|
var seriesList = _libraryManager.GetItemList(new InternalItemsQuery()
|
|
|
|
|
{
|
|
|
|
|
IncludeItemTypes = new[] { typeof(Series).Name },
|
2016-05-06 06:02:10 +00:00
|
|
|
|
Recursive = true,
|
|
|
|
|
GroupByPresentationUniqueKey = false
|
2016-04-26 03:39:21 +00:00
|
|
|
|
|
|
|
|
|
}).Cast<Series>().ToList();
|
2013-08-31 01:08:32 +00:00
|
|
|
|
|
2014-03-30 02:44:03 +00:00
|
|
|
|
var seriesGroups = FindSeriesGroups(seriesList).Where(g => !string.IsNullOrEmpty(g.Key)).ToList();
|
2013-12-08 22:07:57 +00:00
|
|
|
|
|
2016-04-26 03:39:21 +00:00
|
|
|
|
await new MissingEpisodeProvider(_logger, _config, _libraryManager, _localization, _fileSystem)
|
|
|
|
|
.Run(seriesGroups, true, cancellationToken).ConfigureAwait(false);
|
2013-12-08 22:07:57 +00:00
|
|
|
|
|
2013-08-31 01:08:32 +00:00
|
|
|
|
var numComplete = 0;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
2013-08-31 01:08:32 +00:00
|
|
|
|
foreach (var series in seriesList)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2015-01-25 06:34:50 +00:00
|
|
|
|
var episodes = series.GetRecursiveChildren(i => i is Episode)
|
|
|
|
|
.Cast<Episode>()
|
2013-08-31 01:08:32 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-01-20 18:46:08 +00:00
|
|
|
|
var physicalEpisodes = episodes.Where(i => i.LocationType != LocationType.Virtual)
|
2013-08-31 01:08:32 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-01-20 18:46:08 +00:00
|
|
|
|
series.SpecialFeatureIds = physicalEpisodes
|
|
|
|
|
.Where(i => i.ParentIndexNumber.HasValue && i.ParentIndexNumber.Value == 0)
|
|
|
|
|
.Select(i => i.Id)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2013-08-31 01:08:32 +00:00
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= seriesList.Count;
|
|
|
|
|
percent *= 100;
|
|
|
|
|
|
|
|
|
|
progress.Report(percent);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-09 23:08:01 +00:00
|
|
|
|
|
2016-04-26 03:39:21 +00:00
|
|
|
|
internal static IEnumerable<IGrouping<string, Series>> FindSeriesGroups(List<Series> seriesList)
|
2014-03-30 02:44:03 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 03:39:21 +00:00
|
|
|
|
private static void FindAllLinked(Series series, HashSet<Series> visited, IDictionary<Series, List<Series>> linksMap, List<Series> results)
|
2014-03-30 02:44:03 +00:00
|
|
|
|
{
|
|
|
|
|
results.Add(series);
|
|
|
|
|
visited.Add(series);
|
|
|
|
|
|
|
|
|
|
var links = linksMap[series];
|
|
|
|
|
|
|
|
|
|
foreach (var s in links)
|
|
|
|
|
{
|
|
|
|
|
if (!visited.Contains(s))
|
|
|
|
|
{
|
|
|
|
|
FindAllLinked(s, visited, linksMap, results);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 03:39:21 +00:00
|
|
|
|
private static bool ShareProviderId(Series a, Series b)
|
2014-03-30 02:44:03 +00:00
|
|
|
|
{
|
|
|
|
|
return a.ProviderIds.Any(id =>
|
|
|
|
|
{
|
|
|
|
|
string value;
|
|
|
|
|
return b.ProviderIds.TryGetValue(id.Key, out value) && id.Value == value;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 23:08:01 +00:00
|
|
|
|
public int Order
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// Run after tvdb update task
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-31 01:08:32 +00:00
|
|
|
|
}
|
2013-10-16 23:35:11 +00:00
|
|
|
|
|
2016-04-26 03:39:21 +00:00
|
|
|
|
public class CleanMissingEpisodesEntryPoint : IServerEntryPoint
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ILocalizationManager _localization;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly object _libraryChangedSyncLock = new object();
|
|
|
|
|
private const int LibraryUpdateDuration = 180000;
|
|
|
|
|
private readonly ITaskManager _taskManager;
|
|
|
|
|
|
|
|
|
|
public CleanMissingEpisodesEntryPoint(ILibraryManager libraryManager, IServerConfigurationManager config, ILogger logger, ILocalizationManager localization, IFileSystem fileSystem, ITaskManager taskManager)
|
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_config = config;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_localization = localization;
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_taskManager = taskManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Timer LibraryUpdateTimer { get; set; }
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
_libraryManager.ItemAdded += _libraryManager_ItemAdded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!FilterItem(e.Item))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
|
{
|
|
|
|
|
if (LibraryUpdateTimer == null)
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration, Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void LibraryUpdateTimerCallback(object state)
|
|
|
|
|
{
|
|
|
|
|
if (MissingEpisodeProvider.IsRunning)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_libraryManager.IsScanRunning)
|
|
|
|
|
{
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var seriesList = _libraryManager.GetItemList(new InternalItemsQuery()
|
|
|
|
|
{
|
|
|
|
|
IncludeItemTypes = new[] { typeof(Series).Name },
|
2016-05-06 06:02:10 +00:00
|
|
|
|
Recursive = true,
|
|
|
|
|
GroupByPresentationUniqueKey = false
|
2016-04-26 03:39:21 +00:00
|
|
|
|
|
|
|
|
|
}).Cast<Series>().ToList();
|
|
|
|
|
|
|
|
|
|
var seriesGroups = SeriesPostScanTask.FindSeriesGroups(seriesList).Where(g => !string.IsNullOrEmpty(g.Key)).ToList();
|
|
|
|
|
|
|
|
|
|
await new MissingEpisodeProvider(_logger, _config, _libraryManager, _localization, _fileSystem)
|
|
|
|
|
.Run(seriesGroups, false, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool FilterItem(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
return item is Episode && item.LocationType != LocationType.Virtual;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
if (LibraryUpdateTimer != null)
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer.Dispose();
|
|
|
|
|
LibraryUpdateTimer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_libraryManager.ItemAdded -= _libraryManager_ItemAdded;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-31 01:08:32 +00:00
|
|
|
|
}
|