using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Querying; using ServiceStack.ServiceHost; using System; using System.Collections.Generic; using System.Linq; namespace MediaBrowser.Api { /// /// Class BaseGetSimilarItemsFromItem /// public class BaseGetSimilarItemsFromItem : BaseGetSimilarItems { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Id { get; set; } } public class BaseGetSimilarItems : IReturn { /// /// Gets or sets the user id. /// /// The user id. [ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public Guid? UserId { get; set; } /// /// The maximum number of items to return /// /// The limit. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")] public int? Limit { get; set; } /// /// Fields to return within the items, in addition to basic information /// /// The fields. [ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, CriticRatingSummary, DateCreated, Genres, HomePageUrl, ItemCounts, IndexOptions, MediaStreams, Overview, OverviewHtml, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)] public string Fields { get; set; } /// /// Gets the item fields. /// /// IEnumerable{ItemFields}. public IEnumerable GetItemFields() { var val = Fields; if (string.IsNullOrEmpty(val)) { return new ItemFields[] { }; } return val.Split(',').Select(v => { ItemFields value; if (Enum.TryParse(v, true, out value)) { return (ItemFields?)value; } return null; }).Where(i => i.HasValue).Select(i => i.Value); } } /// /// Class SimilarItemsHelper /// public static class SimilarItemsHelper { /// /// Gets the similar items. /// /// The user manager. /// The item repository. /// The library manager. /// The user data repository. /// The dto service. /// The logger. /// The request. /// The include in search. /// The get similarity score. /// ItemsResult. internal static ItemsResult GetSimilarItemsResult(IUserManager userManager, IItemRepository itemRepository, ILibraryManager libraryManager, IUserDataRepository userDataRepository, IDtoService dtoService, ILogger logger, BaseGetSimilarItemsFromItem request, Func includeInSearch, Func getSimilarityScore) { var user = request.UserId.HasValue ? userManager.GetUserById(request.UserId.Value) : null; var item = string.IsNullOrEmpty(request.Id) ? (request.UserId.HasValue ? user.RootFolder : (Folder)libraryManager.RootFolder) : dtoService.GetItemByDtoId(request.Id, request.UserId); var fields = request.GetItemFields().ToList(); var inputItems = user == null ? libraryManager.RootFolder.RecursiveChildren : user.RootFolder.GetRecursiveChildren(user); var items = GetSimilaritems(item, inputItems, includeInSearch, getSimilarityScore) .ToArray(); var result = new ItemsResult { Items = items.Take(request.Limit ?? items.Length).Select(i => dtoService.GetBaseItemDto(i, fields, user)).Select(t => t.Result).ToArray(), TotalRecordCount = items.Length }; return result; } /// /// Gets the similaritems. /// /// The item. /// The input items. /// The include in search. /// The get similarity score. /// IEnumerable{BaseItem}. internal static IEnumerable GetSimilaritems(BaseItem item, IEnumerable inputItems, Func includeInSearch, Func getSimilarityScore) { inputItems = inputItems.Where(includeInSearch); // Avoid implicitly captured closure var currentItem = item; return inputItems.Where(i => i.Id != currentItem.Id) .Select(i => new Tuple(i, getSimilarityScore(item, i))) .Where(i => i.Item2 > 2) .OrderByDescending(i => i.Item2) .Select(i => i.Item1); } /// /// Gets the similiarity score. /// /// The item1. /// The item2. /// System.Int32. internal static int GetSimiliarityScore(BaseItem item1, BaseItem item2) { var points = 0; if (!string.IsNullOrEmpty(item1.OfficialRating) && string.Equals(item1.OfficialRating, item2.OfficialRating, StringComparison.OrdinalIgnoreCase)) { points += 1; } // Find common genres points += item1.Genres.Where(i => item2.Genres.Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 10); // Find common tags points += item1.Tags.Where(i => item2.Tags.Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 10); // Find common studios points += item1.Studios.Where(i => item2.Studios.Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 3); var item2PeopleNames = item2.People.Select(i => i.Name) .Distinct(StringComparer.OrdinalIgnoreCase) .ToDictionary(i => i, StringComparer.OrdinalIgnoreCase); points += item1.People.Where(i => item2PeopleNames.ContainsKey(i.Name)).Sum(i => { if (string.Equals(i.Type, PersonType.Director, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Director, StringComparison.OrdinalIgnoreCase)) { return 5; } if (string.Equals(i.Type, PersonType.Actor, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Actor, StringComparison.OrdinalIgnoreCase)) { return 3; } if (string.Equals(i.Type, PersonType.Composer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Composer, StringComparison.OrdinalIgnoreCase)) { return 3; } if (string.Equals(i.Type, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase)) { return 3; } if (string.Equals(i.Type, PersonType.Writer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Writer, StringComparison.OrdinalIgnoreCase)) { return 2; } return 1; }); if (item1.ProductionYear.HasValue && item2.ProductionYear.HasValue) { var diff = Math.Abs(item1.ProductionYear.Value - item2.ProductionYear.Value); // Add if they came out within the same decade if (diff < 10) { points += 2; } // And more if within five years if (diff < 5) { points += 2; } } return points; } } }