PeopleValidator.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using MediaBrowser.Common.Progress;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Server.Implementations.Library.Validators
  10. {
  11. /// <summary>
  12. /// Class PeopleValidator
  13. /// </summary>
  14. public class PeopleValidator
  15. {
  16. /// <summary>
  17. /// The _library manager
  18. /// </summary>
  19. private readonly ILibraryManager _libraryManager;
  20. /// <summary>
  21. /// The _logger
  22. /// </summary>
  23. private readonly ILogger _logger;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="PeopleValidator" /> class.
  26. /// </summary>
  27. /// <param name="libraryManager">The library manager.</param>
  28. /// <param name="logger">The logger.</param>
  29. public PeopleValidator(ILibraryManager libraryManager, ILogger logger)
  30. {
  31. _libraryManager = libraryManager;
  32. _logger = logger;
  33. }
  34. /// <summary>
  35. /// Validates the people.
  36. /// </summary>
  37. /// <param name="cancellationToken">The cancellation token.</param>
  38. /// <param name="options">The options.</param>
  39. /// <param name="progress">The progress.</param>
  40. /// <returns>Task.</returns>
  41. public async Task ValidatePeople(CancellationToken cancellationToken, MetadataRefreshOptions options, IProgress<double> progress)
  42. {
  43. var innerProgress = new ActionableProgress<double>();
  44. innerProgress.RegisterAction(pct => progress.Report(pct * .15));
  45. var people = _libraryManager.RootFolder.GetRecursiveChildren()
  46. .SelectMany(c => c.People)
  47. .Where(i => !string.IsNullOrWhiteSpace(i.Name))
  48. .Select(i => i.Name)
  49. .Distinct(StringComparer.OrdinalIgnoreCase)
  50. .ToList();
  51. var numComplete = 0;
  52. foreach (var person in people)
  53. {
  54. cancellationToken.ThrowIfCancellationRequested();
  55. try
  56. {
  57. var item = _libraryManager.GetPerson(person);
  58. await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
  59. }
  60. catch (Exception ex)
  61. {
  62. _logger.ErrorException("Error validating IBN entry {0}", ex, person);
  63. }
  64. // Update progress
  65. numComplete++;
  66. double percent = numComplete;
  67. percent /= people.Count;
  68. progress.Report(100 * percent);
  69. }
  70. progress.Report(100);
  71. _logger.Info("People validation complete");
  72. // Bad practice, i know. But we keep a lot in memory, unfortunately.
  73. GC.Collect(2, GCCollectionMode.Forced, true);
  74. GC.Collect(2, GCCollectionMode.Forced, true);
  75. }
  76. }
  77. }