2013-08-09 15:55:22 +00:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
|
using ServiceStack.ServiceHost;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
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 ILibraryManager _libraryManager;
|
|
|
|
|
|
2013-09-04 17:02:19 +00:00
|
|
|
|
private readonly IDtoService _dtoService;
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
2013-09-04 17:02:19 +00:00
|
|
|
|
public InstantMixService(IUserManager userManager, ILibraryManager libraryManager, IDtoService dtoService)
|
2013-08-09 15:55:22 +00:00
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_libraryManager = libraryManager;
|
2013-09-04 17:02:19 +00:00
|
|
|
|
_dtoService = dtoService;
|
2013-08-09 15:55:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetInstantMixFromSong request)
|
|
|
|
|
{
|
2013-09-04 17:02:19 +00:00
|
|
|
|
var item = _dtoService.GetItemByDtoId(request.Id);
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
2013-09-17 02:44:06 +00:00
|
|
|
|
var result = GetInstantMixResult(request, item.Genres);
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetInstantMixFromAlbum request)
|
|
|
|
|
{
|
2013-09-04 17:02:19 +00:00
|
|
|
|
var album = (MusicAlbum)_dtoService.GetItemByDtoId(request.Id);
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
2013-08-09 16:33:12 +00:00
|
|
|
|
var genres = album
|
|
|
|
|
.RecursiveChildren
|
|
|
|
|
.OfType<Audio>()
|
|
|
|
|
.SelectMany(i => i.Genres)
|
|
|
|
|
.Concat(album.Genres)
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
2013-09-17 02:44:06 +00:00
|
|
|
|
var result = GetInstantMixResult(request, genres);
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetInstantMixFromMusicGenre request)
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var genre = GetMusicGenre(request.Name, _libraryManager);
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
2013-09-17 02:44:06 +00:00
|
|
|
|
var result = GetInstantMixResult(request, new[] { genre.Name });
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetInstantMixFromArtist request)
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var artist = GetArtist(request.Name, _libraryManager);
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
|
|
|
|
var genres = _libraryManager.RootFolder
|
|
|
|
|
.RecursiveChildren
|
|
|
|
|
.OfType<Audio>()
|
|
|
|
|
.Where(i => i.HasArtist(artist.Name))
|
|
|
|
|
.SelectMany(i => i.Genres)
|
2013-08-09 16:33:12 +00:00
|
|
|
|
.Concat(artist.Genres)
|
2013-08-09 15:55:22 +00:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
2013-09-17 02:44:06 +00:00
|
|
|
|
var result = GetInstantMixResult(request, genres);
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 02:44:06 +00:00
|
|
|
|
private ItemsResult GetInstantMixResult(BaseGetSimilarItems request, IEnumerable<string> genres)
|
2013-08-09 15:55:22 +00:00
|
|
|
|
{
|
|
|
|
|
var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null;
|
|
|
|
|
|
|
|
|
|
var fields = request.GetItemFields().ToList();
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-17 02:44:06 +00:00
|
|
|
|
var dtos = items.Take(request.Limit ?? items.Length)
|
2013-09-04 17:02:19 +00:00
|
|
|
|
.Select(i => _dtoService.GetBaseItemDto(i, fields, user));
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
2013-09-17 02:44:06 +00:00
|
|
|
|
result.Items = dtos.ToArray();
|
2013-08-09 15:55:22 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|