SoundtrackPostScanTask.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. return Task.Run(() => RunInternal(progress, cancellationToken));
  24. }
  25. private void RunInternal(IProgress<double> progress, CancellationToken cancellationToken)
  26. {
  27. var allItems = _libraryManager.RootFolder
  28. .RecursiveChildren
  29. .ToList();
  30. var musicAlbums = allItems
  31. .OfType<MusicAlbum>()
  32. .ToList();
  33. AttachMovieSoundtracks(allItems, musicAlbums, cancellationToken);
  34. progress.Report(25);
  35. AttachTvSoundtracks(allItems, musicAlbums, cancellationToken);
  36. progress.Report(50);
  37. AttachGameSoundtracks(allItems, musicAlbums, cancellationToken);
  38. progress.Report(75);
  39. AttachAlbumLinks(allItems, musicAlbums, cancellationToken);
  40. progress.Report(100);
  41. }
  42. private void AttachMovieSoundtracks(IEnumerable<BaseItem> allItems, List<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  43. {
  44. foreach (var movie in allItems
  45. .Where(i => (i is Movie) || (i is Trailer)))
  46. {
  47. cancellationToken.ThrowIfCancellationRequested();
  48. var tmdbId = movie.GetProviderId(MetadataProviders.Tmdb);
  49. if (string.IsNullOrEmpty(tmdbId))
  50. {
  51. movie.SoundtrackIds = new List<Guid>();
  52. continue;
  53. }
  54. movie.SoundtrackIds = allAlbums
  55. .Where(i => string.Equals(tmdbId, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase))
  56. .Select(i => i.Id)
  57. .ToList();
  58. }
  59. }
  60. private void AttachTvSoundtracks(IEnumerable<BaseItem> allItems, List<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  61. {
  62. foreach (var series in allItems.OfType<Series>())
  63. {
  64. cancellationToken.ThrowIfCancellationRequested();
  65. var tvdbId = series.GetProviderId(MetadataProviders.Tvdb);
  66. if (string.IsNullOrEmpty(tvdbId))
  67. {
  68. series.SoundtrackIds = new List<Guid>();
  69. continue;
  70. }
  71. series.SoundtrackIds = allAlbums
  72. .Where(i => string.Equals(tvdbId, i.GetProviderId(MetadataProviders.Tvdb), StringComparison.OrdinalIgnoreCase))
  73. .Select(i => i.Id)
  74. .ToList();
  75. }
  76. }
  77. private void AttachGameSoundtracks(IEnumerable<BaseItem> allItems, List<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  78. {
  79. foreach (var game in allItems.OfType<Game>())
  80. {
  81. cancellationToken.ThrowIfCancellationRequested();
  82. var gamesdb = game.GetProviderId(MetadataProviders.Gamesdb);
  83. if (string.IsNullOrEmpty(gamesdb))
  84. {
  85. game.SoundtrackIds = new List<Guid>();
  86. continue;
  87. }
  88. game.SoundtrackIds = allAlbums
  89. .Where(i => string.Equals(gamesdb, i.GetProviderId(MetadataProviders.Gamesdb), StringComparison.OrdinalIgnoreCase))
  90. .Select(i => i.Id)
  91. .ToList();
  92. }
  93. }
  94. private void AttachAlbumLinks(List<BaseItem> allItems, IEnumerable<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  95. {
  96. foreach (var album in allAlbums)
  97. {
  98. cancellationToken.ThrowIfCancellationRequested();
  99. var tmdb = album.GetProviderId(MetadataProviders.Tmdb);
  100. var tvdb = album.GetProviderId(MetadataProviders.Tvdb);
  101. var gamesdb = album.GetProviderId(MetadataProviders.Gamesdb);
  102. if (string.IsNullOrEmpty(tmdb) && string.IsNullOrEmpty(tvdb) && string.IsNullOrEmpty(gamesdb))
  103. {
  104. album.SoundtrackIds = new List<Guid>();
  105. continue;
  106. }
  107. album.SoundtrackIds = allItems.
  108. Where(i =>
  109. {
  110. if (!string.IsNullOrEmpty(tmdb) && string.Equals(tmdb, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase) && i is Movie)
  111. {
  112. return true;
  113. }
  114. if (!string.IsNullOrEmpty(tmdb) && string.Equals(tmdb, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase) && i is Trailer)
  115. {
  116. return true;
  117. }
  118. if (!string.IsNullOrEmpty(tvdb) && string.Equals(tvdb, i.GetProviderId(MetadataProviders.Tvdb), StringComparison.OrdinalIgnoreCase) && i is Series)
  119. {
  120. return true;
  121. }
  122. return !string.IsNullOrEmpty(gamesdb) && string.Equals(gamesdb, i.GetProviderId(MetadataProviders.Gamesdb), StringComparison.OrdinalIgnoreCase) && i is Game;
  123. })
  124. .Select(i => i.Id)
  125. .ToList();
  126. }
  127. }
  128. }
  129. }