GameGenresValidator.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. using MediaBrowser.Controller.Persistence;
  9. namespace Emby.Server.Implementations.Library.Validators
  10. {
  11. class GameGenresValidator
  12. {
  13. /// <summary>
  14. /// The _library manager
  15. /// </summary>
  16. private readonly ILibraryManager _libraryManager;
  17. /// <summary>
  18. /// The _logger
  19. /// </summary>
  20. private readonly ILogger _logger;
  21. private readonly IItemRepository _itemRepo;
  22. public GameGenresValidator(ILibraryManager libraryManager, ILogger logger, IItemRepository itemRepo)
  23. {
  24. _libraryManager = libraryManager;
  25. _logger = logger;
  26. _itemRepo = itemRepo;
  27. }
  28. /// <summary>
  29. /// Runs the specified progress.
  30. /// </summary>
  31. /// <param name="progress">The progress.</param>
  32. /// <param name="cancellationToken">The cancellation token.</param>
  33. /// <returns>Task.</returns>
  34. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  35. {
  36. var names = _itemRepo.GetGameGenreNames();
  37. var numComplete = 0;
  38. var count = names.Count;
  39. foreach (var name in names)
  40. {
  41. try
  42. {
  43. var item = _libraryManager.GetGameGenre(name);
  44. await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  45. }
  46. catch (OperationCanceledException)
  47. {
  48. // Don't clutter the log
  49. throw;
  50. }
  51. catch (Exception ex)
  52. {
  53. _logger.ErrorException("Error refreshing {0}", ex, name);
  54. }
  55. numComplete++;
  56. double percent = numComplete;
  57. percent /= count;
  58. percent *= 100;
  59. progress.Report(percent);
  60. }
  61. progress.Report(100);
  62. }
  63. }
  64. }