GameGenresValidator.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Server.Implementations.Library.Validators
  9. {
  10. class GameGenresValidator
  11. {
  12. /// <summary>
  13. /// The _library manager
  14. /// </summary>
  15. private readonly ILibraryManager _libraryManager;
  16. /// <summary>
  17. /// The _user manager
  18. /// </summary>
  19. private readonly IUserManager _userManager;
  20. /// <summary>
  21. /// The _logger
  22. /// </summary>
  23. private readonly ILogger _logger;
  24. public GameGenresValidator(ILibraryManager libraryManager, IUserManager userManager, ILogger logger)
  25. {
  26. _libraryManager = libraryManager;
  27. _userManager = userManager;
  28. _logger = logger;
  29. }
  30. /// <summary>
  31. /// Runs the specified progress.
  32. /// </summary>
  33. /// <param name="progress">The progress.</param>
  34. /// <param name="cancellationToken">The cancellation token.</param>
  35. /// <returns>Task.</returns>
  36. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  37. {
  38. var items = _libraryManager.RootFolder.RecursiveChildren.Where(i => (i is Game))
  39. .SelectMany(i => i.Genres)
  40. .Distinct(StringComparer.OrdinalIgnoreCase)
  41. .ToList();
  42. progress.Report(2);
  43. var numComplete = 0;
  44. var count = items.Count;
  45. foreach (var name in items)
  46. {
  47. cancellationToken.ThrowIfCancellationRequested();
  48. try
  49. {
  50. var itemByName = _libraryManager.GetGameGenre(name);
  51. await itemByName.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  52. }
  53. catch (OperationCanceledException)
  54. {
  55. // Don't clutter the log
  56. break;
  57. }
  58. catch (Exception ex)
  59. {
  60. _logger.ErrorException("Error refreshing {0}", ex, name);
  61. }
  62. numComplete++;
  63. double percent = numComplete;
  64. percent /= count;
  65. percent *= 90;
  66. progress.Report(percent + 10);
  67. }
  68. progress.Report(100);
  69. }
  70. }
  71. }