jellyfin-server/MediaBrowser.Api/Music/InstantMixService.cs

113 lines
3.8 KiB
C#
Raw Normal View History

2013-08-09 15:55:22 +00:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
2013-08-09 15:55:22 +00:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Querying;
2013-12-07 15:52:38 +00:00
using ServiceStack;
2013-08-09 15:55:22 +00:00
using System.Collections.Generic;
using System.Linq;
2014-03-06 05:17:13 +00:00
namespace MediaBrowser.Api.Music
2013-08-09 15:55:22 +00:00
{
2014-03-25 21:13:55 +00:00
[Route("/Songs/{Id}/InstantMix", "GET", Summary = "Creates an instant playlist based on a given song")]
2013-08-09 15:55:22 +00:00
public class GetInstantMixFromSong : BaseGetSimilarItemsFromItem
{
}
2014-03-25 21:13:55 +00:00
[Route("/Albums/{Id}/InstantMix", "GET", Summary = "Creates an instant playlist based on a given album")]
2013-08-09 15:55:22 +00:00
public class GetInstantMixFromAlbum : BaseGetSimilarItemsFromItem
{
}
2014-03-25 21:13:55 +00:00
[Route("/Artists/{Name}/InstantMix", "GET", Summary = "Creates an instant playlist based on a given artist")]
2013-08-09 15:55:22 +00: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 21:13:55 +00:00
[Route("/MusicGenres/{Name}/InstantMix", "GET", Summary = "Creates an instant playlist based on a music genre")]
2013-08-09 15:55:22 +00: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; }
}
public class InstantMixService : BaseApiService
{
private readonly IUserManager _userManager;
2013-09-04 17:02:19 +00:00
private readonly IDtoService _dtoService;
2014-04-25 20:15:50 +00:00
private readonly ILibraryManager _libraryManager;
private readonly IMusicManager _musicManager;
2013-08-09 15:55:22 +00:00
2014-04-25 20:15:50 +00:00
public InstantMixService(IUserManager userManager, IDtoService dtoService, IMusicManager musicManager, ILibraryManager libraryManager)
2013-08-09 15:55:22 +00:00
{
_userManager = userManager;
2013-09-04 17:02:19 +00:00
_dtoService = dtoService;
_musicManager = musicManager;
2014-04-25 20:15:50 +00:00
_libraryManager = libraryManager;
2013-08-09 15:55:22 +00:00
}
public object Get(GetInstantMixFromSong request)
{
2014-04-25 20:15:50 +00:00
var item = (Audio)_libraryManager.GetItemById(request.Id);
2013-08-09 15:55:22 +00:00
var user = _userManager.GetUserById(request.UserId.Value);
2013-08-09 15:55:22 +00:00
var items = _musicManager.GetInstantMixFromSong(item, user);
return GetResult(items, user, request);
2013-08-09 15:55:22 +00:00
}
public object Get(GetInstantMixFromAlbum request)
{
2014-04-25 20:15:50 +00:00
var album = (MusicAlbum)_libraryManager.GetItemById(request.Id);
2013-08-09 15:55:22 +00:00
var user = _userManager.GetUserById(request.UserId.Value);
2013-08-09 16:33:12 +00:00
var items = _musicManager.GetInstantMixFromAlbum(album, user);
2013-08-09 15:55:22 +00:00
return GetResult(items, user, request);
2013-08-09 15:55:22 +00:00
}
public object Get(GetInstantMixFromMusicGenre request)
{
var user = _userManager.GetUserById(request.UserId.Value);
2013-08-09 15:55:22 +00:00
var items = _musicManager.GetInstantMixFromGenres(new[] { request.Name }, user);
2013-08-09 15:55:22 +00:00
return GetResult(items, user, request);
2013-08-09 15:55:22 +00:00
}
public object Get(GetInstantMixFromArtist request)
{
var user = _userManager.GetUserById(request.UserId.Value);
2013-08-09 15:55:22 +00:00
var items = _musicManager.GetInstantMixFromArtist(request.Name, user);
2013-08-09 15:55:22 +00:00
return GetResult(items, user, request);
2013-08-09 15:55:22 +00:00
}
private object GetResult(IEnumerable<Audio> items, User user, BaseGetSimilarItems request)
2013-08-09 15:55:22 +00:00
{
var fields = request.GetItemFields().ToList();
var list = items.ToList();
2013-08-09 15:55:22 +00:00
var result = new ItemsResult
{
TotalRecordCount = list.Count
2013-08-09 15:55:22 +00:00
};
var dtos = list.Take(request.Limit ?? list.Count)
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 ToOptimizedResult(result);
2013-08-09 15:55:22 +00:00
}
}
}