Exemple #1
0
        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);
        }
Exemple #2
0
        private async Task <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 query = new InternalItemsQuery(user)
            {
                IncludeItemTypes = new[] { typeof(Movie).Name }
            };

            if (user == null || user.Configuration.IncludeTrailersInSuggestions)
            {
                var includeList = query.IncludeItemTypes.ToList();
                includeList.Add(typeof(Trailer).Name);
                query.IncludeItemTypes = includeList.ToArray();
            }

            var parentIds = new string[] { };
            var list      = _libraryManager.GetItemList(query, parentIds)
                            .DistinctBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString("N"))
                            .ToList();

            if (item is Video)
            {
                var imdbId = item.GetProviderId(MetadataProviders.Imdb);

                // Use imdb id to try to filter duplicates of the same item
                if (!string.IsNullOrWhiteSpace(imdbId))
                {
                    list = list
                           .Where(i => !string.Equals(imdbId, i.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase))
                           .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);
        }
        private async Task <ItemsResult> GetSimilarItemsResult(BaseGetSimilarItemsFromItem request, Func <BaseItem, bool> includeInSearch, Func <BaseItem, BaseItem, int> getSimilarityScore)
        {
            var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null;

            var item = string.IsNullOrEmpty(request.Id) ?
                       (request.UserId.HasValue ? user.RootFolder :
                        _libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);

            var fields = request.GetItemFields().ToList();

            var inputItems = user == null
                                 ? _libraryManager.RootFolder.GetRecursiveChildren().Where(i => i.Id != item.Id)
                                 : user.RootFolder.GetRecursiveChildren(user).Where(i => i.Id != item.Id);

            inputItems = inputItems.Where(includeInSearch);

            var list = inputItems.ToList();

            if (item is Movie && user != null && user.Configuration.IncludeTrailersInSuggestions)
            {
                var trailerResult = await _channelManager.GetAllMediaInternal(new AllChannelMediaQuery
                {
                    ContentTypes = new[] { ChannelMediaContentType.MovieExtra },
                    ExtraTypes   = new[] { ExtraType.Trailer },
                    UserId       = user.Id.ToString("N")
                }, CancellationToken.None).ConfigureAwait(false);

                var newTrailers = trailerResult.Items;

                list.AddRange(newTrailers);

                list = list
                       .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
                       .DistinctBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString(), StringComparer.OrdinalIgnoreCase)
                       .ToList();
            }

            var items = SimilarItemsHelper.GetSimilaritems(item, list, getSimilarityScore).ToList();

            IEnumerable <BaseItem> returnItems = items;

            if (request.Limit.HasValue)
            {
                returnItems = returnItems.Take(request.Limit.Value);
            }

            var result = new ItemsResult
            {
                Items = returnItems.Select(i => _dtoService.GetBaseItemDto(i, fields, user)).ToArray(),

                TotalRecordCount = items.Count
            };

            return(result);
        }
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>System.Object.</returns>
        public object Get(GetSimilarAlbums request)
        {
            var result = SimilarItemsHelper.GetSimilarItemsResult(_userManager,
                                                                  _itemRepo,
                                                                  _libraryManager,
                                                                  _userDataRepository,
                                                                  _dtoService,
                                                                  Logger,
                                                                  request, item => item is MusicAlbum,
                                                                  GetAlbumSimilarityScore);

            return(ToOptimizedSerializedResultUsingCache(result));
        }
Exemple #5
0
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>System.Object.</returns>
        public object Get(GetSimilarAlbums request)
        {
            var dtoOptions = GetDtoOptions(request);

            var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
                                                                  _itemRepo,
                                                                  _libraryManager,
                                                                  _userDataRepository,
                                                                  _dtoService,
                                                                  Logger,
                                                                  request, new[] { typeof(MusicAlbum) },
                                                                  GetAlbumSimilarityScore);

            return(ToOptimizedSerializedResultUsingCache(result));
        }
Exemple #6
0
        public object Get(GetSimilarArtists request)
        {
            var dtoOptions = GetDtoOptions(_authContext, request);

            var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
                                                                  _itemRepo,
                                                                  _libraryManager,
                                                                  _userDataRepository,
                                                                  _dtoService,
                                                                  Logger,
                                                                  request, new[] { typeof(MusicArtist) },
                                                                  SimilarItemsHelper.GetSimiliarityScore);

            return(ToOptimizedResult(result));
        }
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>System.Object.</returns>
        public async Task <object> Get(GetSimilarAlbums request)
        {
            var dtoOptions = GetDtoOptions(_authContext, request);

            var result = await SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
                                                                        _itemRepo,
                                                                        _libraryManager,
                                                                        _userDataRepository,
                                                                        _dtoService,
                                                                        Logger,
                                                                        request, new[] { typeof(MusicAlbum) },
                                                                        GetAlbumSimilarityScore).ConfigureAwait(false);

            return(ToOptimizedSerializedResultUsingCache(result));
        }
Exemple #8
0
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>System.Object.</returns>
        public object Get(GetSimilarTrailers request)
        {
            var result = SimilarItemsHelper.GetSimilarItemsResult(_userManager,
                                                                  _itemRepo,
                                                                  _libraryManager,
                                                                  _userDataRepository,
                                                                  _dtoService,
                                                                  Logger,

                                                                  // Strip out secondary versions
                                                                  request, item => (item is Movie || item is Trailer) && !((Video)item).PrimaryVersionId.HasValue,

                                                                  SimilarItemsHelper.GetSimiliarityScore);

            return(ToOptimizedSerializedResultUsingCache(result));
        }
Exemple #9
0
        /// <summary>
        /// Gets the album similarity score.
        /// </summary>
        /// <param name="item1">The item1.</param>
        /// <param name="item1People">The item1 people.</param>
        /// <param name="allPeople">All people.</param>
        /// <param name="item2">The item2.</param>
        /// <returns>System.Int32.</returns>
        private int GetAlbumSimilarityScore(BaseItem item1, List <PersonInfo> item1People, List <PersonInfo> allPeople, BaseItem item2)
        {
            var points = SimilarItemsHelper.GetSimiliarityScore(item1, item1People, allPeople, item2);

            var album1 = (MusicAlbum)item1;
            var album2 = (MusicAlbum)item2;

            var artists1 = album1
                           .GetAllArtists()
                           .DistinctNames()
                           .ToList();

            var artists2 = new HashSet <string>(
                album2.GetAllArtists().DistinctNames(),
                StringComparer.OrdinalIgnoreCase);

            return(points + artists1.Where(artists2.Contains).Sum(i => 5));
        }
Exemple #10
0
        /// <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.GetRecursiveChildren()
                           .OfType <Audio>()
                           .SelectMany(i =>
            {
                var list = new List <string>();

                if (!string.IsNullOrEmpty(i.AlbumArtist))
                {
                    list.Add(i.AlbumArtist);
                }
                list.AddRange(i.Artists);

                return(list);
            })
                           .Distinct(StringComparer.OrdinalIgnoreCase)
                           .ToList();

            var artists2 = album2.GetRecursiveChildren()
                           .OfType <Audio>()
                           .SelectMany(i =>
            {
                var list = new List <string>();

                if (!string.IsNullOrEmpty(i.AlbumArtist))
                {
                    list.Add(i.AlbumArtist);
                }
                list.AddRange(i.Artists);

                return(list);
            })
                           .Distinct(StringComparer.OrdinalIgnoreCase)
                           .ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);

            return(points + artists1.Where(artists2.ContainsKey).Sum(i => 5));
        }
Exemple #11
0
        /// <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
                           .AllArtists
                           .DistinctNames()
                           .ToList();

            var artists2 = album2
                           .AllArtists
                           .DistinctNames()
                           .ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);

            return(points + artists1.Where(artists2.ContainsKey).Sum(i => 5));
        }
Exemple #12
0
        public ActionResult <QueryResult <BaseItemDto> > GetSimilarArtists(
            [FromRoute] string artistId,
            [FromQuery] Guid?userId,
            [FromQuery] string?excludeArtistIds,
            [FromQuery] int?limit)
        {
            var dtoOptions = new DtoOptions().AddClientFields(Request);

            return(SimilarItemsHelper.GetSimilarItemsResult(
                       dtoOptions,
                       _userManager,
                       _libraryManager,
                       _dtoService,
                       userId,
                       artistId,
                       excludeArtistIds,
                       limit,
                       new[] { typeof(MusicArtist) },
                       SimilarItemsHelper.GetSimiliarityScore));
        }
Exemple #13
0
        private IEnumerable <RecommendationDto> GetSimilarTo(User user, List <BaseItem> allMovies, IEnumerable <BaseItem> baselineItems, int itemLimit, DtoOptions dtoOptions, RecommendationType type)
        {
            foreach (var item in baselineItems)
            {
                var similar = SimilarItemsHelper
                              .GetSimilaritems(item, _libraryManager, allMovies, SimilarItemsHelper.GetSimiliarityScore)
                              .Take(itemLimit)
                              .ToList();

                if (similar.Count > 0)
                {
                    yield return(new RecommendationDto
                    {
                        BaselineItemName = item.Name,
                        CategoryId = item.Id.ToString("N"),
                        RecommendationType = type,
                        Items = _dtoService.GetBaseItemDtos(similar, dtoOptions, user).ToArray()
                    });
                }
            }
        }
        /// <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.GetRecursiveChildren(i => i is IHasArtist)
                           .Cast <IHasArtist>()
                           .SelectMany(i => i.AllArtists)
                           .Distinct(StringComparer.OrdinalIgnoreCase)
                           .ToList();

            var artists2 = album2.GetRecursiveChildren(i => i is IHasArtist)
                           .Cast <IHasArtist>()
                           .SelectMany(i => i.AllArtists)
                           .Distinct(StringComparer.OrdinalIgnoreCase)
                           .ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);

            return(points + artists1.Where(artists2.ContainsKey).Sum(i => 5));
        }
        private IEnumerable<RecommendationDto> GetSimilarTo(User user, List<Movie> allMovies, IEnumerable<Movie> baselineItems, int itemLimit, List<ItemFields> fields, RecommendationType type)
        {
            var userId = user.Id;

            foreach (var item in baselineItems)
            {
                var similar = SimilarItemsHelper
                    .GetSimilaritems(item, allMovies, SimilarItemsHelper.GetSimiliarityScore)
                    .Take(itemLimit)
                    .ToList();

                if (similar.Count > 0)
                {
                    yield return new RecommendationDto
                    {
                        BaselineItemName = item.Name,
                        CategoryId = item.Id.ToString("N"),
                        RecommendationType = type,
                        Items = similar.Select(i => _dtoService.GetBaseItemDto(i, fields, user)).ToArray()
                    };
                }
            }
        }
Exemple #16
0
        private async Task <ItemsResult> GetSimilarItemsResult(BaseGetSimilarItemsFromItem request, Func <BaseItem, bool> includeInSearch, Func <BaseItem, 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);

            Func <BaseItem, bool> filter = i => i.Id != item.Id && includeInSearch(i);

            var inputItems = user == null
                                 ? _libraryManager.RootFolder.GetRecursiveChildren(filter)
                                 : user.RootFolder.GetRecursiveChildren(user, filter);

            var list = inputItems.ToList();

            if (item is Movie && user != null && user.Configuration.IncludeTrailersInSuggestions)
            {
                var trailerResult = await _channelManager.GetAllMediaInternal(new AllChannelMediaQuery
                {
                    ContentTypes = new[] { ChannelMediaContentType.MovieExtra },
                    ExtraTypes   = new[] { ExtraType.Trailer },
                    UserId       = user.Id.ToString("N")
                }, CancellationToken.None).ConfigureAwait(false);

                var newTrailers = trailerResult.Items;

                list.AddRange(newTrailers);

                list = list
                       .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
                       .DistinctBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString(), StringComparer.OrdinalIgnoreCase)
                       .ToList();
            }

            if (item is Video)
            {
                var imdbId = item.GetProviderId(MetadataProviders.Imdb);

                // Use imdb id to try to filter duplicates of the same item
                if (!string.IsNullOrWhiteSpace(imdbId))
                {
                    list = list
                           .Where(i => !string.Equals(imdbId, i.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase))
                           .ToList();
                }
            }

            var items = SimilarItemsHelper.GetSimilaritems(item, 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);
        }