2013-08-09 08:55:22 -07:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2014-03-30 09:49:40 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-08-09 08:55:22 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-07-02 11:34:08 -07:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2014-12-12 20:56:30 -07:00
|
|
|
|
using MediaBrowser.Controller.Playlists;
|
2013-08-09 08:55:22 -07:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
2013-12-07 08:52:38 -07:00
|
|
|
|
using ServiceStack;
|
2013-08-09 08:55:22 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2016-06-18 23:18:29 -07:00
|
|
|
|
using System.Threading.Tasks;
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2014-03-05 22:17:13 -07:00
|
|
|
|
namespace MediaBrowser.Api.Music
|
2013-08-09 08:55:22 -07:00
|
|
|
|
{
|
2014-03-25 14:13:55 -07:00
|
|
|
|
[Route("/Songs/{Id}/InstantMix", "GET", Summary = "Creates an instant playlist based on a given song")]
|
2013-08-09 08:55:22 -07:00
|
|
|
|
public class GetInstantMixFromSong : BaseGetSimilarItemsFromItem
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 14:13:55 -07:00
|
|
|
|
[Route("/Albums/{Id}/InstantMix", "GET", Summary = "Creates an instant playlist based on a given album")]
|
2013-08-09 08:55:22 -07:00
|
|
|
|
public class GetInstantMixFromAlbum : BaseGetSimilarItemsFromItem
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-12 20:56:30 -07:00
|
|
|
|
[Route("/Playlists/{Id}/InstantMix", "GET", Summary = "Creates an instant playlist based on a given playlist")]
|
|
|
|
|
public class GetInstantMixFromPlaylist : BaseGetSimilarItemsFromItem
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 14:13:55 -07:00
|
|
|
|
[Route("/Artists/{Name}/InstantMix", "GET", Summary = "Creates an instant playlist based on a given artist")]
|
2013-08-09 08:55:22 -07:00
|
|
|
|
public class GetInstantMixFromArtist : BaseGetSimilarItems
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Name", Description = "The artist name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 14:13:55 -07:00
|
|
|
|
[Route("/MusicGenres/{Name}/InstantMix", "GET", Summary = "Creates an instant playlist based on a music genre")]
|
2013-08-09 08:55:22 -07:00
|
|
|
|
public class GetInstantMixFromMusicGenre : BaseGetSimilarItems
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Name", Description = "The genre name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-18 07:20:02 -07:00
|
|
|
|
[Route("/Artists/InstantMix", "GET", Summary = "Creates an instant playlist based on a given artist")]
|
|
|
|
|
public class GetInstantMixFromArtistId : BaseGetSimilarItems
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Id", Description = "The artist Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
|
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Route("/MusicGenres/InstantMix", "GET", Summary = "Creates an instant playlist based on a music genre")]
|
|
|
|
|
public class GetInstantMixFromMusicGenreId : BaseGetSimilarItems
|
|
|
|
|
{
|
2015-05-25 12:08:05 -07:00
|
|
|
|
[ApiMember(Name = "Id", Description = "The genre Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
|
2014-08-18 07:20:02 -07:00
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-27 21:19:08 -07:00
|
|
|
|
[Route("/Items/{Id}/InstantMix", "GET", Summary = "Creates an instant playlist based on a given item")]
|
|
|
|
|
public class GetInstantMixFromItem : BaseGetSimilarItemsFromItem
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-02 11:34:08 -07:00
|
|
|
|
[Authenticated]
|
2013-08-09 08:55:22 -07:00
|
|
|
|
public class InstantMixService : BaseApiService
|
|
|
|
|
{
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
2013-09-04 10:02:19 -07:00
|
|
|
|
private readonly IDtoService _dtoService;
|
2014-04-25 13:15:50 -07:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-03-30 09:49:40 -07:00
|
|
|
|
private readonly IMusicManager _musicManager;
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2014-04-25 13:15:50 -07:00
|
|
|
|
public InstantMixService(IUserManager userManager, IDtoService dtoService, IMusicManager musicManager, ILibraryManager libraryManager)
|
2013-08-09 08:55:22 -07:00
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
2013-09-04 10:02:19 -07:00
|
|
|
|
_dtoService = dtoService;
|
2014-03-30 09:49:40 -07:00
|
|
|
|
_musicManager = musicManager;
|
2014-04-25 13:15:50 -07:00
|
|
|
|
_libraryManager = libraryManager;
|
2013-08-09 08:55:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
public Task<object> Get(GetInstantMixFromItem request)
|
2015-08-27 21:19:08 -07:00
|
|
|
|
{
|
|
|
|
|
var item = _libraryManager.GetItemById(request.Id);
|
|
|
|
|
|
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
|
|
|
|
|
|
|
|
|
var items = _musicManager.GetInstantMixFromItem(item, user);
|
|
|
|
|
|
|
|
|
|
return GetResult(items, user, request);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
public Task<object> Get(GetInstantMixFromArtistId request)
|
2014-08-18 07:20:02 -07:00
|
|
|
|
{
|
2015-01-26 09:47:15 -07:00
|
|
|
|
var item = _libraryManager.GetItemById(request.Id);
|
2014-08-18 07:20:02 -07:00
|
|
|
|
|
2015-05-29 16:51:33 -07:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2014-08-18 07:20:02 -07:00
|
|
|
|
|
2015-01-26 09:47:15 -07:00
|
|
|
|
var items = _musicManager.GetInstantMixFromItem(item, user);
|
2014-08-18 07:20:02 -07:00
|
|
|
|
|
|
|
|
|
return GetResult(items, user, request);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
public Task<object> Get(GetInstantMixFromMusicGenreId request)
|
2014-08-18 07:20:02 -07:00
|
|
|
|
{
|
2015-01-26 09:47:15 -07:00
|
|
|
|
var item = _libraryManager.GetItemById(request.Id);
|
2014-08-18 07:20:02 -07:00
|
|
|
|
|
2015-05-29 16:51:33 -07:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2014-08-18 07:20:02 -07:00
|
|
|
|
|
2015-01-26 09:47:15 -07:00
|
|
|
|
var items = _musicManager.GetInstantMixFromItem(item, user);
|
2014-08-18 07:20:02 -07:00
|
|
|
|
|
|
|
|
|
return GetResult(items, user, request);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
public Task<object> Get(GetInstantMixFromSong request)
|
2013-08-09 08:55:22 -07:00
|
|
|
|
{
|
2015-01-26 09:47:15 -07:00
|
|
|
|
var item = _libraryManager.GetItemById(request.Id);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2015-05-29 16:51:33 -07:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2015-01-26 09:47:15 -07:00
|
|
|
|
var items = _musicManager.GetInstantMixFromItem(item, user);
|
2014-03-30 09:49:40 -07:00
|
|
|
|
|
|
|
|
|
return GetResult(items, user, request);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
public Task<object> Get(GetInstantMixFromAlbum request)
|
2013-08-09 08:55:22 -07:00
|
|
|
|
{
|
2015-01-26 09:47:15 -07:00
|
|
|
|
var album = _libraryManager.GetItemById(request.Id);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2015-05-29 16:51:33 -07:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2013-08-09 09:33:12 -07:00
|
|
|
|
|
2015-01-26 09:47:15 -07:00
|
|
|
|
var items = _musicManager.GetInstantMixFromItem(album, user);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2014-03-30 09:49:40 -07:00
|
|
|
|
return GetResult(items, user, request);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
public Task<object> Get(GetInstantMixFromPlaylist request)
|
2014-12-12 20:56:30 -07:00
|
|
|
|
{
|
|
|
|
|
var playlist = (Playlist)_libraryManager.GetItemById(request.Id);
|
|
|
|
|
|
2015-05-29 16:51:33 -07:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2014-12-12 20:56:30 -07:00
|
|
|
|
|
2015-01-26 09:47:15 -07:00
|
|
|
|
var items = _musicManager.GetInstantMixFromItem(playlist, user);
|
2014-12-12 20:56:30 -07:00
|
|
|
|
|
|
|
|
|
return GetResult(items, user, request);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
public Task<object> Get(GetInstantMixFromMusicGenre request)
|
2013-08-09 08:55:22 -07:00
|
|
|
|
{
|
2015-05-29 16:51:33 -07:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2014-03-30 09:49:40 -07:00
|
|
|
|
var items = _musicManager.GetInstantMixFromGenres(new[] { request.Name }, user);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2014-03-30 09:49:40 -07:00
|
|
|
|
return GetResult(items, user, request);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
public Task<object> Get(GetInstantMixFromArtist request)
|
2013-08-09 08:55:22 -07:00
|
|
|
|
{
|
2015-05-29 16:51:33 -07:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2015-08-19 16:57:27 -07:00
|
|
|
|
var artist = _libraryManager.GetArtist(request.Name);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2015-08-19 16:57:27 -07:00
|
|
|
|
var items = _musicManager.GetInstantMixFromArtist(artist, user);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2014-03-30 09:49:40 -07:00
|
|
|
|
return GetResult(items, user, request);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
private async Task<object> GetResult(IEnumerable<Audio> items, User user, BaseGetSimilarItems request)
|
2013-08-09 08:55:22 -07:00
|
|
|
|
{
|
2014-03-30 09:49:40 -07:00
|
|
|
|
var list = items.ToList();
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
|
|
|
|
var result = new ItemsResult
|
|
|
|
|
{
|
2014-03-30 09:49:40 -07:00
|
|
|
|
TotalRecordCount = list.Count
|
2013-08-09 08:55:22 -07:00
|
|
|
|
};
|
|
|
|
|
|
2015-01-24 12:03:55 -07:00
|
|
|
|
var dtoOptions = GetDtoOptions(request);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
result.Items = (await _dtoService.GetBaseItemDtos(list.Take(request.Limit ?? list.Count), dtoOptions, user).ConfigureAwait(false)).ToArray();
|
2013-08-09 08:55:22 -07:00
|
|
|
|
|
2014-03-30 09:49:40 -07:00
|
|
|
|
return ToOptimizedResult(result);
|
2013-08-09 08:55:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|