From 9758adb8a57e3b43013d9feabdf413af6c964a0f Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 12 Nov 2013 10:36:08 -0500 Subject: [PATCH] add soundtracks to theme media result --- MediaBrowser.Api/LibraryService.cs | 68 ++++++++++++++++--- .../ScheduledTasks/Tasks/SystemUpdateTask.cs | 1 + .../Entities/Audio/MusicAlbum.cs | 3 + MediaBrowser.Controller/Entities/BaseItem.cs | 6 -- MediaBrowser.Controller/Entities/Game.cs | 6 +- .../Entities/IHasSoundtracks.cs | 17 +++++ .../Entities/Movies/Movie.cs | 5 +- .../Entities/TV/Episode.cs | 3 + MediaBrowser.Controller/Entities/TV/Season.cs | 4 ++ MediaBrowser.Controller/Entities/TV/Series.cs | 4 +- MediaBrowser.Controller/Entities/Trailer.cs | 6 +- .../MediaBrowser.Controller.csproj | 1 + .../Querying/ThemeSongsResult.cs | 4 ++ .../Music/SoundtrackPostScanTask.cs | 6 +- .../Dto/DtoService.cs | 22 +++--- 15 files changed, 125 insertions(+), 31 deletions(-) create mode 100644 MediaBrowser.Controller/Entities/IHasSoundtracks.cs diff --git a/MediaBrowser.Api/LibraryService.cs b/MediaBrowser.Api/LibraryService.cs index aa2264398..f03d79795 100644 --- a/MediaBrowser.Api/LibraryService.cs +++ b/MediaBrowser.Api/LibraryService.cs @@ -116,7 +116,7 @@ namespace MediaBrowser.Api /// [Route("/Items/{Id}/ThemeMedia", "GET")] [Api(Description = "Gets theme videos and songs for an item")] - public class GetThemeMedia : IReturn + public class GetThemeMedia : IReturn { /// /// Gets or sets the user id. @@ -435,11 +435,6 @@ namespace MediaBrowser.Api return items; } - - private int FavoriteCount(IEnumerable items, Guid userId) - { - return items.AsParallel().Count(i => _userDataManager.GetUserData(userId, i.GetUserDataKey()).IsFavorite); - } /// /// Posts the specified request. @@ -572,7 +567,9 @@ namespace MediaBrowser.Api return ToOptimizedResult(new AllThemeMediaResult { ThemeSongsResult = themeSongs, - ThemeVideosResult = themeVideos + ThemeVideosResult = themeVideos, + + SoundtrackSongsResult = GetSoundtrackSongs(request.Id, request.UserId, request.InheritFromParent) }); } @@ -650,8 +647,7 @@ namespace MediaBrowser.Api } // Get everything - var fields = - Enum.GetNames(typeof(ItemFields)) + var fields = Enum.GetNames(typeof(ItemFields)) .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)) .ToList(); @@ -669,7 +665,7 @@ namespace MediaBrowser.Api }; } - private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); public object Get(GetYearIndex request) { @@ -687,11 +683,61 @@ namespace MediaBrowser.Api .Select(i => new ItemIndex { ItemCount = i.Count(), - Name = i.Key == -1 ? string.Empty : i.Key.ToString(UsCulture) + Name = i.Key == -1 ? string.Empty : i.Key.ToString(_usCulture) }) .ToList(); return ToOptimizedResult(lookup); } + + public ThemeMediaResult GetSoundtrackSongs(string id, Guid? userId, bool inheritFromParent) + { + var user = userId.HasValue ? _userManager.GetUserById(userId.Value) : null; + + var item = string.IsNullOrEmpty(id) + ? (userId.HasValue + ? user.RootFolder + : (Folder)_libraryManager.RootFolder) + : _dtoService.GetItemByDtoId(id, userId); + + while (GetSoundtrackSongIds(item).Count == 0 && inheritFromParent && item.Parent != null) + { + item = item.Parent; + } + + // Get everything + var fields = Enum.GetNames(typeof(ItemFields)) + .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)) + .ToList(); + + var dtos = GetSoundtrackSongIds(item) + .Select(_libraryManager.GetItemById) + .OfType() + .SelectMany(i => i.RecursiveChildren) + .OfType public class MusicAlbum : Folder, IHasAlbumArtist, IHasArtist, IHasMusicGenres { + public List SoundtrackIds { get; set; } + public MusicAlbum() { Artists = new List(); + SoundtrackIds = new List(); } public string LastFmImageUrl { get; set; } diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index f8b2fad23..9cd839b21 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -25,11 +25,6 @@ namespace MediaBrowser.Controller.Entities /// public abstract class BaseItem : IHasProviderIds, ILibraryItem { - /// - /// MusicAlbums in the library that are the soundtrack for this item - /// - public List SoundtrackIds { get; set; } - protected BaseItem() { Genres = new List(); @@ -43,7 +38,6 @@ namespace MediaBrowser.Controller.Entities Tags = new List(); ThemeSongIds = new List(); ThemeVideoIds = new List(); - SoundtrackIds = new List(); LocalTrailerIds = new List(); LockedFields = new List(); Taglines = new List(); diff --git a/MediaBrowser.Controller/Entities/Game.cs b/MediaBrowser.Controller/Entities/Game.cs index 8a33eeb86..e8374c274 100644 --- a/MediaBrowser.Controller/Entities/Game.cs +++ b/MediaBrowser.Controller/Entities/Game.cs @@ -1,13 +1,17 @@ using MediaBrowser.Model.Entities; +using System; using System.Collections.Generic; namespace MediaBrowser.Controller.Entities { - public class Game : BaseItem + public class Game : BaseItem, IHasSoundtracks { + public List SoundtrackIds { get; set; } + public Game() { MultiPartGameFiles = new List(); + SoundtrackIds = new List(); } /// diff --git a/MediaBrowser.Controller/Entities/IHasSoundtracks.cs b/MediaBrowser.Controller/Entities/IHasSoundtracks.cs new file mode 100644 index 000000000..31defc9f4 --- /dev/null +++ b/MediaBrowser.Controller/Entities/IHasSoundtracks.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; + +namespace MediaBrowser.Controller.Entities +{ + /// + /// Interface IHasSoundtracks + /// + public interface IHasSoundtracks + { + /// + /// Gets or sets the soundtrack ids. + /// + /// The soundtrack ids. + List SoundtrackIds { get; set; } + } +} diff --git a/MediaBrowser.Controller/Entities/Movies/Movie.cs b/MediaBrowser.Controller/Entities/Movies/Movie.cs index 54ad9c1c9..eef348f61 100644 --- a/MediaBrowser.Controller/Entities/Movies/Movie.cs +++ b/MediaBrowser.Controller/Entities/Movies/Movie.cs @@ -11,13 +11,16 @@ namespace MediaBrowser.Controller.Entities.Movies /// /// Class Movie /// - public class Movie : Video, IHasCriticRating + public class Movie : Video, IHasCriticRating, IHasSoundtracks { public List SpecialFeatureIds { get; set; } + public List SoundtrackIds { get; set; } + public Movie() { SpecialFeatureIds = new List(); + SoundtrackIds = new List(); } /// diff --git a/MediaBrowser.Controller/Entities/TV/Episode.cs b/MediaBrowser.Controller/Entities/TV/Episode.cs index 96b120b8f..95d346733 100644 --- a/MediaBrowser.Controller/Entities/TV/Episode.cs +++ b/MediaBrowser.Controller/Entities/TV/Episode.cs @@ -193,6 +193,7 @@ namespace MediaBrowser.Controller.Entities.TV return false; } + [IgnoreDataMember] public bool IsMissingEpisode { get @@ -201,11 +202,13 @@ namespace MediaBrowser.Controller.Entities.TV } } + [IgnoreDataMember] public bool IsUnaired { get { return PremiereDate.HasValue && PremiereDate.Value.ToLocalTime().Date >= DateTime.Now.Date; } } + [IgnoreDataMember] public bool IsVirtualUnaired { get { return LocationType == Model.Entities.LocationType.Virtual && IsUnaired; } diff --git a/MediaBrowser.Controller/Entities/TV/Season.cs b/MediaBrowser.Controller/Entities/TV/Season.cs index 5a53e8c0f..97a09b7b5 100644 --- a/MediaBrowser.Controller/Entities/TV/Season.cs +++ b/MediaBrowser.Controller/Entities/TV/Season.cs @@ -149,21 +149,25 @@ namespace MediaBrowser.Controller.Entities.TV return IndexNumber != null ? IndexNumber.Value.ToString("0000") : Name; } + [IgnoreDataMember] public bool IsMissingSeason { get { return LocationType == Model.Entities.LocationType.Virtual && Children.OfType().All(i => i.IsMissingEpisode); } } + [IgnoreDataMember] public bool IsUnaired { get { return Children.OfType().All(i => i.IsUnaired); } } + [IgnoreDataMember] public bool IsVirtualUnaired { get { return LocationType == Model.Entities.LocationType.Virtual && IsUnaired; } } + [IgnoreDataMember] public bool IsMissingOrVirtualUnaired { get { return LocationType == Model.Entities.LocationType.Virtual && Children.OfType().All(i => i.IsVirtualUnaired || i.IsMissingEpisode); } diff --git a/MediaBrowser.Controller/Entities/TV/Series.cs b/MediaBrowser.Controller/Entities/TV/Series.cs index 11fb198c2..02ea50c6b 100644 --- a/MediaBrowser.Controller/Entities/TV/Series.cs +++ b/MediaBrowser.Controller/Entities/TV/Series.cs @@ -11,9 +11,10 @@ namespace MediaBrowser.Controller.Entities.TV /// /// Class Series /// - public class Series : Folder + public class Series : Folder, IHasSoundtracks { public List SpecialFeatureIds { get; set; } + public List SoundtrackIds { get; set; } public int SeasonCount { get; set; } @@ -22,6 +23,7 @@ namespace MediaBrowser.Controller.Entities.TV AirDays = new List(); SpecialFeatureIds = new List(); + SoundtrackIds = new List(); } /// diff --git a/MediaBrowser.Controller/Entities/Trailer.cs b/MediaBrowser.Controller/Entities/Trailer.cs index c9fe471b3..26814ad40 100644 --- a/MediaBrowser.Controller/Entities/Trailer.cs +++ b/MediaBrowser.Controller/Entities/Trailer.cs @@ -1,4 +1,5 @@ using MediaBrowser.Model.Entities; +using System; using System.Collections.Generic; using System.Runtime.Serialization; @@ -7,12 +8,15 @@ namespace MediaBrowser.Controller.Entities /// /// Class Trailer /// - public class Trailer : Video, IHasCriticRating + public class Trailer : Video, IHasCriticRating, IHasSoundtracks { + public List SoundtrackIds { get; set; } + public Trailer() { RemoteTrailers = new List(); Taglines = new List(); + SoundtrackIds = new List(); } /// diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 94ff5d305..f2837a1f1 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -90,6 +90,7 @@ + diff --git a/MediaBrowser.Model/Querying/ThemeSongsResult.cs b/MediaBrowser.Model/Querying/ThemeSongsResult.cs index 4c01e3b3d..9b0a1c61b 100644 --- a/MediaBrowser.Model/Querying/ThemeSongsResult.cs +++ b/MediaBrowser.Model/Querying/ThemeSongsResult.cs @@ -19,11 +19,15 @@ namespace MediaBrowser.Model.Querying public ThemeMediaResult ThemeSongsResult { get; set; } + public ThemeMediaResult SoundtrackSongsResult { get; set; } + public AllThemeMediaResult() { ThemeVideosResult = new ThemeMediaResult(); ThemeSongsResult = new ThemeMediaResult(); + + SoundtrackSongsResult = new ThemeMediaResult(); } } } diff --git a/MediaBrowser.Providers/Music/SoundtrackPostScanTask.cs b/MediaBrowser.Providers/Music/SoundtrackPostScanTask.cs index e18351248..a14f387b1 100644 --- a/MediaBrowser.Providers/Music/SoundtrackPostScanTask.cs +++ b/MediaBrowser.Providers/Music/SoundtrackPostScanTask.cs @@ -60,17 +60,19 @@ namespace MediaBrowser.Providers.Music foreach (var movie in allItems .Where(i => (i is Movie) || (i is Trailer))) { + var hasSoundtracks = (IHasSoundtracks) movie; + cancellationToken.ThrowIfCancellationRequested(); var tmdbId = movie.GetProviderId(MetadataProviders.Tmdb); if (string.IsNullOrEmpty(tmdbId)) { - movie.SoundtrackIds = new List(); + hasSoundtracks.SoundtrackIds = new List(); continue; } - movie.SoundtrackIds = allAlbums + hasSoundtracks.SoundtrackIds = allAlbums .Where(i => string.Equals(tmdbId, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase)) .Select(i => i.Id) .ToList(); diff --git a/MediaBrowser.Server.Implementations/Dto/DtoService.cs b/MediaBrowser.Server.Implementations/Dto/DtoService.cs index d327796f9..31b0c1a6a 100644 --- a/MediaBrowser.Server.Implementations/Dto/DtoService.cs +++ b/MediaBrowser.Server.Implementations/Dto/DtoService.cs @@ -102,9 +102,14 @@ namespace MediaBrowser.Server.Implementations.Dto if (fields.Contains(ItemFields.SoundtrackIds)) { - dto.SoundtrackIds = item.SoundtrackIds - .Select(i => i.ToString("N")) - .ToArray(); + var hasSoundtracks = item as IHasSoundtracks; + + if (hasSoundtracks != null) + { + dto.SoundtrackIds = hasSoundtracks.SoundtrackIds + .Select(i => i.ToString("N")) + .ToArray(); + } } var itemByName = item as IItemByName; @@ -131,12 +136,9 @@ namespace MediaBrowser.Server.Implementations.Dto //counts = item.ItemCounts; return; } - else + if (!item.UserItemCounts.TryGetValue(user.Id, out counts)) { - if (!item.UserItemCounts.TryGetValue(user.Id, out counts)) - { - counts = new ItemByNameCounts(); - } + counts = new ItemByNameCounts(); } dto.ChildCount = counts.TotalCount; @@ -967,6 +969,10 @@ namespace MediaBrowser.Server.Implementations.Dto if (album != null) { dto.Artists = album.Artists; + + dto.SoundtrackIds = album.SoundtrackIds + .Select(i => i.ToString("N")) + .ToArray(); } var hasAlbumArtist = item as IHasAlbumArtist;