jellyfin/MediaBrowser.Api/InstantMixService.cs

150 lines
5.4 KiB
C#
Raw Normal View History

2013-08-09 08:55:22 -07:00
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)
{
2013-08-09 09:33:12 -07:00
var album = (MusicAlbum)DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager);
2013-08-09 08:55:22 -07:00
2013-08-09 09:33:12 -07:00
var genres = album
.RecursiveChildren
.OfType<Audio>()
.SelectMany(i => i.Genres)
.Concat(album.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase);
var result = GetInstantMixResult(request, genres).Result;
2013-08-09 08:55:22 -07:00
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)
2013-08-09 09:33:12 -07:00
.Concat(artist.Genres)
2013-08-09 08:55:22 -07:00
.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;
}
}
}