2013-10-13 19:52:57 +02:00
|
|
|
|
using MediaBrowser.Common.Progress;
|
2014-08-14 15:24:30 +02:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-10-13 19:52:57 +02:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-03-09 23:14:44 +01:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-08-14 15:24:30 +02:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-10-13 19:52:57 +02:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
2014-08-14 15:24:30 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-10-13 19:52:57 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-10-04 06:23:11 +02:00
|
|
|
|
using CommonIO;
|
2015-09-14 01:07:54 +02:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2013-10-13 19:52:57 +02:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Library.Validators
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class PeopleValidator
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class PeopleValidator
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2014-08-14 15:24:30 +02:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2015-09-14 01:07:54 +02:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-08-14 15:24:30 +02:00
|
|
|
|
|
2013-10-13 19:52:57 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="PeopleValidator" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
2015-09-14 01:07:54 +02:00
|
|
|
|
public PeopleValidator(ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem)
|
2013-10-13 19:52:57 +02:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_logger = logger;
|
2014-08-14 15:24:30 +02:00
|
|
|
|
_config = config;
|
2015-09-14 01:07:54 +02:00
|
|
|
|
_fileSystem = fileSystem;
|
2014-08-14 15:24:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool DownloadMetadata(PersonInfo i, PeopleMetadataOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (i.IsType(PersonType.Actor))
|
|
|
|
|
{
|
|
|
|
|
return options.DownloadActorMetadata;
|
|
|
|
|
}
|
|
|
|
|
if (i.IsType(PersonType.Director))
|
|
|
|
|
{
|
|
|
|
|
return options.DownloadDirectorMetadata;
|
|
|
|
|
}
|
|
|
|
|
if (i.IsType(PersonType.Composer))
|
|
|
|
|
{
|
|
|
|
|
return options.DownloadComposerMetadata;
|
|
|
|
|
}
|
|
|
|
|
if (i.IsType(PersonType.Writer))
|
|
|
|
|
{
|
|
|
|
|
return options.DownloadWriterMetadata;
|
|
|
|
|
}
|
|
|
|
|
if (i.IsType(PersonType.Producer))
|
|
|
|
|
{
|
|
|
|
|
return options.DownloadProducerMetadata;
|
|
|
|
|
}
|
|
|
|
|
if (i.IsType(PersonType.GuestStar))
|
|
|
|
|
{
|
|
|
|
|
return options.DownloadGuestStarMetadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return options.DownloadOtherPeopleMetadata;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-13 19:52:57 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Validates the people.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2014-08-14 15:24:30 +02:00
|
|
|
|
public async Task ValidatePeople(CancellationToken cancellationToken, IProgress<double> progress)
|
2013-10-13 19:52:57 +02:00
|
|
|
|
{
|
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
|
|
|
|
|
|
|
|
|
innerProgress.RegisterAction(pct => progress.Report(pct * .15));
|
|
|
|
|
|
2014-08-14 15:24:30 +02:00
|
|
|
|
var peopleOptions = _config.Configuration.PeopleMetadataOptions;
|
|
|
|
|
|
2015-06-21 05:35:22 +02:00
|
|
|
|
var people = _libraryManager.GetAllPeople();
|
2013-10-13 19:52:57 +02:00
|
|
|
|
|
2014-08-14 15:24:30 +02:00
|
|
|
|
var dict = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
|
2013-10-13 19:52:57 +02:00
|
|
|
|
|
|
|
|
|
foreach (var person in people)
|
2014-08-14 15:24:30 +02:00
|
|
|
|
{
|
2015-08-22 19:50:37 +02:00
|
|
|
|
var isMetadataEnabled = DownloadMetadata(person, peopleOptions);
|
|
|
|
|
|
|
|
|
|
bool currentValue;
|
|
|
|
|
if (dict.TryGetValue(person.Name, out currentValue))
|
|
|
|
|
{
|
|
|
|
|
if (!currentValue && isMetadataEnabled)
|
|
|
|
|
{
|
|
|
|
|
dict[person.Name] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-08-14 15:24:30 +02:00
|
|
|
|
{
|
2015-08-22 19:50:37 +02:00
|
|
|
|
dict[person.Name] = isMetadataEnabled;
|
2014-08-14 15:24:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
2015-08-22 19:50:37 +02:00
|
|
|
|
var validIds = new List<Guid>();
|
|
|
|
|
|
2014-08-14 15:24:30 +02:00
|
|
|
|
foreach (var person in dict)
|
2013-10-13 19:52:57 +02:00
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-08-14 15:24:30 +02:00
|
|
|
|
var item = _libraryManager.GetPerson(person.Key);
|
|
|
|
|
|
2015-08-22 19:50:37 +02:00
|
|
|
|
validIds.Add(item.Id);
|
2015-10-05 05:24:24 +02:00
|
|
|
|
|
2015-09-14 01:07:54 +02:00
|
|
|
|
var options = new MetadataRefreshOptions(_fileSystem)
|
2014-08-14 15:24:30 +02:00
|
|
|
|
{
|
2015-10-05 05:24:24 +02:00
|
|
|
|
MetadataRefreshMode = person.Value ? MetadataRefreshMode.Default : MetadataRefreshMode.ValidationOnly,
|
|
|
|
|
ImageRefreshMode = person.Value ? ImageRefreshMode.Default : ImageRefreshMode.ValidationOnly
|
2014-08-14 15:24:30 +02:00
|
|
|
|
};
|
2013-10-13 19:52:57 +02:00
|
|
|
|
|
2014-03-09 23:14:44 +01:00
|
|
|
|
await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
|
2013-10-13 19:52:57 +02:00
|
|
|
|
}
|
2015-10-05 05:24:24 +02:00
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-10-13 19:52:57 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2014-04-05 17:02:50 +02:00
|
|
|
|
_logger.ErrorException("Error validating IBN entry {0}", ex, person);
|
2013-10-13 19:52:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update progress
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= people.Count;
|
|
|
|
|
|
2014-05-29 16:19:12 +02:00
|
|
|
|
progress.Report(100 * percent);
|
2013-10-13 19:52:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-22 19:50:37 +02:00
|
|
|
|
var allIds = _libraryManager.GetItemIds(new InternalItemsQuery
|
|
|
|
|
{
|
|
|
|
|
IncludeItemTypes = new[] { typeof(Person).Name }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var invalidIds = allIds
|
|
|
|
|
.Except(validIds)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var id in invalidIds)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var item = _libraryManager.GetItemById(id);
|
|
|
|
|
|
2016-02-06 21:31:10 +01:00
|
|
|
|
if (item != null)
|
2015-08-22 19:50:37 +02:00
|
|
|
|
{
|
2016-02-06 21:31:10 +01:00
|
|
|
|
await _libraryManager.DeleteItem(item, new DeleteOptions
|
|
|
|
|
{
|
|
|
|
|
DeleteFileLocation = false
|
2015-08-22 19:50:37 +02:00
|
|
|
|
|
2016-02-06 21:31:10 +01:00
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
}
|
2015-08-22 19:50:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-13 19:52:57 +02:00
|
|
|
|
progress.Report(100);
|
|
|
|
|
|
|
|
|
|
_logger.Info("People validation complete");
|
|
|
|
|
|
|
|
|
|
// Bad practice, i know. But we keep a lot in memory, unfortunately.
|
|
|
|
|
GC.Collect(2, GCCollectionMode.Forced, true);
|
|
|
|
|
GC.Collect(2, GCCollectionMode.Forced, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|