GenresValidator.cs 2.3 KB

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