mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-05-30 20:38:27 +01:00
Merge pull request #16856 from Shadowghost/movie-recommendations
Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
Format / format-check (push) Has been cancelled
Tests / run-tests (macos-latest) (push) Has been cancelled
Tests / run-tests (ubuntu-latest) (push) Has been cancelled
Tests / run-tests (windows-latest) (push) Has been cancelled
OpenAPI Publish / OpenAPI - Publish Artifact (push) Has been cancelled
OpenAPI Publish / OpenAPI - Publish Unstable Spec (push) Has been cancelled
OpenAPI Publish / OpenAPI - Publish Stable Spec (push) Has been cancelled
Project Automation / Project board (push) Has been cancelled
Merge Conflict Labeler / Labeling (push) Has been cancelled
Stale PR Check / Check PRs with merge conflicts (push) Has been cancelled
Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
Format / format-check (push) Has been cancelled
Tests / run-tests (macos-latest) (push) Has been cancelled
Tests / run-tests (ubuntu-latest) (push) Has been cancelled
Tests / run-tests (windows-latest) (push) Has been cancelled
OpenAPI Publish / OpenAPI - Publish Artifact (push) Has been cancelled
OpenAPI Publish / OpenAPI - Publish Unstable Spec (push) Has been cancelled
OpenAPI Publish / OpenAPI - Publish Stable Spec (push) Has been cancelled
Project Automation / Project board (push) Has been cancelled
Merge Conflict Labeler / Labeling (push) Has been cancelled
Stale PR Check / Check PRs with merge conflicts (push) Has been cancelled
Fix movie recommendations
This commit is contained in:
@@ -3394,6 +3394,12 @@ namespace Emby.Server.Implementations.Library
|
||||
return _peopleRepository.GetPeopleNames(query);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IReadOnlyList<string> GetPeopleNamesByItems(IReadOnlyList<Guid> itemIds, IReadOnlyList<string> personTypes, int limit)
|
||||
{
|
||||
return _peopleRepository.GetPeopleNamesByItems(itemIds, personTypes, limit);
|
||||
}
|
||||
|
||||
public void UpdatePeople(BaseItem item, List<PersonInfo> people)
|
||||
{
|
||||
UpdatePeopleAsync(item, people, CancellationToken.None).GetAwaiter().GetResult();
|
||||
|
||||
@@ -1,36 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Data.Enums;
|
||||
using Jellyfin.Database.Implementations.Enums;
|
||||
using Jellyfin.Database.Implementations;
|
||||
using Jellyfin.Database.Implementations.Entities;
|
||||
using Jellyfin.Extensions;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Dto;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Entities.Movies;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Persistence;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem;
|
||||
|
||||
namespace Emby.Server.Implementations.Library.SimilarItems;
|
||||
|
||||
/// <summary>
|
||||
/// Provides similar items for movies and trailers.
|
||||
/// Provides similar items for movies and trailers using weighted scoring.
|
||||
/// </summary>
|
||||
public sealed class MovieSimilarItemsProvider : ILocalSimilarItemsProvider<Movie>, ILocalSimilarItemsProvider<Trailer>
|
||||
public sealed class MovieSimilarItemsProvider : ILocalSimilarItemsProvider<Movie>, ILocalSimilarItemsProvider<Trailer>, IBatchLocalSimilarItemsProvider
|
||||
{
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private const int GenreWeight = 10;
|
||||
private const int TagWeight = 5;
|
||||
private const int StudioWeight = 5;
|
||||
private const int DirectorWeight = 50;
|
||||
private const int ActorWeight = 15;
|
||||
|
||||
// Caps the batch fan-out so downstream IN-list sizes (per-source scores, accessible-id
|
||||
// load, navigation includes) stay bounded regardless of caller input.
|
||||
private const int MaxBatchSourceItems = 64;
|
||||
|
||||
private static readonly (ItemValueType Type, int Weight)[] _itemValueDimensions =
|
||||
[
|
||||
(ItemValueType.Genre, GenreWeight),
|
||||
(ItemValueType.Tags, TagWeight),
|
||||
(ItemValueType.Studios, StudioWeight)
|
||||
];
|
||||
|
||||
private static readonly Dictionary<string, int> _personTypeWeights = new(StringComparer.Ordinal)
|
||||
{
|
||||
[nameof(PersonKind.Director)] = DirectorWeight,
|
||||
[nameof(PersonKind.Actor)] = ActorWeight,
|
||||
[nameof(PersonKind.GuestStar)] = ActorWeight,
|
||||
};
|
||||
|
||||
private static readonly string[] _scoredPersonTypes = [.. _personTypeWeights.Keys];
|
||||
|
||||
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
|
||||
private readonly IItemQueryHelpers _queryHelpers;
|
||||
private readonly IServerConfigurationManager _serverConfigurationManager;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MovieSimilarItemsProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="libraryManager">The library manager.</param>
|
||||
/// <param name="dbProvider">The database context factory.</param>
|
||||
/// <param name="queryHelpers">The shared query helpers.</param>
|
||||
/// <param name="serverConfigurationManager">The server configuration manager.</param>
|
||||
public MovieSimilarItemsProvider(
|
||||
ILibraryManager libraryManager,
|
||||
IDbContextFactory<JellyfinDbContext> dbProvider,
|
||||
IItemQueryHelpers queryHelpers,
|
||||
IServerConfigurationManager serverConfigurationManager)
|
||||
{
|
||||
_libraryManager = libraryManager;
|
||||
_dbProvider = dbProvider;
|
||||
_queryHelpers = queryHelpers;
|
||||
_serverConfigurationManager = serverConfigurationManager;
|
||||
}
|
||||
|
||||
@@ -41,15 +77,17 @@ public sealed class MovieSimilarItemsProvider : ILocalSimilarItemsProvider<Movie
|
||||
public MetadataPluginType Type => MetadataPluginType.LocalSimilarityProvider;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Task<IReadOnlyList<BaseItem>> GetSimilarItemsAsync(Movie item, SimilarItemsQuery query, CancellationToken cancellationToken)
|
||||
public async Task<IReadOnlyList<BaseItemDto>> GetSimilarItemsAsync(Movie item, SimilarItemsQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.FromResult(GetSimilarMovieItems(item, query));
|
||||
var results = await GetBatchSimilarItemsAsync([item], query, cancellationToken).ConfigureAwait(false);
|
||||
return results.TryGetValue(item.Id, out var items) ? items : [];
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Task<IReadOnlyList<BaseItem>> GetSimilarItemsAsync(Trailer item, SimilarItemsQuery query, CancellationToken cancellationToken)
|
||||
public async Task<IReadOnlyList<BaseItemDto>> GetSimilarItemsAsync(Trailer item, SimilarItemsQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.FromResult(GetSimilarMovieItems(item, query));
|
||||
var results = await GetBatchSimilarItemsAsync([item], query, cancellationToken).ConfigureAwait(false);
|
||||
return results.TryGetValue(item.Id, out var items) ? items : [];
|
||||
}
|
||||
|
||||
bool ILocalSimilarItemsProvider.Supports(Type itemType)
|
||||
@@ -63,29 +101,233 @@ public sealed class MovieSimilarItemsProvider : ILocalSimilarItemsProvider<Movie
|
||||
_ => throw new ArgumentException($"Unsupported item type {item.GetType()}", nameof(item))
|
||||
};
|
||||
|
||||
private IReadOnlyList<BaseItem> GetSimilarMovieItems(BaseItem item, SimilarItemsQuery query)
|
||||
/// <inheritdoc/>
|
||||
public async Task<Dictionary<Guid, IReadOnlyList<BaseItemDto>>> GetBatchSimilarItemsAsync(
|
||||
IReadOnlyList<BaseItemDto> sourceItems,
|
||||
SimilarItemsQuery query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var includeItemTypes = new List<BaseItemKind> { BaseItemKind.Movie };
|
||||
|
||||
if (_serverConfigurationManager.Configuration.EnableExternalContentInSuggestions)
|
||||
{
|
||||
includeItemTypes.Add(BaseItemKind.Trailer);
|
||||
includeItemTypes.Add(BaseItemKind.LiveTvProgram);
|
||||
}
|
||||
|
||||
var internalQuery = new InternalItemsQuery(query.User)
|
||||
{
|
||||
Genres = item.Genres,
|
||||
Tags = item.Tags,
|
||||
Limit = query.Limit,
|
||||
DtoOptions = query.DtoOptions ?? new DtoOptions(),
|
||||
ExcludeItemIds = [.. query.ExcludeItemIds],
|
||||
IncludeItemTypes = [.. includeItemTypes],
|
||||
EnableGroupByMetadataKey = true,
|
||||
EnableTotalRecordCount = false,
|
||||
OrderBy = [(ItemSortBy.Random, SortOrder.Ascending)]
|
||||
};
|
||||
var limit = query.Limit ?? 50;
|
||||
var dtoOptions = query.DtoOptions ?? new DtoOptions();
|
||||
|
||||
return _libraryManager.GetItemList(internalQuery);
|
||||
if (sourceItems.Count > MaxBatchSourceItems)
|
||||
{
|
||||
sourceItems = sourceItems.Take(MaxBatchSourceItems).ToList();
|
||||
}
|
||||
|
||||
var context = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using (context.ConfigureAwait(false))
|
||||
{
|
||||
// Phase 1: Score all candidates per source item
|
||||
var sourceIds = sourceItems.Select(i => i.Id).ToList();
|
||||
var perSourceScores = await ComputeBatchScoresAsync(sourceIds, context, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var allCandidateIds = new HashSet<Guid>();
|
||||
foreach (var (_, scores) in perSourceScores)
|
||||
{
|
||||
allCandidateIds.UnionWith(
|
||||
scores.OrderByDescending(kvp => kvp.Value)
|
||||
.Take(limit * 3)
|
||||
.Select(kvp => kvp.Key));
|
||||
}
|
||||
|
||||
var result = new Dictionary<Guid, IReadOnlyList<BaseItemDto>>();
|
||||
if (allCandidateIds.Count == 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// Phase 2: One access filter for all candidates
|
||||
var filter = new InternalItemsQuery(query.User)
|
||||
{
|
||||
IncludeItemTypes = [.. includeItemTypes],
|
||||
ExcludeItemIds = [.. query.ExcludeItemIds],
|
||||
DtoOptions = dtoOptions,
|
||||
EnableGroupByMetadataKey = true,
|
||||
EnableTotalRecordCount = false,
|
||||
IsMovie = true,
|
||||
IsPlayed = false
|
||||
};
|
||||
|
||||
_queryHelpers.PrepareFilterQuery(filter);
|
||||
var baseQuery = _queryHelpers.PrepareItemQuery(context, filter);
|
||||
baseQuery = _queryHelpers.TranslateQuery(baseQuery, context, filter);
|
||||
|
||||
var allCandidateIdsList = allCandidateIds.ToList();
|
||||
var accessibleItems = await baseQuery
|
||||
.WhereOneOrMany(allCandidateIdsList, e => e.Id)
|
||||
.Select(e => new { e.Id, e.PresentationUniqueKey })
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Phase 3: Pick top IDs per source, dedup by PresentationUniqueKey
|
||||
var allOrderedIds = new HashSet<Guid>();
|
||||
var perSourceOrderedIds = new Dictionary<Guid, List<Guid>>();
|
||||
|
||||
foreach (var item in sourceItems)
|
||||
{
|
||||
if (!perSourceScores.TryGetValue(item.Id, out var scores))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var orderedIds = accessibleItems
|
||||
.Where(x => scores.ContainsKey(x.Id))
|
||||
.OrderByDescending(x => scores.GetValueOrDefault(x.Id))
|
||||
.DistinctBy(x => x.PresentationUniqueKey)
|
||||
.Take(limit)
|
||||
.Select(x => x.Id)
|
||||
.ToList();
|
||||
|
||||
if (orderedIds.Count > 0)
|
||||
{
|
||||
perSourceOrderedIds[item.Id] = orderedIds;
|
||||
allOrderedIds.UnionWith(orderedIds);
|
||||
}
|
||||
}
|
||||
|
||||
if (allOrderedIds.Count == 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// Phase 4: One entity load for all results
|
||||
var allOrderedIdsList = allOrderedIds.ToList();
|
||||
var entities = await _queryHelpers.ApplyNavigations(
|
||||
context.BaseItems.AsNoTracking().WhereOneOrMany(allOrderedIdsList, e => e.Id),
|
||||
filter)
|
||||
.AsSplitQuery()
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var entitiesById = entities
|
||||
.Select(e => _queryHelpers.DeserializeBaseItem(e, filter.SkipDeserialization))
|
||||
.Where(dto => dto is not null)
|
||||
.ToDictionary(i => i!.Id);
|
||||
|
||||
// Phase 5: Split by source, preserving score order
|
||||
foreach (var (sourceId, orderedIds) in perSourceOrderedIds)
|
||||
{
|
||||
var items = orderedIds
|
||||
.Where(entitiesById.ContainsKey)
|
||||
.Select(id => entitiesById[id]!)
|
||||
.ToList();
|
||||
|
||||
if (items.Count > 0)
|
||||
{
|
||||
result[sourceId] = items;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<Dictionary<Guid, Dictionary<Guid, int>>> ComputeBatchScoresAsync(List<Guid> sourceIds, JellyfinDbContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = new Dictionary<Guid, Dictionary<Guid, int>>();
|
||||
foreach (var id in sourceIds)
|
||||
{
|
||||
result[id] = [];
|
||||
}
|
||||
|
||||
foreach (var (valueType, weight) in _itemValueDimensions)
|
||||
{
|
||||
var sourceRows = await context.ItemValuesMap.AsNoTracking()
|
||||
.Where(m => sourceIds.Contains(m.ItemId) && m.ItemValue.Type == valueType)
|
||||
.Select(m => new { m.ItemId, Key = m.ItemValue.CleanValue })
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var sourceMap = sourceRows.GroupBy(r => r.ItemId).ToDictionary(g => g.Key, g => g.Select(x => x.Key).ToHashSet());
|
||||
var allKeys = sourceMap.Values.SelectMany(v => v).Distinct().ToList();
|
||||
if (allKeys.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var candidateRows = await context.ItemValuesMap.AsNoTracking()
|
||||
.Where(m => m.ItemValue.Type == valueType && allKeys.Contains(m.ItemValue.CleanValue))
|
||||
.Select(m => new { m.ItemId, Key = m.ItemValue.CleanValue })
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var keyToCandidates = candidateRows.GroupBy(r => r.Key).ToDictionary(g => g.Key, g => g.Select(x => x.ItemId).ToList());
|
||||
ApplyDimensionScores(sourceIds, sourceMap, keyToCandidates, weight, result);
|
||||
}
|
||||
|
||||
var personSourceRows = await context.PeopleBaseItemMap.AsNoTracking()
|
||||
.Where(m => sourceIds.Contains(m.ItemId) && _scoredPersonTypes.Contains(m.People.PersonType))
|
||||
.Select(m => new { m.ItemId, m.PeopleId, m.People.PersonType })
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (personSourceRows.Count > 0)
|
||||
{
|
||||
var personCandidateRows = await context.PeopleBaseItemMap.AsNoTracking()
|
||||
.Where(m => context.PeopleBaseItemMap
|
||||
.Where(s => sourceIds.Contains(s.ItemId) && _scoredPersonTypes.Contains(s.People.PersonType))
|
||||
.Select(s => s.PeopleId)
|
||||
.Contains(m.PeopleId))
|
||||
.Select(m => new { m.ItemId, m.PeopleId })
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var personToCandidates = personCandidateRows
|
||||
.GroupBy(r => r.PeopleId)
|
||||
.ToDictionary(g => g.Key, g => g.Select(x => x.ItemId).ToList());
|
||||
|
||||
foreach (var weightGroup in personSourceRows.GroupBy(r => _personTypeWeights[r.PersonType!]))
|
||||
{
|
||||
var sourceMap = weightGroup
|
||||
.GroupBy(r => r.ItemId)
|
||||
.ToDictionary(g => g.Key, g => g.Select(x => x.PeopleId).ToHashSet());
|
||||
ApplyDimensionScores(sourceIds, sourceMap, personToCandidates, weightGroup.Key, result);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var sourceId in sourceIds)
|
||||
{
|
||||
var scoreMap = result[sourceId];
|
||||
scoreMap.Remove(sourceId);
|
||||
if (scoreMap.Count == 0)
|
||||
{
|
||||
result.Remove(sourceId);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void ApplyDimensionScores<TKey>(
|
||||
List<Guid> sourceIds,
|
||||
Dictionary<Guid, HashSet<TKey>> sourceMap,
|
||||
Dictionary<TKey, List<Guid>> keyToCandidates,
|
||||
int weight,
|
||||
Dictionary<Guid, Dictionary<Guid, int>> result)
|
||||
where TKey : notnull
|
||||
{
|
||||
foreach (var sourceId in sourceIds)
|
||||
{
|
||||
if (!sourceMap.TryGetValue(sourceId, out var sourceKeys))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var scoreMap = result[sourceId];
|
||||
foreach (var key in sourceKeys)
|
||||
{
|
||||
if (!keyToCandidates.TryGetValue(key, out var candidates))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var candidateId in candidates)
|
||||
{
|
||||
scoreMap[candidateId] = scoreMap.GetValueOrDefault(candidateId) + weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,12 +8,16 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Data.Enums;
|
||||
using Jellyfin.Database.Implementations.Entities;
|
||||
using Jellyfin.Database.Implementations.Enums;
|
||||
using Jellyfin.Extensions.Json;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Dto;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using MediaBrowser.Model.Dto;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.IO;
|
||||
using MediaBrowser.Model.Querying;
|
||||
@@ -30,6 +34,7 @@ public class SimilarItemsManager : ISimilarItemsManager
|
||||
private readonly IServerApplicationPaths _appPaths;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private readonly IFileSystem _fileSystem;
|
||||
private readonly IServerConfigurationManager _serverConfigurationManager;
|
||||
private ISimilarItemsProvider[] _similarItemsProviders = [];
|
||||
|
||||
/// <summary>
|
||||
@@ -39,16 +44,19 @@ public class SimilarItemsManager : ISimilarItemsManager
|
||||
/// <param name="appPaths">The server application paths.</param>
|
||||
/// <param name="libraryManager">The library manager.</param>
|
||||
/// <param name="fileSystem">The file system.</param>
|
||||
/// <param name="serverConfigurationManager">The server configuration manager.</param>
|
||||
public SimilarItemsManager(
|
||||
ILogger<SimilarItemsManager> logger,
|
||||
IServerApplicationPaths appPaths,
|
||||
ILibraryManager libraryManager,
|
||||
IFileSystem fileSystem)
|
||||
IFileSystem fileSystem,
|
||||
IServerConfigurationManager serverConfigurationManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_appPaths = appPaths;
|
||||
_libraryManager = libraryManager;
|
||||
_fileSystem = fileSystem;
|
||||
_serverConfigurationManager = serverConfigurationManager;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -225,6 +233,211 @@ public class SimilarItemsManager : ISimilarItemsManager
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<IReadOnlyList<SimilarItemsRecommendation>> GetMovieRecommendationsAsync(
|
||||
User? user,
|
||||
Guid parentId,
|
||||
int categoryLimit,
|
||||
int itemLimit,
|
||||
DtoOptions dtoOptions,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(dtoOptions);
|
||||
|
||||
var recentlyPlayedMovies = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
||||
{
|
||||
IncludeItemTypes = [BaseItemKind.Movie],
|
||||
OrderBy = [(ItemSortBy.DatePlayed, SortOrder.Descending), (ItemSortBy.Random, SortOrder.Descending)],
|
||||
Limit = 7,
|
||||
ParentId = parentId,
|
||||
Recursive = true,
|
||||
IsPlayed = true,
|
||||
EnableGroupByMetadataKey = true,
|
||||
DtoOptions = dtoOptions
|
||||
});
|
||||
|
||||
var itemTypes = new List<BaseItemKind> { BaseItemKind.Movie };
|
||||
if (_serverConfigurationManager.Configuration.EnableExternalContentInSuggestions)
|
||||
{
|
||||
itemTypes.Add(BaseItemKind.Trailer);
|
||||
itemTypes.Add(BaseItemKind.LiveTvProgram);
|
||||
}
|
||||
|
||||
var likedMovies = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
||||
{
|
||||
IncludeItemTypes = itemTypes.ToArray(),
|
||||
IsMovie = true,
|
||||
OrderBy = [(ItemSortBy.Random, SortOrder.Descending)],
|
||||
Limit = 10,
|
||||
IsFavoriteOrLiked = true,
|
||||
ExcludeItemIds = recentlyPlayedMovies.Select(i => i.Id).ToArray(),
|
||||
EnableGroupByMetadataKey = true,
|
||||
ParentId = parentId,
|
||||
Recursive = true,
|
||||
DtoOptions = dtoOptions
|
||||
});
|
||||
|
||||
var mostRecentMovies = recentlyPlayedMovies.Take(Math.Min(recentlyPlayedMovies.Count, 6)).ToList();
|
||||
var recentDirectors = GetPeopleNames(mostRecentMovies, [PersonType.Director]);
|
||||
var recentActors = GetPeopleNames(mostRecentMovies, [PersonType.Actor, PersonType.GuestStar]);
|
||||
|
||||
// Cap baseline items to categoryLimit - the round-robin can't use more categories than that.
|
||||
var recentlyPlayedBaseline = recentlyPlayedMovies.Count > categoryLimit
|
||||
? recentlyPlayedMovies.Take(categoryLimit).ToList()
|
||||
: recentlyPlayedMovies;
|
||||
var likedBaseline = likedMovies.Count > categoryLimit
|
||||
? likedMovies.Take(categoryLimit).ToList()
|
||||
: likedMovies;
|
||||
|
||||
var batchQuery = new SimilarItemsQuery
|
||||
{
|
||||
User = user,
|
||||
Limit = itemLimit,
|
||||
DtoOptions = dtoOptions
|
||||
};
|
||||
|
||||
var similarToRecentlyPlayed = await GetSimilarItemsRecommendationsAsync(
|
||||
recentlyPlayedBaseline,
|
||||
RecommendationType.SimilarToRecentlyPlayed,
|
||||
batchQuery,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var similarToLiked = await GetSimilarItemsRecommendationsAsync(
|
||||
likedBaseline,
|
||||
RecommendationType.SimilarToLikedItem,
|
||||
batchQuery,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var hasDirectorFromRecentlyPlayed = GetPersonRecommendations(user, recentDirectors, itemLimit, dtoOptions, RecommendationType.HasDirectorFromRecentlyPlayed, itemTypes);
|
||||
var hasActorFromRecentlyPlayed = GetPersonRecommendations(user, recentActors, itemLimit, dtoOptions, RecommendationType.HasActorFromRecentlyPlayed, itemTypes);
|
||||
|
||||
// Use a single enumerator per list, listed twice so MoveNext advances it
|
||||
// twice per round-robin pass (giving these categories double weight).
|
||||
// IMPORTANT: Declare as IEnumerator<T> to box the List<T>.Enumerator struct once;
|
||||
// using var would box separately per list insertion, creating independent copies.
|
||||
IEnumerator<SimilarItemsRecommendation> similarToRecentlyPlayedEnum = similarToRecentlyPlayed.GetEnumerator();
|
||||
IEnumerator<SimilarItemsRecommendation> similarToLikedEnum = similarToLiked.GetEnumerator();
|
||||
|
||||
var categoryTypes = new List<IEnumerator<SimilarItemsRecommendation>>
|
||||
{
|
||||
similarToRecentlyPlayedEnum,
|
||||
similarToRecentlyPlayedEnum,
|
||||
similarToLikedEnum,
|
||||
similarToLikedEnum,
|
||||
hasDirectorFromRecentlyPlayed.GetEnumerator(),
|
||||
hasActorFromRecentlyPlayed.GetEnumerator()
|
||||
};
|
||||
|
||||
var categories = new List<SimilarItemsRecommendation>();
|
||||
while (categories.Count < categoryLimit)
|
||||
{
|
||||
var allEmpty = true;
|
||||
foreach (var category in categoryTypes)
|
||||
{
|
||||
if (category.MoveNext())
|
||||
{
|
||||
categories.Add(category.Current);
|
||||
allEmpty = false;
|
||||
|
||||
if (categories.Count >= categoryLimit)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allEmpty)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [.. categories.OrderBy(i => i.RecommendationType)];
|
||||
}
|
||||
|
||||
private async Task<IReadOnlyList<SimilarItemsRecommendation>> GetSimilarItemsRecommendationsAsync(
|
||||
IReadOnlyList<BaseItem> baselineItems,
|
||||
RecommendationType recommendationType,
|
||||
SimilarItemsQuery query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var batchProvider = _similarItemsProviders
|
||||
.OfType<IBatchLocalSimilarItemsProvider>()
|
||||
.FirstOrDefault();
|
||||
|
||||
if (batchProvider is null || baselineItems.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var batchResults = await batchProvider.GetBatchSimilarItemsAsync(baselineItems, query, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var recommendations = new List<SimilarItemsRecommendation>(baselineItems.Count);
|
||||
foreach (var baseline in baselineItems)
|
||||
{
|
||||
if (batchResults.TryGetValue(baseline.Id, out var similar) && similar.Count > 0)
|
||||
{
|
||||
recommendations.Add(new SimilarItemsRecommendation
|
||||
{
|
||||
BaselineItemName = baseline.Name,
|
||||
CategoryId = baseline.Id,
|
||||
RecommendationType = recommendationType,
|
||||
Items = similar
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return recommendations;
|
||||
}
|
||||
|
||||
private IEnumerable<SimilarItemsRecommendation> GetPersonRecommendations(
|
||||
User? user,
|
||||
IReadOnlyList<string> names,
|
||||
int itemLimit,
|
||||
DtoOptions dtoOptions,
|
||||
RecommendationType type,
|
||||
IReadOnlyList<BaseItemKind> itemTypes)
|
||||
{
|
||||
var personTypes = type == RecommendationType.HasDirectorFromRecentlyPlayed
|
||||
? [PersonType.Director]
|
||||
: Array.Empty<string>();
|
||||
|
||||
foreach (var name in names)
|
||||
{
|
||||
var items = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
||||
{
|
||||
Person = name,
|
||||
Limit = itemLimit + 2,
|
||||
PersonTypes = personTypes,
|
||||
IncludeItemTypes = itemTypes.ToArray(),
|
||||
IsMovie = true,
|
||||
IsPlayed = false,
|
||||
EnableGroupByMetadataKey = true,
|
||||
DtoOptions = dtoOptions
|
||||
})
|
||||
.DistinctBy(i => i.GetProviderId(MetadataProvider.Imdb) ?? Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture))
|
||||
.Take(itemLimit)
|
||||
.ToList();
|
||||
|
||||
if (items.Count > 0)
|
||||
{
|
||||
yield return new SimilarItemsRecommendation
|
||||
{
|
||||
BaselineItemName = name,
|
||||
CategoryId = name.GetMD5(),
|
||||
RecommendationType = type,
|
||||
Items = items
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IReadOnlyList<string> GetPeopleNames(IReadOnlyList<BaseItem> items, IReadOnlyList<string> personTypes)
|
||||
{
|
||||
var itemIds = items.Select(i => i.Id).ToArray();
|
||||
return _libraryManager.GetPeopleNamesByItems(itemIds, personTypes, limit: 0);
|
||||
}
|
||||
|
||||
private List<(BaseItem Item, float Score)> ResolveRemoteReferences(
|
||||
IReadOnlyList<SimilarItemReference> references,
|
||||
int providerOrder,
|
||||
|
||||
Reference in New Issue
Block a user