MusicGenresValidator.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Persistence;
  6. using Microsoft.Extensions.Logging;
  7. namespace Emby.Server.Implementations.Library.Validators;
  8. /// <summary>
  9. /// Class MusicGenresValidator.
  10. /// </summary>
  11. public class MusicGenresValidator
  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<MusicGenresValidator> _logger;
  21. private readonly IItemRepository _itemRepo;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="MusicGenresValidator" /> class.
  24. /// </summary>
  25. /// <param name="libraryManager">The library manager.</param>
  26. /// <param name="logger">The logger.</param>
  27. /// <param name="itemRepo">The item repository.</param>
  28. public MusicGenresValidator(ILibraryManager libraryManager, ILogger<MusicGenresValidator> logger, IItemRepository itemRepo)
  29. {
  30. _libraryManager = libraryManager;
  31. _logger = logger;
  32. _itemRepo = itemRepo;
  33. }
  34. /// <summary>
  35. /// Runs the specified progress.
  36. /// </summary>
  37. /// <param name="progress">The progress.</param>
  38. /// <param name="cancellationToken">The cancellation token.</param>
  39. /// <returns>Task.</returns>
  40. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  41. {
  42. var names = _itemRepo.GetMusicGenreNames();
  43. var numComplete = 0;
  44. var count = names.Count;
  45. foreach (var name in names)
  46. {
  47. try
  48. {
  49. var item = _libraryManager.GetMusicGenre(name);
  50. await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  51. }
  52. catch (OperationCanceledException)
  53. {
  54. // Don't clutter the log
  55. throw;
  56. }
  57. catch (Exception ex)
  58. {
  59. _logger.LogError(ex, "Error refreshing {GenreName}", name);
  60. }
  61. numComplete++;
  62. double percent = numComplete;
  63. percent /= count;
  64. percent *= 100;
  65. progress.Report(percent);
  66. }
  67. progress.Report(100);
  68. }
  69. }