Added instant mixes

This commit is contained in:
Luke Pulverenti 2013-08-09 11:55:22 -04:00
parent a7b07f9631
commit 2173ab0f9f
10 changed files with 207 additions and 17 deletions

View File

@ -10,7 +10,7 @@ namespace MediaBrowser.Api
{ {
[Route("/Albums/{Id}/Similar", "GET")] [Route("/Albums/{Id}/Similar", "GET")]
[Api(Description = "Finds albums similar to a given album.")] [Api(Description = "Finds albums similar to a given album.")]
public class GetSimilarAlbums : BaseGetSimilarItems public class GetSimilarAlbums : BaseGetSimilarItemsFromItem
{ {
} }

View File

@ -10,7 +10,7 @@ namespace MediaBrowser.Api
/// </summary> /// </summary>
[Route("/Games/{Id}/Similar", "GET")] [Route("/Games/{Id}/Similar", "GET")]
[Api(Description = "Finds games similar to a given game.")] [Api(Description = "Finds games similar to a given game.")]
public class GetSimilarGames : BaseGetSimilarItems public class GetSimilarGames : BaseGetSimilarItemsFromItem
{ {
} }

View File

@ -0,0 +1,141 @@
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Querying;
using ServiceStack.ServiceHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
[Route("/Songs/{Id}/InstantMix", "GET")]
[Api(Description = "Creates an instant playlist based on a given song")]
public class GetInstantMixFromSong : BaseGetSimilarItemsFromItem
{
}
[Route("/Albums/{Id}/InstantMix", "GET")]
[Api(Description = "Creates an instant playlist based on a given album")]
public class GetInstantMixFromAlbum : BaseGetSimilarItemsFromItem
{
}
[Route("/Artists/{Name}/InstantMix", "GET")]
[Api(Description = "Creates an instant playlist based on a given artist")]
public class GetInstantMixFromArtist : BaseGetSimilarItems
{
[ApiMember(Name = "Name", Description = "The artist name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Name { get; set; }
}
[Route("/MusicGenres/{Name}/InstantMix", "GET")]
[Api(Description = "Creates an instant playlist based on a music genre")]
public class GetInstantMixFromMusicGenre : BaseGetSimilarItems
{
[ApiMember(Name = "Name", Description = "The genre name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Name { get; set; }
}
public class InstantMixService : BaseApiService
{
private readonly IUserManager _userManager;
private readonly IUserDataRepository _userDataRepository;
private readonly ILibraryManager _libraryManager;
private readonly IItemRepository _itemRepo;
public InstantMixService(IUserManager userManager, IUserDataRepository userDataRepository, ILibraryManager libraryManager, IItemRepository itemRepo)
{
_userManager = userManager;
_userDataRepository = userDataRepository;
_libraryManager = libraryManager;
_itemRepo = itemRepo;
}
public object Get(GetInstantMixFromSong request)
{
var item = DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager);
var result = GetInstantMixResult(request, item.Genres).Result;
return ToOptimizedResult(result);
}
public object Get(GetInstantMixFromAlbum request)
{
var item = DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager);
var result = GetInstantMixResult(request, item.Genres).Result;
return ToOptimizedResult(result);
}
public object Get(GetInstantMixFromMusicGenre request)
{
var genre = GetMusicGenre(request.Name, _libraryManager).Result;
var result = GetInstantMixResult(request, new[] { genre.Name }).Result;
return ToOptimizedResult(result);
}
public object Get(GetInstantMixFromArtist request)
{
var artist = GetArtist(request.Name, _libraryManager).Result;
var genres = _libraryManager.RootFolder
.RecursiveChildren
.OfType<Audio>()
.Where(i => i.HasArtist(artist.Name))
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase);
var result = GetInstantMixResult(request, genres).Result;
return ToOptimizedResult(result);
}
private async Task<ItemsResult> GetInstantMixResult(BaseGetSimilarItems request, IEnumerable<string> genres)
{
var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null;
var fields = request.GetItemFields().ToList();
var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo);
var inputItems = user == null
? _libraryManager.RootFolder.RecursiveChildren
: user.RootFolder.GetRecursiveChildren(user);
var genresDictionary = genres.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
var limit = request.Limit.HasValue ? request.Limit.Value * 2 : 100;
var items = inputItems
.OfType<Audio>()
.Select(i => new Tuple<Audio, int>(i, i.Genres.Count(genresDictionary.ContainsKey)))
.OrderByDescending(i => i.Item2)
.ThenBy(i => Guid.NewGuid())
.Select(i => i.Item1)
.Take(limit)
.OrderBy(i => Guid.NewGuid())
.ToArray();
var result = new ItemsResult
{
TotalRecordCount = items.Length
};
var tasks = items.Take(request.Limit ?? items.Length)
.Select(i => dtoBuilder.GetBaseItemDto(i, fields, user));
result.Items = await Task.WhenAll(tasks).ConfigureAwait(false);
return result;
}
}
}

View File

@ -74,6 +74,7 @@
<Compile Include="Images\ImageRequest.cs" /> <Compile Include="Images\ImageRequest.cs" />
<Compile Include="Images\ImageService.cs" /> <Compile Include="Images\ImageService.cs" />
<Compile Include="Images\ImageWriter.cs" /> <Compile Include="Images\ImageWriter.cs" />
<Compile Include="InstantMixService.cs" />
<Compile Include="ItemRefreshService.cs" /> <Compile Include="ItemRefreshService.cs" />
<Compile Include="ItemUpdateService.cs" /> <Compile Include="ItemUpdateService.cs" />
<Compile Include="LibraryService.cs" /> <Compile Include="LibraryService.cs" />

View File

@ -11,7 +11,7 @@ namespace MediaBrowser.Api
/// </summary> /// </summary>
[Route("/Movies/{Id}/Similar", "GET")] [Route("/Movies/{Id}/Similar", "GET")]
[Api(Description = "Finds movies and trailers similar to a given movie.")] [Api(Description = "Finds movies and trailers similar to a given movie.")]
public class GetSimilarMovies : BaseGetSimilarItems public class GetSimilarMovies : BaseGetSimilarItemsFromItem
{ {
[ApiMember(Name = "IncludeTrailers", Description = "Whether or not to include trailers within the results. Defaults to true.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")] [ApiMember(Name = "IncludeTrailers", Description = "Whether or not to include trailers within the results. Defaults to true.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool IncludeTrailers { get; set; } public bool IncludeTrailers { get; set; }

View File

@ -13,8 +13,18 @@ using System.Linq;
namespace MediaBrowser.Api namespace MediaBrowser.Api
{ {
/// <summary> /// <summary>
/// Class BaseGetSimilarItems /// Class BaseGetSimilarItemsFromItem
/// </summary> /// </summary>
public class BaseGetSimilarItemsFromItem : BaseGetSimilarItems
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
public class BaseGetSimilarItems : IReturn<ItemsResult> public class BaseGetSimilarItems : IReturn<ItemsResult>
{ {
/// <summary> /// <summary>
@ -24,13 +34,6 @@ namespace MediaBrowser.Api
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] [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; } public Guid? UserId { get; set; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
/// <summary> /// <summary>
/// The maximum number of items to return /// The maximum number of items to return
/// </summary> /// </summary>
@ -71,7 +74,7 @@ namespace MediaBrowser.Api
}).Where(i => i.HasValue).Select(i => i.Value); }).Where(i => i.HasValue).Select(i => i.Value);
} }
} }
/// <summary> /// <summary>
/// Class SimilarItemsHelper /// Class SimilarItemsHelper
/// </summary> /// </summary>
@ -89,7 +92,7 @@ namespace MediaBrowser.Api
/// <param name="includeInSearch">The include in search.</param> /// <param name="includeInSearch">The include in search.</param>
/// <param name="getSimilarityScore">The get similarity score.</param> /// <param name="getSimilarityScore">The get similarity score.</param>
/// <returns>ItemsResult.</returns> /// <returns>ItemsResult.</returns>
internal static ItemsResult GetSimilarItemsResult(IUserManager userManager, IItemRepository itemRepository, ILibraryManager libraryManager, IUserDataRepository userDataRepository, ILogger logger, BaseGetSimilarItems request, Func<BaseItem, bool> includeInSearch, Func<BaseItem, BaseItem, int> getSimilarityScore) internal static ItemsResult GetSimilarItemsResult(IUserManager userManager, IItemRepository itemRepository, ILibraryManager libraryManager, IUserDataRepository userDataRepository, ILogger logger, BaseGetSimilarItemsFromItem request, Func<BaseItem, bool> includeInSearch, Func<BaseItem, BaseItem, int> getSimilarityScore)
{ {
var user = request.UserId.HasValue ? userManager.GetUserById(request.UserId.Value) : null; var user = request.UserId.HasValue ? userManager.GetUserById(request.UserId.Value) : null;
@ -105,7 +108,8 @@ namespace MediaBrowser.Api
? libraryManager.RootFolder.RecursiveChildren ? libraryManager.RootFolder.RecursiveChildren
: user.RootFolder.GetRecursiveChildren(user); : user.RootFolder.GetRecursiveChildren(user);
var items = GetSimilaritems(item, inputItems, includeInSearch, getSimilarityScore).ToArray(); var items = GetSimilaritems(item, inputItems, includeInSearch, getSimilarityScore)
.ToArray();
var result = new ItemsResult var result = new ItemsResult
{ {

View File

@ -11,7 +11,7 @@ namespace MediaBrowser.Api
/// </summary> /// </summary>
[Route("/Trailers/{Id}/Similar", "GET")] [Route("/Trailers/{Id}/Similar", "GET")]
[Api(Description = "Finds movies and trailers similar to a given trailer.")] [Api(Description = "Finds movies and trailers similar to a given trailer.")]
public class GetSimilarTrailers : BaseGetSimilarItems public class GetSimilarTrailers : BaseGetSimilarItemsFromItem
{ {
} }

View File

@ -77,7 +77,7 @@ namespace MediaBrowser.Api
[Route("/Shows/{Id}/Similar", "GET")] [Route("/Shows/{Id}/Similar", "GET")]
[Api(Description = "Finds tv shows similar to a given one.")] [Api(Description = "Finds tv shows similar to a given one.")]
public class GetSimilarShows : BaseGetSimilarItems public class GetSimilarShows : BaseGetSimilarItemsFromItem
{ {
} }

View File

@ -319,6 +319,50 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
}); });
}; };
self.getInstantMixFromSong = function (itemId, options) {
var url = self.getUrl("Songs/" + itemId + "/InstantMix", options);
return self.ajax({
type: "GET",
url: url,
dataType: "json"
});
};
self.getInstantMixFromAlbum = function (itemId, options) {
var url = self.getUrl("Albums/" + itemId + "/InstantMix", options);
return self.ajax({
type: "GET",
url: url,
dataType: "json"
});
};
self.getInstantMixFromArtist = function (name, options) {
var url = self.getUrl("Artists/" + self.encodeName(name) + "/InstantMix", options);
return self.ajax({
type: "GET",
url: url,
dataType: "json"
});
};
self.getInstantMixFromMusicGenre = function (name, options) {
var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/InstantMix", options);
return self.ajax({
type: "GET",
url: url,
dataType: "json"
});
};
self.getSimilarMovies = function (itemId, options) { self.getSimilarMovies = function (itemId, options) {
var url = self.getUrl("Movies/" + itemId + "/Similar", options); var url = self.getUrl("Movies/" + itemId + "/Similar", options);

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="MediaBrowser.ApiClient.Javascript" version="3.0.159" targetFramework="net45" /> <package id="MediaBrowser.ApiClient.Javascript" version="3.0.160" targetFramework="net45" />
<package id="ServiceStack.Common" version="3.9.56" targetFramework="net45" /> <package id="ServiceStack.Common" version="3.9.56" targetFramework="net45" />
<package id="ServiceStack.Text" version="3.9.55" targetFramework="net45" /> <package id="ServiceStack.Text" version="3.9.55" targetFramework="net45" />
</packages> </packages>