AlbumsService.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using MediaBrowser.Controller.Dto;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Persistence;
  6. using ServiceStack.ServiceHost;
  7. using System;
  8. using System.Linq;
  9. namespace MediaBrowser.Api
  10. {
  11. [Route("/Albums/{Id}/Similar", "GET")]
  12. [Api(Description = "Finds albums similar to a given album.")]
  13. public class GetSimilarAlbums : BaseGetSimilarItemsFromItem
  14. {
  15. }
  16. public class AlbumsService : BaseApiService
  17. {
  18. /// <summary>
  19. /// The _user manager
  20. /// </summary>
  21. private readonly IUserManager _userManager;
  22. /// <summary>
  23. /// The _user data repository
  24. /// </summary>
  25. private readonly IUserDataRepository _userDataRepository;
  26. /// <summary>
  27. /// The _library manager
  28. /// </summary>
  29. private readonly ILibraryManager _libraryManager;
  30. private readonly IItemRepository _itemRepo;
  31. private readonly IDtoService _dtoService;
  32. public AlbumsService(IUserManager userManager, IUserDataRepository userDataRepository, ILibraryManager libraryManager, IItemRepository itemRepo, IDtoService dtoService)
  33. {
  34. _userManager = userManager;
  35. _userDataRepository = userDataRepository;
  36. _libraryManager = libraryManager;
  37. _itemRepo = itemRepo;
  38. _dtoService = dtoService;
  39. }
  40. /// <summary>
  41. /// Gets the specified request.
  42. /// </summary>
  43. /// <param name="request">The request.</param>
  44. /// <returns>System.Object.</returns>
  45. public object Get(GetSimilarAlbums request)
  46. {
  47. var result = SimilarItemsHelper.GetSimilarItemsResult(_userManager,
  48. _itemRepo,
  49. _libraryManager,
  50. _userDataRepository,
  51. _dtoService,
  52. Logger,
  53. request, item => item is MusicAlbum,
  54. GetAlbumSimilarityScore);
  55. return ToOptimizedResult(result);
  56. }
  57. /// <summary>
  58. /// Gets the album similarity score.
  59. /// </summary>
  60. /// <param name="item1">The item1.</param>
  61. /// <param name="item2">The item2.</param>
  62. /// <returns>System.Int32.</returns>
  63. private int GetAlbumSimilarityScore(BaseItem item1, BaseItem item2)
  64. {
  65. var points = SimilarItemsHelper.GetSimiliarityScore(item1, item2);
  66. var album1 = (MusicAlbum)item1;
  67. var album2 = (MusicAlbum)item2;
  68. var artists1 = album1.RecursiveChildren
  69. .OfType<Audio>()
  70. .SelectMany(i => new[] { i.AlbumArtist, i.Artist })
  71. .Where(i => !string.IsNullOrEmpty(i))
  72. .Distinct(StringComparer.OrdinalIgnoreCase)
  73. .ToList();
  74. var artists2 = album2.RecursiveChildren
  75. .OfType<Audio>()
  76. .SelectMany(i => new[] { i.AlbumArtist, i.Artist })
  77. .Where(i => !string.IsNullOrEmpty(i))
  78. .Distinct(StringComparer.OrdinalIgnoreCase)
  79. .ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  80. return points + artists1.Where(artists2.ContainsKey).Sum(i => 5);
  81. }
  82. }
  83. }