2013-09-04 10:02:19 -07:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-05-25 16:52:41 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
|
|
|
|
using ServiceStack.ServiceHost;
|
|
|
|
|
using System;
|
2013-09-05 12:00:50 -07:00
|
|
|
|
using System.Collections.Generic;
|
2013-05-25 16:52:41 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
|
{
|
|
|
|
|
[Route("/Albums/{Id}/Similar", "GET")]
|
|
|
|
|
[Api(Description = "Finds albums similar to a given album.")]
|
2013-08-09 08:55:22 -07:00
|
|
|
|
public class GetSimilarAlbums : BaseGetSimilarItemsFromItem
|
2013-05-25 16:52:41 -07:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class AlbumsService : BaseApiService
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _user manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _user data repository
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IUserDataRepository _userDataRepository;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2013-06-18 12:16:27 -07:00
|
|
|
|
private readonly IItemRepository _itemRepo;
|
2013-09-04 10:02:19 -07:00
|
|
|
|
private readonly IDtoService _dtoService;
|
2013-05-25 16:52:41 -07:00
|
|
|
|
|
2013-09-04 10:02:19 -07:00
|
|
|
|
public AlbumsService(IUserManager userManager, IUserDataRepository userDataRepository, ILibraryManager libraryManager, IItemRepository itemRepo, IDtoService dtoService)
|
2013-05-25 16:52:41 -07:00
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_userDataRepository = userDataRepository;
|
|
|
|
|
_libraryManager = libraryManager;
|
2013-06-18 12:16:27 -07:00
|
|
|
|
_itemRepo = itemRepo;
|
2013-09-04 10:02:19 -07:00
|
|
|
|
_dtoService = dtoService;
|
2013-05-25 16:52:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object Get(GetSimilarAlbums request)
|
|
|
|
|
{
|
2013-08-08 13:18:05 -07:00
|
|
|
|
var result = SimilarItemsHelper.GetSimilarItemsResult(_userManager,
|
2013-06-18 12:16:27 -07:00
|
|
|
|
_itemRepo,
|
2013-05-25 16:52:41 -07:00
|
|
|
|
_libraryManager,
|
|
|
|
|
_userDataRepository,
|
2013-09-04 10:02:19 -07:00
|
|
|
|
_dtoService,
|
2013-05-25 16:52:41 -07:00
|
|
|
|
Logger,
|
|
|
|
|
request, item => item is MusicAlbum,
|
|
|
|
|
GetAlbumSimilarityScore);
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the album similarity score.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item1">The item1.</param>
|
|
|
|
|
/// <param name="item2">The item2.</param>
|
|
|
|
|
/// <returns>System.Int32.</returns>
|
|
|
|
|
private int GetAlbumSimilarityScore(BaseItem item1, BaseItem item2)
|
|
|
|
|
{
|
|
|
|
|
var points = SimilarItemsHelper.GetSimiliarityScore(item1, item2);
|
|
|
|
|
|
|
|
|
|
var album1 = (MusicAlbum)item1;
|
|
|
|
|
var album2 = (MusicAlbum)item2;
|
|
|
|
|
|
|
|
|
|
var artists1 = album1.RecursiveChildren
|
|
|
|
|
.OfType<Audio>()
|
2013-09-05 12:00:50 -07:00
|
|
|
|
.SelectMany(i =>
|
|
|
|
|
{
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(i.AlbumArtist))
|
|
|
|
|
{
|
|
|
|
|
list.Add(i.AlbumArtist);
|
|
|
|
|
}
|
|
|
|
|
list.AddRange(i.Artists);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
})
|
2013-05-25 16:52:41 -07:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var artists2 = album2.RecursiveChildren
|
|
|
|
|
.OfType<Audio>()
|
2013-09-05 12:00:50 -07:00
|
|
|
|
.SelectMany(i =>
|
|
|
|
|
{
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(i.AlbumArtist))
|
|
|
|
|
{
|
|
|
|
|
list.Add(i.AlbumArtist);
|
|
|
|
|
}
|
|
|
|
|
list.AddRange(i.Artists);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
})
|
2013-05-25 16:52:41 -07:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
2013-08-07 09:00:20 -07:00
|
|
|
|
.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
2013-05-25 16:52:41 -07:00
|
|
|
|
|
2013-08-07 09:00:20 -07:00
|
|
|
|
return points + artists1.Where(artists2.ContainsKey).Sum(i => 5);
|
2013-05-25 16:52:41 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|