SoundtrackPostScanTask.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using MediaBrowser.Controller.Entities.Movies;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Model.Entities;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Providers.Music
  13. {
  14. public class SoundtrackPostScanTask : ILibraryPostScanTask
  15. {
  16. private readonly ILibraryManager _libraryManager;
  17. public SoundtrackPostScanTask(ILibraryManager libraryManager)
  18. {
  19. _libraryManager = libraryManager;
  20. }
  21. public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  22. {
  23. RunInternal(progress, cancellationToken);
  24. return Task.FromResult(true);
  25. }
  26. private void RunInternal(IProgress<double> progress, CancellationToken cancellationToken)
  27. {
  28. var allItems = _libraryManager.RootFolder
  29. .RecursiveChildren
  30. .ToList();
  31. var musicAlbums = allItems
  32. .OfType<MusicAlbum>()
  33. .ToList();
  34. AttachMovieSoundtracks(allItems, musicAlbums, cancellationToken);
  35. progress.Report(25);
  36. AttachTvSoundtracks(allItems, musicAlbums, cancellationToken);
  37. progress.Report(50);
  38. AttachGameSoundtracks(allItems, musicAlbums, cancellationToken);
  39. progress.Report(75);
  40. AttachAlbumLinks(allItems, musicAlbums, cancellationToken);
  41. progress.Report(100);
  42. }
  43. private void AttachMovieSoundtracks(IEnumerable<BaseItem> allItems, List<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  44. {
  45. foreach (var movie in allItems
  46. .Where(i => (i is Movie) || (i is Trailer)))
  47. {
  48. var hasSoundtracks = (IHasSoundtracks) movie;
  49. cancellationToken.ThrowIfCancellationRequested();
  50. var tmdbId = movie.GetProviderId(MetadataProviders.Tmdb);
  51. if (string.IsNullOrEmpty(tmdbId))
  52. {
  53. hasSoundtracks.SoundtrackIds = new List<Guid>();
  54. continue;
  55. }
  56. hasSoundtracks.SoundtrackIds = allAlbums
  57. .Where(i => string.Equals(tmdbId, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase))
  58. .Select(i => i.Id)
  59. .ToList();
  60. }
  61. }
  62. private void AttachTvSoundtracks(IEnumerable<BaseItem> allItems, List<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  63. {
  64. foreach (var series in allItems.OfType<Series>())
  65. {
  66. cancellationToken.ThrowIfCancellationRequested();
  67. var tvdbId = series.GetProviderId(MetadataProviders.Tvdb);
  68. if (string.IsNullOrEmpty(tvdbId))
  69. {
  70. series.SoundtrackIds = new List<Guid>();
  71. continue;
  72. }
  73. series.SoundtrackIds = allAlbums
  74. .Where(i => string.Equals(tvdbId, i.GetProviderId(MetadataProviders.Tvdb), StringComparison.OrdinalIgnoreCase))
  75. .Select(i => i.Id)
  76. .ToList();
  77. }
  78. }
  79. private void AttachGameSoundtracks(IEnumerable<BaseItem> allItems, List<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  80. {
  81. foreach (var game in allItems.OfType<Game>())
  82. {
  83. cancellationToken.ThrowIfCancellationRequested();
  84. var gamesdb = game.GetProviderId(MetadataProviders.Gamesdb);
  85. if (string.IsNullOrEmpty(gamesdb))
  86. {
  87. game.SoundtrackIds = new List<Guid>();
  88. continue;
  89. }
  90. game.SoundtrackIds = allAlbums
  91. .Where(i => string.Equals(gamesdb, i.GetProviderId(MetadataProviders.Gamesdb), StringComparison.OrdinalIgnoreCase))
  92. .Select(i => i.Id)
  93. .ToList();
  94. }
  95. }
  96. private void AttachAlbumLinks(List<BaseItem> allItems, IEnumerable<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  97. {
  98. foreach (var album in allAlbums)
  99. {
  100. cancellationToken.ThrowIfCancellationRequested();
  101. var tmdb = album.GetProviderId(MetadataProviders.Tmdb);
  102. var tvdb = album.GetProviderId(MetadataProviders.Tvdb);
  103. var gamesdb = album.GetProviderId(MetadataProviders.Gamesdb);
  104. if (string.IsNullOrEmpty(tmdb) && string.IsNullOrEmpty(tvdb) && string.IsNullOrEmpty(gamesdb))
  105. {
  106. album.SoundtrackIds = new List<Guid>();
  107. continue;
  108. }
  109. album.SoundtrackIds = allItems.
  110. Where(i =>
  111. {
  112. if (!string.IsNullOrEmpty(tmdb) && string.Equals(tmdb, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase) && i is Movie)
  113. {
  114. return true;
  115. }
  116. if (!string.IsNullOrEmpty(tmdb) && string.Equals(tmdb, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase) && i is Trailer)
  117. {
  118. return true;
  119. }
  120. if (!string.IsNullOrEmpty(tvdb) && string.Equals(tvdb, i.GetProviderId(MetadataProviders.Tvdb), StringComparison.OrdinalIgnoreCase) && i is Series)
  121. {
  122. return true;
  123. }
  124. return !string.IsNullOrEmpty(gamesdb) && string.Equals(gamesdb, i.GetProviderId(MetadataProviders.Gamesdb), StringComparison.OrdinalIgnoreCase) && i is Game;
  125. })
  126. .Select(i => i.Id)
  127. .ToList();
  128. }
  129. }
  130. }
  131. }