jellyfin/MediaBrowser.Api/Music/AlbumsService.cs

120 lines
4.1 KiB
C#
Raw Normal View History

2013-09-04 10:02:19 -07:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
2014-07-02 11:34:08 -07:00
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Persistence;
using System;
2015-07-08 09:10:34 -07:00
using System.Collections.Generic;
using System.Linq;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.Services;
2014-03-05 22:17:13 -07:00
namespace MediaBrowser.Api.Music
{
2014-04-26 20:42:05 -07:00
[Route("/Albums/{Id}/Similar", "GET", Summary = "Finds albums similar to a given album.")]
2013-08-09 08:55:22 -07:00
public class GetSimilarAlbums : BaseGetSimilarItemsFromItem
{
}
2015-08-20 19:36:30 -07:00
[Route("/Artists/{Id}/Similar", "GET", Summary = "Finds albums similar to a given album.")]
public class GetSimilarArtists : BaseGetSimilarItemsFromItem
{
}
2014-07-02 11:34:08 -07:00
[Authenticated]
public class AlbumsService : BaseApiService
{
/// <summary>
/// The _user manager
/// </summary>
private readonly IUserManager _userManager;
/// <summary>
/// The _user data repository
/// </summary>
2013-10-02 09:08:58 -07:00
private readonly IUserDataManager _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;
2016-11-10 07:41:24 -07:00
private readonly IAuthorizationContext _authContext;
2016-11-10 07:41:24 -07:00
public AlbumsService(IUserManager userManager, IUserDataManager userDataRepository, ILibraryManager libraryManager, IItemRepository itemRepo, IDtoService dtoService, IAuthorizationContext authContext)
{
_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;
2016-11-10 07:41:24 -07:00
_authContext = authContext;
}
2017-08-27 17:33:05 -07:00
public object Get(GetSimilarArtists request)
2015-08-20 19:36:30 -07:00
{
2016-11-10 07:41:24 -07:00
var dtoOptions = GetDtoOptions(_authContext, request);
2015-08-20 19:36:30 -07:00
2017-08-27 17:33:05 -07:00
var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
2016-03-24 20:53:42 -07:00
_itemRepo,
_libraryManager,
_userDataRepository,
_dtoService,
Logger,
request, new[] { typeof(MusicArtist) },
2017-08-27 17:33:05 -07:00
SimilarItemsHelper.GetSimiliarityScore);
2015-08-20 19:36:30 -07:00
2018-09-12 10:26:21 -07:00
return ToOptimizedResult(result);
2015-08-20 19:36:30 -07:00
}
2017-08-27 17:33:05 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2017-08-27 17:33:05 -07:00
public object Get(GetSimilarAlbums request)
{
2016-11-10 07:41:24 -07:00
var dtoOptions = GetDtoOptions(_authContext, request);
2015-01-24 12:03:55 -07:00
2017-08-27 17:33:05 -07:00
var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
2013-06-18 12:16:27 -07:00
_itemRepo,
_libraryManager,
_userDataRepository,
2013-09-04 10:02:19 -07:00
_dtoService,
Logger,
2016-03-24 20:53:42 -07:00
request, new[] { typeof(MusicAlbum) },
2017-08-27 17:33:05 -07:00
GetAlbumSimilarityScore);
2018-09-12 10:26:21 -07:00
return ToOptimizedResult(result);
}
2017-08-27 17:33:05 -07:00
/// <summary>
/// Gets the album similarity score.
/// </summary>
/// <param name="item1">The item1.</param>
2015-07-08 09:10:34 -07:00
/// <param name="item1People">The item1 people.</param>
/// <param name="allPeople">All people.</param>
/// <param name="item2">The item2.</param>
/// <returns>System.Int32.</returns>
2015-07-08 09:10:34 -07:00
private int GetAlbumSimilarityScore(BaseItem item1, List<PersonInfo> item1People, List<PersonInfo> allPeople, BaseItem item2)
{
2015-07-08 09:10:34 -07:00
var points = SimilarItemsHelper.GetSimiliarityScore(item1, item1People, allPeople, item2);
var album1 = (MusicAlbum)item1;
var album2 = (MusicAlbum)item2;
2015-03-13 10:25:28 -07:00
var artists1 = album1
.AllArtists
2015-04-09 14:11:57 -07:00
.DistinctNames()
.ToList();
2015-03-13 10:25:28 -07:00
var artists2 = album2
.AllArtists
2015-04-09 14:11:57 -07:00
.DistinctNames()
2013-08-07 09:00:20 -07:00
.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
2013-08-07 09:00:20 -07:00
return points + artists1.Where(artists2.ContainsKey).Sum(i => 5);
}
}
}