2019-01-13 13:01:16 -07:00
using System ;
2019-01-13 12:24:58 -07:00
using System.Collections.Generic ;
using System.Linq ;
using MediaBrowser.Controller.Dto ;
2013-05-25 16:52:41 -07:00
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.Persistence ;
2019-01-13 12:24:58 -07:00
using MediaBrowser.Model.Dto ;
2013-05-25 16:52:41 -07:00
using MediaBrowser.Model.Entities ;
using MediaBrowser.Model.Querying ;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.Services ;
2019-01-13 12:24:58 -07:00
using Microsoft.Extensions.Logging ;
2013-05-25 16:52:41 -07:00
namespace MediaBrowser.Api
{
/// <summary>
2013-08-09 08:55:22 -07:00
/// Class BaseGetSimilarItemsFromItem
2013-05-25 16:52:41 -07:00
/// </summary>
2013-08-09 08:55:22 -07:00
public class BaseGetSimilarItemsFromItem : BaseGetSimilarItems
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
2016-07-22 10:54:43 -07:00
2016-07-22 15:10:39 -07:00
public string ExcludeArtistIds { get ; set ; }
2013-08-09 08:55:22 -07:00
}
2017-08-19 12:43:35 -07:00
public class BaseGetSimilarItems : IReturn < QueryResult < BaseItemDto > > , IHasDtoOptions
2013-05-25 16:52:41 -07:00
{
2016-08-17 12:28:43 -07:00
[ApiMember(Name = "EnableImages", Description = "Optional, include image information in output", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? EnableImages { get ; set ; }
[ApiMember(Name = "EnableUserData", Description = "Optional, include user data", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? EnableUserData { get ; set ; }
[ApiMember(Name = "ImageTypeLimit", Description = "Optional, the max number of images to return, per image type", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? ImageTypeLimit { get ; set ; }
[ApiMember(Name = "EnableImageTypes", Description = "Optional. The image types to include in the output.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string EnableImageTypes { get ; set ; }
2013-05-25 16:52:41 -07:00
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-05-25 16:52:41 -07:00
/// <summary>
/// The maximum number of items to return
/// </summary>
/// <value>The limit.</value>
[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 ; }
2013-06-03 19:02:49 -07:00
/// <summary>
/// Fields to return within the items, in addition to basic information
/// </summary>
/// <value>The fields.</value>
2017-04-28 23:22:33 -07:00
[ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
2013-06-03 19:02:49 -07:00
public string Fields { get ; set ; }
2013-05-25 16:52:41 -07:00
}
2013-09-16 19:44:06 -07:00
2013-05-25 16:52:41 -07:00
/// <summary>
/// Class SimilarItemsHelper
/// </summary>
public static class SimilarItemsHelper
{
2017-08-27 17:33:05 -07:00
internal static QueryResult < BaseItemDto > GetSimilarItemsResult ( DtoOptions dtoOptions , IUserManager userManager , IItemRepository itemRepository , ILibraryManager libraryManager , IUserDataManager userDataRepository , IDtoService dtoService , ILogger logger , BaseGetSimilarItemsFromItem request , Type [ ] includeTypes , Func < BaseItem , List < PersonInfo > , List < PersonInfo > , BaseItem , int > getSimilarityScore )
2013-05-25 16:52:41 -07:00
{
2018-09-12 10:26:21 -07:00
var user = ! request . UserId . Equals ( Guid . Empty ) ? userManager . GetUserById ( request . UserId ) : null ;
2013-05-25 16:52:41 -07:00
var item = string . IsNullOrEmpty ( request . Id ) ?
2018-09-12 10:26:21 -07:00
( ! request . UserId . Equals ( Guid . Empty ) ? libraryManager . GetUserRootFolder ( ) :
2014-04-25 13:15:50 -07:00
libraryManager . RootFolder ) : libraryManager . GetItemById ( request . Id ) ;
2013-05-25 16:52:41 -07:00
2016-03-24 20:53:42 -07:00
var query = new InternalItemsQuery ( user )
{
2018-12-28 08:48:26 -07:00
IncludeItemTypes = includeTypes . Select ( i = > i . Name ) . ToArray ( ) ,
2016-10-07 22:57:38 -07:00
Recursive = true ,
2016-10-10 11:18:28 -07:00
DtoOptions = dtoOptions
2016-03-24 20:53:42 -07:00
} ;
2013-05-25 16:52:41 -07:00
2016-07-22 15:10:39 -07:00
// ExcludeArtistIds
if ( ! string . IsNullOrEmpty ( request . ExcludeArtistIds ) )
2016-07-22 10:54:43 -07:00
{
2018-09-12 10:26:21 -07:00
query . ExcludeArtistIds = BaseApiService . GetGuids ( request . ExcludeArtistIds ) ;
2016-07-22 10:54:43 -07:00
}
2016-03-24 20:53:42 -07:00
var inputItems = libraryManager . GetItemList ( query ) ;
2013-05-25 16:52:41 -07:00
2015-06-20 20:35:22 -07:00
var items = GetSimilaritems ( item , libraryManager , inputItems , getSimilarityScore )
2013-09-19 17:53:18 -07:00
. ToList ( ) ;
2019-01-13 13:37:13 -07:00
var returnItems = items ;
2013-09-19 17:53:18 -07:00
if ( request . Limit . HasValue )
{
2017-08-19 12:43:35 -07:00
returnItems = returnItems . Take ( request . Limit . Value ) . ToList ( ) ;
2013-09-19 17:53:18 -07:00
}
2013-05-25 16:52:41 -07:00
2017-08-27 17:33:05 -07:00
var dtos = dtoService . GetBaseItemDtos ( returnItems , dtoOptions , user ) ;
2016-06-18 23:18:29 -07:00
return new QueryResult < BaseItemDto >
2013-05-25 16:52:41 -07:00
{
2017-08-19 12:43:35 -07:00
Items = dtos ,
2013-05-25 16:52:41 -07:00
2013-09-19 17:53:18 -07:00
TotalRecordCount = items . Count
2013-05-25 16:52:41 -07:00
} ;
}
/// <summary>
/// Gets the similaritems.
/// </summary>
/// <param name="item">The item.</param>
2015-06-20 20:35:22 -07:00
/// <param name="libraryManager">The library manager.</param>
2013-05-25 16:52:41 -07:00
/// <param name="inputItems">The input items.</param>
/// <param name="getSimilarityScore">The get similarity score.</param>
/// <returns>IEnumerable{BaseItem}.</returns>
2015-07-08 09:10:34 -07:00
internal static IEnumerable < BaseItem > GetSimilaritems ( BaseItem item , ILibraryManager libraryManager , IEnumerable < BaseItem > inputItems , Func < BaseItem , List < PersonInfo > , List < PersonInfo > , BaseItem , int > getSimilarityScore )
2013-05-25 16:52:41 -07:00
{
2014-03-11 19:11:01 -07:00
var itemId = item . Id ;
inputItems = inputItems . Where ( i = > i . Id ! = itemId ) ;
2015-07-08 09:10:34 -07:00
var itemPeople = libraryManager . GetPeople ( item ) ;
var allPeople = libraryManager . GetPeople ( new InternalPeopleQuery
{
AppearsInItemId = item . Id
} ) ;
2013-05-25 16:52:41 -07:00
2015-07-08 09:10:34 -07:00
return inputItems . Select ( i = > new Tuple < BaseItem , int > ( i , getSimilarityScore ( item , itemPeople , allPeople , i ) ) )
2013-07-21 07:20:57 -07:00
. Where ( i = > i . Item2 > 2 )
2013-05-25 16:52:41 -07:00
. OrderByDescending ( i = > i . Item2 )
. Select ( i = > i . Item1 ) ;
}
2013-12-05 09:50:21 -07:00
private static IEnumerable < string > GetTags ( BaseItem item )
{
2016-06-02 10:43:29 -07:00
return item . Tags ;
2013-12-05 09:50:21 -07:00
}
2013-05-25 16:52:41 -07:00
/// <summary>
/// Gets the similiarity score.
/// </summary>
/// <param name="item1">The item1.</param>
2015-07-08 09:10:34 -07:00
/// <param name="item1People">The item1 people.</param>
/// <param name="allPeople">All people.</param>
2013-05-25 16:52:41 -07:00
/// <param name="item2">The item2.</param>
/// <returns>System.Int32.</returns>
2015-07-08 09:10:34 -07:00
internal static int GetSimiliarityScore ( BaseItem item1 , List < PersonInfo > item1People , List < PersonInfo > allPeople , BaseItem item2 )
2013-05-25 16:52:41 -07:00
{
var points = 0 ;
if ( ! string . IsNullOrEmpty ( item1 . OfficialRating ) & & string . Equals ( item1 . OfficialRating , item2 . OfficialRating , StringComparison . OrdinalIgnoreCase ) )
{
2014-03-13 02:00:47 -07:00
points + = 10 ;
2013-05-25 16:52:41 -07:00
}
// Find common genres
2013-08-07 09:00:20 -07:00
points + = item1 . Genres . Where ( i = > item2 . Genres . Contains ( i , StringComparer . OrdinalIgnoreCase ) ) . Sum ( i = > 10 ) ;
2013-05-25 16:52:41 -07:00
// Find common tags
2013-12-05 09:50:21 -07:00
points + = GetTags ( item1 ) . Where ( i = > GetTags ( item2 ) . Contains ( i , StringComparer . OrdinalIgnoreCase ) ) . Sum ( i = > 10 ) ;
2013-05-25 16:52:41 -07:00
// Find common studios
points + = item1 . Studios . Where ( i = > item2 . Studios . Contains ( i , StringComparer . OrdinalIgnoreCase ) ) . Sum ( i = > 3 ) ;
2015-07-08 09:10:34 -07:00
var item2PeopleNames = allPeople . Where ( i = > i . ItemId = = item2 . Id )
. Select ( i = > i . Name )
. Where ( i = > ! string . IsNullOrWhiteSpace ( i ) )
2015-04-09 14:11:57 -07:00
. DistinctNames ( )
2013-08-07 13:44:57 -07:00
. ToDictionary ( i = > i , StringComparer . OrdinalIgnoreCase ) ;
2013-05-25 16:52:41 -07:00
2015-07-08 09:10:34 -07:00
points + = item1People . Where ( i = > item2PeopleNames . ContainsKey ( i . Name ) ) . Sum ( i = >
2013-05-25 16:52:41 -07:00
{
2013-08-07 09:00:20 -07:00
if ( string . Equals ( i . Type , PersonType . Director , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( i . Role , PersonType . Director , StringComparison . OrdinalIgnoreCase ) )
2013-05-25 16:52:41 -07:00
{
return 5 ;
}
2013-08-07 09:00:20 -07:00
if ( string . Equals ( i . Type , PersonType . Actor , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( i . Role , PersonType . Actor , StringComparison . OrdinalIgnoreCase ) )
2013-05-25 16:52:41 -07:00
{
return 3 ;
}
2013-08-07 09:00:20 -07:00
if ( string . Equals ( i . Type , PersonType . Composer , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( i . Role , PersonType . Composer , StringComparison . OrdinalIgnoreCase ) )
2013-05-25 16:52:41 -07:00
{
return 3 ;
}
2013-08-07 09:00:20 -07:00
if ( string . Equals ( i . Type , PersonType . GuestStar , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( i . Role , PersonType . GuestStar , StringComparison . OrdinalIgnoreCase ) )
2013-05-25 16:52:41 -07:00
{
return 3 ;
}
2013-08-07 09:00:20 -07:00
if ( string . Equals ( i . Type , PersonType . Writer , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( i . Role , PersonType . Writer , StringComparison . OrdinalIgnoreCase ) )
2013-05-25 16:52:41 -07:00
{
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 )
{
2013-06-13 08:59:47 -07:00
points + = 2 ;
2013-05-25 16:52:41 -07:00
}
// And more if within five years
if ( diff < 5 )
{
2013-06-13 08:59:47 -07:00
points + = 2 ;
2013-05-25 16:52:41 -07:00
}
}
return points ;
}
}
}