GameGenresValidator.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 _logger
  18. /// </summary>
  19. private readonly ILogger _logger;
  20. public GameGenresValidator(ILibraryManager libraryManager, ILogger logger)
  21. {
  22. _libraryManager = libraryManager;
  23. _logger = logger;
  24. }
  25. /// <summary>
  26. /// Runs the specified progress.
  27. /// </summary>
  28. /// <param name="progress">The progress.</param>
  29. /// <param name="cancellationToken">The cancellation token.</param>
  30. /// <returns>Task.</returns>
  31. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  32. {
  33. var items = _libraryManager.GetGameGenres(new InternalItemsQuery
  34. {
  35. IncludeItemTypes = new[] { typeof(Game).Name }
  36. })
  37. .Items
  38. .Select(i => i.Item1)
  39. .ToList();
  40. var numComplete = 0;
  41. var count = items.Count;
  42. foreach (var item in items)
  43. {
  44. try
  45. {
  46. await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  47. }
  48. catch (OperationCanceledException)
  49. {
  50. // Don't clutter the log
  51. break;
  52. }
  53. catch (Exception ex)
  54. {
  55. _logger.ErrorException("Error refreshing {0}", ex, item.Name);
  56. }
  57. numComplete++;
  58. double percent = numComplete;
  59. percent /= count;
  60. percent *= 100;
  61. progress.Report(percent);
  62. }
  63. progress.Report(100);
  64. }
  65. }
  66. }