SoundtrackPostScanTask.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. cancellationToken.ThrowIfCancellationRequested();
  49. var tmdbId = movie.GetProviderId(MetadataProviders.Tmdb);
  50. if (string.IsNullOrEmpty(tmdbId))
  51. {
  52. movie.SoundtrackIds = new List<Guid>();
  53. continue;
  54. }
  55. movie.SoundtrackIds = allAlbums
  56. .Where(i => string.Equals(tmdbId, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase))
  57. .Select(i => i.Id)
  58. .ToList();
  59. }
  60. }
  61. private void AttachTvSoundtracks(IEnumerable<BaseItem> allItems, List<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  62. {
  63. foreach (var series in allItems.OfType<Series>())
  64. {
  65. cancellationToken.ThrowIfCancellationRequested();
  66. var tvdbId = series.GetProviderId(MetadataProviders.Tvdb);
  67. if (string.IsNullOrEmpty(tvdbId))
  68. {
  69. series.SoundtrackIds = new List<Guid>();
  70. continue;
  71. }
  72. series.SoundtrackIds = allAlbums
  73. .Where(i => string.Equals(tvdbId, i.GetProviderId(MetadataProviders.Tvdb), StringComparison.OrdinalIgnoreCase))
  74. .Select(i => i.Id)
  75. .ToList();
  76. }
  77. }
  78. private void AttachGameSoundtracks(IEnumerable<BaseItem> allItems, List<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  79. {
  80. foreach (var game in allItems.OfType<Game>())
  81. {
  82. cancellationToken.ThrowIfCancellationRequested();
  83. var gamesdb = game.GetProviderId(MetadataProviders.Gamesdb);
  84. if (string.IsNullOrEmpty(gamesdb))
  85. {
  86. game.SoundtrackIds = new List<Guid>();
  87. continue;
  88. }
  89. game.SoundtrackIds = allAlbums
  90. .Where(i => string.Equals(gamesdb, i.GetProviderId(MetadataProviders.Gamesdb), StringComparison.OrdinalIgnoreCase))
  91. .Select(i => i.Id)
  92. .ToList();
  93. }
  94. }
  95. private void AttachAlbumLinks(List<BaseItem> allItems, IEnumerable<MusicAlbum> allAlbums, CancellationToken cancellationToken)
  96. {
  97. foreach (var album in allAlbums)
  98. {
  99. cancellationToken.ThrowIfCancellationRequested();
  100. var tmdb = album.GetProviderId(MetadataProviders.Tmdb);
  101. var tvdb = album.GetProviderId(MetadataProviders.Tvdb);
  102. var gamesdb = album.GetProviderId(MetadataProviders.Gamesdb);
  103. if (string.IsNullOrEmpty(tmdb) && string.IsNullOrEmpty(tvdb) && string.IsNullOrEmpty(gamesdb))
  104. {
  105. album.SoundtrackIds = new List<Guid>();
  106. continue;
  107. }
  108. album.SoundtrackIds = allItems.
  109. Where(i =>
  110. {
  111. if (!string.IsNullOrEmpty(tmdb) && string.Equals(tmdb, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase) && i is Movie)
  112. {
  113. return true;
  114. }
  115. if (!string.IsNullOrEmpty(tmdb) && string.Equals(tmdb, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase) && i is Trailer)
  116. {
  117. return true;
  118. }
  119. if (!string.IsNullOrEmpty(tvdb) && string.Equals(tvdb, i.GetProviderId(MetadataProviders.Tvdb), StringComparison.OrdinalIgnoreCase) && i is Series)
  120. {
  121. return true;
  122. }
  123. return !string.IsNullOrEmpty(gamesdb) && string.Equals(gamesdb, i.GetProviderId(MetadataProviders.Gamesdb), StringComparison.OrdinalIgnoreCase) && i is Game;
  124. })
  125. .Select(i => i.Id)
  126. .ToList();
  127. }
  128. }
  129. }
  130. }