2013-09-04 17:02:19 +00:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-07-02 18:34:08 +00:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
2015-08-21 02:36:30 +00:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
2013-12-07 15:52:38 +00:00
|
|
|
|
using ServiceStack;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
using System;
|
2015-07-08 16:10:34 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2014-03-06 05:17:13 +00:00
|
|
|
|
namespace MediaBrowser.Api.Music
|
2013-05-25 23:52:41 +00:00
|
|
|
|
{
|
2014-04-27 03:42:05 +00:00
|
|
|
|
[Route("/Albums/{Id}/Similar", "GET", Summary = "Finds albums similar to a given album.")]
|
2013-08-09 15:55:22 +00:00
|
|
|
|
public class GetSimilarAlbums : BaseGetSimilarItemsFromItem
|
2013-05-25 23:52:41 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 02:36:30 +00:00
|
|
|
|
[Route("/Artists/{Id}/Similar", "GET", Summary = "Finds albums similar to a given album.")]
|
|
|
|
|
public class GetSimilarArtists : BaseGetSimilarItemsFromItem
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-02 18:34:08 +00:00
|
|
|
|
[Authenticated]
|
2013-05-25 23:52:41 +00:00
|
|
|
|
public class AlbumsService : BaseApiService
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _user manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _user data repository
|
|
|
|
|
/// </summary>
|
2013-10-02 16:08:58 +00:00
|
|
|
|
private readonly IUserDataManager _userDataRepository;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2013-06-18 19:16:27 +00:00
|
|
|
|
private readonly IItemRepository _itemRepo;
|
2013-09-04 17:02:19 +00:00
|
|
|
|
private readonly IDtoService _dtoService;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
|
2013-10-02 16:08:58 +00:00
|
|
|
|
public AlbumsService(IUserManager userManager, IUserDataManager userDataRepository, ILibraryManager libraryManager, IItemRepository itemRepo, IDtoService dtoService)
|
2013-05-25 23:52:41 +00:00
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_userDataRepository = userDataRepository;
|
|
|
|
|
_libraryManager = libraryManager;
|
2013-06-18 19:16:27 +00:00
|
|
|
|
_itemRepo = itemRepo;
|
2013-09-04 17:02:19 +00:00
|
|
|
|
_dtoService = dtoService;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 02:36:30 +00:00
|
|
|
|
public object Get(GetSimilarArtists request)
|
|
|
|
|
{
|
|
|
|
|
var result = GetSimilarItemsResult(
|
|
|
|
|
|
|
|
|
|
request,
|
|
|
|
|
|
|
|
|
|
SimilarItemsHelper.GetSimiliarityScore);
|
|
|
|
|
|
|
|
|
|
return ToOptimizedSerializedResultUsingCache(result);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 23:52:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object Get(GetSimilarAlbums request)
|
|
|
|
|
{
|
2015-01-24 19:03:55 +00:00
|
|
|
|
var dtoOptions = GetDtoOptions(request);
|
|
|
|
|
|
|
|
|
|
var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
|
2013-06-18 19:16:27 +00:00
|
|
|
|
_itemRepo,
|
2013-05-25 23:52:41 +00:00
|
|
|
|
_libraryManager,
|
|
|
|
|
_userDataRepository,
|
2013-09-04 17:02:19 +00:00
|
|
|
|
_dtoService,
|
2013-05-25 23:52:41 +00:00
|
|
|
|
Logger,
|
|
|
|
|
request, item => item is MusicAlbum,
|
|
|
|
|
GetAlbumSimilarityScore);
|
|
|
|
|
|
2014-02-04 04:04:19 +00:00
|
|
|
|
return ToOptimizedSerializedResultUsingCache(result);
|
2013-05-25 23:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 02:36:30 +00:00
|
|
|
|
private ItemsResult GetSimilarItemsResult(BaseGetSimilarItemsFromItem request, Func<BaseItem, List<PersonInfo>, List<PersonInfo>, BaseItem, int> getSimilarityScore)
|
|
|
|
|
{
|
|
|
|
|
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
|
|
|
|
|
|
|
|
|
|
var item = string.IsNullOrEmpty(request.Id) ?
|
|
|
|
|
(!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder :
|
|
|
|
|
_libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);
|
|
|
|
|
|
|
|
|
|
var inputItems = _libraryManager.GetArtists(user.RootFolder.GetRecursiveChildren(user, i => i is IHasArtist).OfType<IHasArtist>());
|
|
|
|
|
|
|
|
|
|
var list = inputItems.ToList();
|
|
|
|
|
|
|
|
|
|
var items = SimilarItemsHelper.GetSimilaritems(item, _libraryManager, list, getSimilarityScore).ToList();
|
|
|
|
|
|
|
|
|
|
IEnumerable<BaseItem> returnItems = items;
|
|
|
|
|
|
|
|
|
|
if (request.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
returnItems = returnItems.Take(request.Limit.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dtoOptions = GetDtoOptions(request);
|
|
|
|
|
|
|
|
|
|
var result = new ItemsResult
|
|
|
|
|
{
|
|
|
|
|
Items = _dtoService.GetBaseItemDtos(returnItems, dtoOptions, user).ToArray(),
|
|
|
|
|
|
|
|
|
|
TotalRecordCount = items.Count
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 23:52:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the album similarity score.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item1">The item1.</param>
|
2015-07-08 16:10:34 +00:00
|
|
|
|
/// <param name="item1People">The item1 people.</param>
|
|
|
|
|
/// <param name="allPeople">All people.</param>
|
2013-05-25 23:52:41 +00:00
|
|
|
|
/// <param name="item2">The item2.</param>
|
|
|
|
|
/// <returns>System.Int32.</returns>
|
2015-07-08 16:10:34 +00:00
|
|
|
|
private int GetAlbumSimilarityScore(BaseItem item1, List<PersonInfo> item1People, List<PersonInfo> allPeople, BaseItem item2)
|
2013-05-25 23:52:41 +00:00
|
|
|
|
{
|
2015-07-08 16:10:34 +00:00
|
|
|
|
var points = SimilarItemsHelper.GetSimiliarityScore(item1, item1People, allPeople, item2);
|
2013-05-25 23:52:41 +00:00
|
|
|
|
|
|
|
|
|
var album1 = (MusicAlbum)item1;
|
|
|
|
|
var album2 = (MusicAlbum)item2;
|
|
|
|
|
|
2015-03-13 17:25:28 +00:00
|
|
|
|
var artists1 = album1
|
|
|
|
|
.AllArtists
|
2015-04-09 21:11:57 +00:00
|
|
|
|
.DistinctNames()
|
2013-05-25 23:52:41 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2015-03-13 17:25:28 +00:00
|
|
|
|
var artists2 = album2
|
|
|
|
|
.AllArtists
|
2015-04-09 21:11:57 +00:00
|
|
|
|
.DistinctNames()
|
2013-08-07 16:00:20 +00:00
|
|
|
|
.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
2013-05-25 23:52:41 +00:00
|
|
|
|
|
2013-08-07 16:00:20 +00:00
|
|
|
|
return points + artists1.Where(artists2.ContainsKey).Sum(i => 5);
|
2013-05-25 23:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|