jellyfin/MediaBrowser.Server.Implementations/Library/Validators/PeopleValidator.cs

171 lines
5.7 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Progress;
2014-08-14 13:24:30 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
2014-08-14 13:24:30 +00:00
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
2014-08-14 13:24:30 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2015-10-04 04:23:11 +00:00
using CommonIO;
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 13:24:30 +00:00
private readonly IServerConfigurationManager _config;
2015-09-13 23:07:54 +00:00
private readonly IFileSystem _fileSystem;
2014-08-14 13:24:30 +00: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-13 23:07:54 +00:00
public PeopleValidator(ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem)
{
_libraryManager = libraryManager;
_logger = logger;
2014-08-14 13:24:30 +00:00
_config = config;
2015-09-13 23:07:54 +00:00
_fileSystem = fileSystem;
2014-08-14 13:24:30 +00: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;
}
/// <summary>
/// Validates the people.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress.</param>
/// <returns>Task.</returns>
2014-08-14 13:24:30 +00:00
public async Task ValidatePeople(CancellationToken cancellationToken, IProgress<double> progress)
{
var innerProgress = new ActionableProgress<double>();
innerProgress.RegisterAction(pct => progress.Report(pct * .15));
2014-08-14 13:24:30 +00:00
var peopleOptions = _config.Configuration.PeopleMetadataOptions;
2016-08-04 04:38:58 +00:00
var people = _libraryManager.GetPeople(new InternalPeopleQuery());
2014-08-14 13:24:30 +00:00
var dict = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
foreach (var person in people)
2014-08-14 13:24:30 +00:00
{
2015-08-22 17:50:37 +00: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 13:24:30 +00:00
{
2015-08-22 17:50:37 +00:00
dict[person.Name] = isMetadataEnabled;
2014-08-14 13:24:30 +00:00
}
}
var numComplete = 0;
2016-08-04 04:38:58 +00:00
_logger.Debug("Will refresh {0} people", dict.Count);
2016-08-07 21:57:46 +00:00
var numPeople = dict.Count;
2014-08-14 13:24:30 +00:00
foreach (var person in dict)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
2014-08-14 13:24:30 +00:00
var item = _libraryManager.GetPerson(person.Key);
2016-02-23 17:53:13 +00:00
var hasMetdata = !string.IsNullOrWhiteSpace(item.Overview);
2016-08-12 15:54:37 +00:00
var performFullRefresh = !hasMetdata && (DateTime.UtcNow - item.DateLastRefreshed).TotalDays >= 30;
2016-02-23 17:53:13 +00:00
var defaultMetadataRefreshMode = performFullRefresh
? MetadataRefreshMode.FullRefresh
: MetadataRefreshMode.Default;
var imageRefreshMode = performFullRefresh
? ImageRefreshMode.FullRefresh
: ImageRefreshMode.Default;
2015-09-13 23:07:54 +00:00
var options = new MetadataRefreshOptions(_fileSystem)
2014-08-14 13:24:30 +00:00
{
2016-02-23 17:53:13 +00:00
MetadataRefreshMode = person.Value ? defaultMetadataRefreshMode : MetadataRefreshMode.ValidationOnly,
2016-08-06 19:56:35 +00:00
ImageRefreshMode = person.Value ? imageRefreshMode : ImageRefreshMode.ValidationOnly,
ForceSave = performFullRefresh
2014-08-14 13:24:30 +00:00
};
await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
}
2015-10-05 03:24:24 +00:00
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
2014-04-05 15:02:50 +00:00
_logger.ErrorException("Error validating IBN entry {0}", ex, person);
}
// Update progress
numComplete++;
double percent = numComplete;
2016-08-07 21:57:46 +00:00
percent /= numPeople;
2014-05-29 14:19:12 +00:00
progress.Report(100 * percent);
}
progress.Report(100);
_logger.Info("People validation complete");
}
}
}