From 9821faf5661f4ed06fbc46219bfa843885a1c9bd Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 10 Jul 2013 16:06:11 -0400 Subject: [PATCH] Added new theme song/video endpoints --- MediaBrowser.Api/LibraryService.cs | 128 ++++++++++++++---- MediaBrowser.Api/UserLibrary/ItemsService.cs | 7 + .../Entities/MusicVideo.cs | 23 ++++ MediaBrowser.Model/ApiClient/IApiClient.cs | 27 ++++ .../Querying/ThemeSongsResult.cs | 17 +-- Nuget/MediaBrowser.Common.Internal.nuspec | 4 +- Nuget/MediaBrowser.Common.nuspec | 2 +- Nuget/MediaBrowser.Server.Core.nuspec | 4 +- 8 files changed, 170 insertions(+), 42 deletions(-) diff --git a/MediaBrowser.Api/LibraryService.cs b/MediaBrowser.Api/LibraryService.cs index 17a448c3d..7eed64de1 100644 --- a/MediaBrowser.Api/LibraryService.cs +++ b/MediaBrowser.Api/LibraryService.cs @@ -52,7 +52,7 @@ namespace MediaBrowser.Api /// [Route("/Items/{Id}/ThemeSongs", "GET")] [Api(Description = "Gets theme songs for an item")] - public class GetThemeSongs : IReturn + public class GetThemeSongs : IReturn { /// /// Gets or sets the user id. @@ -67,14 +67,17 @@ namespace MediaBrowser.Api /// The id. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Id { get; set; } + + [ApiMember(Name = "InheritFromParent", Description = "Determines whether or not parent items should be searched for theme media.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] + public bool InheritFromParent { get; set; } } /// /// Class GetThemeVideos /// [Route("/Items/{Id}/ThemeVideos", "GET")] - [Api(Description = "Gets video backdrops for an item")] - public class GetThemeVideos : IReturn + [Api(Description = "Gets theme videos for an item")] + public class GetThemeVideos : IReturn { /// /// Gets or sets the user id. @@ -89,6 +92,34 @@ namespace MediaBrowser.Api /// The id. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Id { get; set; } + + [ApiMember(Name = "InheritFromParent", Description = "Determines whether or not parent items should be searched for theme media.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] + public bool InheritFromParent { get; set; } + } + + /// + /// Class GetThemeVideos + /// + [Route("/Items/{Id}/ThemeMedia", "GET")] + [Api(Description = "Gets theme videos and songs for an item")] + public class GetThemeMedia : IReturn + { + /// + /// Gets or sets the user id. + /// + /// The user id. + [ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] + public Guid? UserId { get; set; } + + /// + /// Gets or sets the id. + /// + /// The id. + [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] + public string Id { get; set; } + + [ApiMember(Name = "InheritFromParent", Description = "Determines whether or not parent items should be searched for theme media.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] + public bool InheritFromParent { get; set; } } [Route("/Library/Refresh", "POST")] @@ -112,7 +143,7 @@ namespace MediaBrowser.Api [ApiMember(Name = "UserId", Description = "Optional. Get counts from a specific user's library.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public Guid? UserId { get; set; } } - + /// /// Class LibraryService /// @@ -230,7 +261,7 @@ namespace MediaBrowser.Api { throw new InvalidOperationException(string.Format("{0} is currently offline.", item.Name)); } - + if (item.LocationType == LocationType.FileSystem) { if (Directory.Exists(item.Path)) @@ -303,12 +334,44 @@ namespace MediaBrowser.Api return result; } + public object Get(GetThemeMedia request) + { + var themeSongs = GetThemeSongs(new GetThemeSongs + { + InheritFromParent = request.InheritFromParent, + Id = request.Id, + UserId = request.UserId + + }).Result; + + var themeVideos = GetThemeVideos(new GetThemeVideos + { + InheritFromParent = request.InheritFromParent, + Id = request.Id, + UserId = request.UserId + + }).Result; + + return ToOptimizedResult(new AllThemeMediaResult + { + ThemeSongsResult = themeSongs, + ThemeVideosResult = themeVideos + }); + } + /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetThemeSongs request) + { + var result = GetThemeSongs(request).Result; + + return ToOptimizedResult(result); + } + + private async Task GetThemeSongs(GetThemeSongs request) { var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null; @@ -318,6 +381,11 @@ namespace MediaBrowser.Api : (Folder)_libraryManager.RootFolder) : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, request.UserId); + while (item.ThemeSongIds.Count == 0 && request.InheritFromParent && item.Parent != null) + { + item = item.Parent; + } + // Get everything var fields = Enum.GetNames(typeof(ItemFields)) .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)) @@ -325,20 +393,18 @@ namespace MediaBrowser.Api var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo); - var items = item.ThemeSongIds.Select(_itemRepo.RetrieveItem) - .OrderBy(i => i.SortName) - .Select(i => dtoBuilder.GetBaseItemDto(i, fields, user)) - .Select(t => t.Result) - .ToArray(); + var tasks = item.ThemeSongIds.Select(_itemRepo.RetrieveItem) + .OrderBy(i => i.SortName) + .Select(i => dtoBuilder.GetBaseItemDto(i, fields, user)); - var result = new ThemeSongsResult + var items = await Task.WhenAll(tasks).ConfigureAwait(false); + + return new ThemeMediaResult { Items = items, TotalRecordCount = items.Length, OwnerId = DtoBuilder.GetClientItemId(item) }; - - return ToOptimizedResult(result); } /// @@ -347,6 +413,13 @@ namespace MediaBrowser.Api /// The request. /// System.Object. public object Get(GetThemeVideos request) + { + var result = GetThemeVideos(request).Result; + + return ToOptimizedResult(result); + } + + public async Task GetThemeVideos(GetThemeVideos request) { var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null; @@ -356,6 +429,11 @@ namespace MediaBrowser.Api : (Folder)_libraryManager.RootFolder) : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, request.UserId); + while (item.ThemeVideoIds.Count == 0 && request.InheritFromParent && item.Parent != null) + { + item = item.Parent; + } + // Get everything var fields = Enum.GetNames(typeof(ItemFields)) @@ -364,22 +442,18 @@ namespace MediaBrowser.Api var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo); - var items = - item.ThemeVideoIds.Select(_itemRepo.RetrieveItem) - .OrderBy(i => i.SortName) - .Select(i => dtoBuilder.GetBaseItemDto(i, fields, user)) - .Select(t => t.Result) - .ToArray(); + var tasks = item.ThemeVideoIds.Select(_itemRepo.RetrieveItem) + .OrderBy(i => i.SortName) + .Select(i => dtoBuilder.GetBaseItemDto(i, fields, user)); - var result = new ThemeVideosResult - { - Items = items, - TotalRecordCount = items.Length, - OwnerId = DtoBuilder.GetClientItemId(item) - }; + var items = await Task.WhenAll(tasks).ConfigureAwait(false); - return ToOptimizedResult(result); + return new ThemeMediaResult + { + Items = items, + TotalRecordCount = items.Length, + OwnerId = DtoBuilder.GetClientItemId(item) + }; } - } } diff --git a/MediaBrowser.Api/UserLibrary/ItemsService.cs b/MediaBrowser.Api/UserLibrary/ItemsService.cs index 8497901f6..b52696efa 100644 --- a/MediaBrowser.Api/UserLibrary/ItemsService.cs +++ b/MediaBrowser.Api/UserLibrary/ItemsService.cs @@ -425,6 +425,13 @@ namespace MediaBrowser.Api.UserLibrary return artists.Any(album.HasArtist); } + var musicVideo = i as MusicVideo; + + if (musicVideo != null) + { + return artists.Any(musicVideo.HasArtist); + } + return false; }); } diff --git a/MediaBrowser.Controller/Entities/MusicVideo.cs b/MediaBrowser.Controller/Entities/MusicVideo.cs index 035709bb4..09f99f500 100644 --- a/MediaBrowser.Controller/Entities/MusicVideo.cs +++ b/MediaBrowser.Controller/Entities/MusicVideo.cs @@ -1,10 +1,23 @@ using MediaBrowser.Model.Entities; +using System; using System.Runtime.Serialization; namespace MediaBrowser.Controller.Entities { public class MusicVideo : Video { + /// + /// Gets or sets the artist. + /// + /// The artist. + public string Artist { get; set; } + + /// + /// Gets or sets the album. + /// + /// The album. + public string Album { get; set; } + /// /// Should be overridden to return the proper folder where metadata lives /// @@ -18,6 +31,16 @@ namespace MediaBrowser.Controller.Entities } } + /// + /// Determines whether the specified name has artist. + /// + /// The name. + /// true if the specified name has artist; otherwise, false. + public bool HasArtist(string name) + { + return string.Equals(Artist, name, StringComparison.OrdinalIgnoreCase); + } + /// /// Gets the user data key. /// diff --git a/MediaBrowser.Model/ApiClient/IApiClient.cs b/MediaBrowser.Model/ApiClient/IApiClient.cs index 839fb0124..9c4a0f364 100644 --- a/MediaBrowser.Model/ApiClient/IApiClient.cs +++ b/MediaBrowser.Model/ApiClient/IApiClient.cs @@ -23,6 +23,33 @@ namespace MediaBrowser.Model.ApiClient /// event EventHandler HttpResponseReceived; + /// + /// Gets the theme songs async. + /// + /// The user id. + /// The item id. + /// if set to true [inherit from parents]. + /// Task{ThemeMediaResult}. + Task GetThemeSongsAsync(string userId, string itemId, bool inheritFromParents); + + /// + /// Gets the theme videos async. + /// + /// The user id. + /// The item id. + /// if set to true [inherit from parents]. + /// Task{ThemeMediaResult}. + Task GetThemeVideosAsync(string userId, string itemId, bool inheritFromParents); + + /// + /// Gets all theme media async. + /// + /// The user id. + /// The item id. + /// if set to true [inherit from parents]. + /// Task{AllThemeMediaResult}. + Task GetAllThemeMediaAsync(string userId, string itemId, bool inheritFromParents); + /// /// Marks the notifications read. /// diff --git a/MediaBrowser.Model/Querying/ThemeSongsResult.cs b/MediaBrowser.Model/Querying/ThemeSongsResult.cs index 919709f0f..f62219c69 100644 --- a/MediaBrowser.Model/Querying/ThemeSongsResult.cs +++ b/MediaBrowser.Model/Querying/ThemeSongsResult.cs @@ -1,11 +1,10 @@ -using System; - + namespace MediaBrowser.Model.Querying { /// - /// Class ThemeSongsResult + /// Class ThemeMediaResult /// - public class ThemeSongsResult : ItemsResult + public class ThemeMediaResult : ItemsResult { /// /// Gets or sets the owner id. @@ -14,12 +13,10 @@ namespace MediaBrowser.Model.Querying public string OwnerId { get; set; } } - public class ThemeVideosResult : ItemsResult + public class AllThemeMediaResult { - /// - /// Gets or sets the owner id. - /// - /// The owner id. - public string OwnerId { get; set; } + public ThemeMediaResult ThemeVideosResult { get; set; } + + public ThemeMediaResult ThemeSongsResult { get; set; } } } diff --git a/Nuget/MediaBrowser.Common.Internal.nuspec b/Nuget/MediaBrowser.Common.Internal.nuspec index 4a86d14e2..3c604133d 100644 --- a/Nuget/MediaBrowser.Common.Internal.nuspec +++ b/Nuget/MediaBrowser.Common.Internal.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common.Internal - 3.0.144 + 3.0.145 MediaBrowser.Common.Internal Luke ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains common components shared by Media Browser Theater and Media Browser Server. Not intended for plugin developer consumption. Copyright © Media Browser 2013 - + diff --git a/Nuget/MediaBrowser.Common.nuspec b/Nuget/MediaBrowser.Common.nuspec index cb86b71af..06c1804f1 100644 --- a/Nuget/MediaBrowser.Common.nuspec +++ b/Nuget/MediaBrowser.Common.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common - 3.0.144 + 3.0.145 MediaBrowser.Common Media Browser Team ebr,Luke,scottisafool diff --git a/Nuget/MediaBrowser.Server.Core.nuspec b/Nuget/MediaBrowser.Server.Core.nuspec index 2048aa173..5624cda40 100644 --- a/Nuget/MediaBrowser.Server.Core.nuspec +++ b/Nuget/MediaBrowser.Server.Core.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Server.Core - 3.0.144 + 3.0.145 Media Browser.Server.Core Media Browser Team ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains core components required to build plugins for Media Browser Server. Copyright © Media Browser 2013 - +