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