2013-05-25 23:52:41 +00:00
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 ;
2013-12-07 15:52:38 +00:00
using ServiceStack ;
2013-05-25 23:52:41 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
namespace MediaBrowser.Api
{
/// <summary>
2013-08-09 15:55:22 +00:00
/// Class BaseGetSimilarItemsFromItem
2013-05-25 23:52:41 +00:00
/// </summary>
2013-08-09 15:55:22 +00: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 ; }
}
2013-11-22 15:33:14 +00:00
public class BaseGetSimilarItems : IReturn < ItemsResult > , IHasItemFields
2013-05-25 23:52:41 +00: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")]
public Guid ? UserId { get ; set ; }
/// <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-04 02:02:49 +00:00
/// <summary>
/// Fields to return within the items, in addition to basic information
/// </summary>
/// <value>The fields.</value>
2014-05-27 14:30:21 +00:00
[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, 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-04 02:02:49 +00:00
public string Fields { get ; set ; }
2013-05-25 23:52:41 +00:00
}
2013-09-17 02:44:06 +00:00
2013-05-25 23:52:41 +00:00
/// <summary>
/// Class SimilarItemsHelper
/// </summary>
public static class SimilarItemsHelper
{
/// <summary>
/// Gets the similar items.
/// </summary>
/// <param name="userManager">The user manager.</param>
2013-06-18 19:16:27 +00:00
/// <param name="itemRepository">The item repository.</param>
2013-05-25 23:52:41 +00:00
/// <param name="libraryManager">The library manager.</param>
/// <param name="userDataRepository">The user data repository.</param>
2013-09-04 17:02:19 +00:00
/// <param name="dtoService">The dto service.</param>
2013-05-25 23:52:41 +00:00
/// <param name="logger">The logger.</param>
/// <param name="request">The request.</param>
/// <param name="includeInSearch">The include in search.</param>
/// <param name="getSimilarityScore">The get similarity score.</param>
/// <returns>ItemsResult.</returns>
2013-10-02 16:08:58 +00:00
internal static ItemsResult GetSimilarItemsResult ( IUserManager userManager , IItemRepository itemRepository , ILibraryManager libraryManager , IUserDataManager userDataRepository , IDtoService dtoService , ILogger logger , BaseGetSimilarItemsFromItem request , Func < BaseItem , bool > includeInSearch , Func < BaseItem , BaseItem , int > getSimilarityScore )
2013-05-25 23:52:41 +00:00
{
var user = request . UserId . HasValue ? userManager . GetUserById ( request . UserId . Value ) : null ;
var item = string . IsNullOrEmpty ( request . Id ) ?
( request . UserId . HasValue ? user . RootFolder :
2014-04-25 20:15:50 +00:00
libraryManager . RootFolder ) : libraryManager . GetItemById ( request . Id ) ;
2013-05-25 23:52:41 +00:00
2013-06-04 02:02:49 +00:00
var fields = request . GetItemFields ( ) . ToList ( ) ;
2013-05-25 23:52:41 +00:00
var inputItems = user = = null
2013-09-27 12:24:28 +00:00
? libraryManager . RootFolder . GetRecursiveChildren ( i = > i . Id ! = item . Id )
: user . RootFolder . GetRecursiveChildren ( user , i = > i . Id ! = item . Id ) ;
2013-05-25 23:52:41 +00:00
2014-03-12 02:11:01 +00:00
var items = GetSimilaritems ( item , inputItems . Where ( includeInSearch ) , getSimilarityScore )
2013-09-20 00:53:18 +00:00
. ToList ( ) ;
IEnumerable < BaseItem > returnItems = items ;
if ( request . Limit . HasValue )
{
returnItems = returnItems . Take ( request . Limit . Value ) ;
}
2013-05-25 23:52:41 +00:00
var result = new ItemsResult
{
2013-09-20 00:53:18 +00:00
Items = returnItems . Select ( i = > dtoService . GetBaseItemDto ( i , fields , user ) ) . ToArray ( ) ,
2013-05-25 23:52:41 +00:00
2013-09-20 00:53:18 +00:00
TotalRecordCount = items . Count
2013-05-25 23:52:41 +00:00
} ;
return result ;
}
/// <summary>
/// Gets the similaritems.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="inputItems">The input items.</param>
/// <param name="getSimilarityScore">The get similarity score.</param>
/// <returns>IEnumerable{BaseItem}.</returns>
2014-03-12 02:11:01 +00:00
internal static IEnumerable < BaseItem > GetSimilaritems ( BaseItem item , IEnumerable < BaseItem > inputItems , Func < BaseItem , BaseItem , int > getSimilarityScore )
2013-05-25 23:52:41 +00:00
{
2014-03-12 02:11:01 +00:00
var itemId = item . Id ;
inputItems = inputItems . Where ( i = > i . Id ! = itemId ) ;
2013-05-25 23:52:41 +00:00
2013-09-27 12:24:28 +00:00
return inputItems . Select ( i = > new Tuple < BaseItem , int > ( i , getSimilarityScore ( item , i ) ) )
2013-07-21 14:20:57 +00:00
. Where ( i = > i . Item2 > 2 )
2013-05-25 23:52:41 +00:00
. OrderByDescending ( i = > i . Item2 )
. Select ( i = > i . Item1 ) ;
}
2013-12-05 16:50:21 +00:00
private static IEnumerable < string > GetTags ( BaseItem item )
{
var hasTags = item as IHasTags ;
if ( hasTags ! = null )
{
return hasTags . Tags ;
}
return new List < string > ( ) ;
}
2014-01-14 15:50:39 +00:00
private static IEnumerable < string > GetKeywords ( BaseItem item )
{
var hasTags = item as IHasKeywords ;
if ( hasTags ! = null )
{
return hasTags . Keywords ;
}
return new List < string > ( ) ;
}
2013-05-25 23:52:41 +00:00
/// <summary>
/// Gets the similiarity score.
/// </summary>
/// <param name="item1">The item1.</param>
/// <param name="item2">The item2.</param>
/// <returns>System.Int32.</returns>
internal static int GetSimiliarityScore ( BaseItem item1 , BaseItem item2 )
{
var points = 0 ;
if ( ! string . IsNullOrEmpty ( item1 . OfficialRating ) & & string . Equals ( item1 . OfficialRating , item2 . OfficialRating , StringComparison . OrdinalIgnoreCase ) )
{
2014-03-13 09:00:47 +00:00
points + = 10 ;
2013-05-25 23:52:41 +00:00
}
// Find common genres
2013-08-07 16:00:20 +00:00
points + = item1 . Genres . Where ( i = > item2 . Genres . Contains ( i , StringComparer . OrdinalIgnoreCase ) ) . Sum ( i = > 10 ) ;
2013-05-25 23:52:41 +00:00
// Find common tags
2013-12-05 16:50:21 +00:00
points + = GetTags ( item1 ) . Where ( i = > GetTags ( item2 ) . Contains ( i , StringComparer . OrdinalIgnoreCase ) ) . Sum ( i = > 10 ) ;
2013-05-25 23:52:41 +00:00
2014-01-14 15:50:39 +00:00
// Find common keywords
points + = GetKeywords ( item1 ) . Where ( i = > GetKeywords ( item2 ) . Contains ( i , StringComparer . OrdinalIgnoreCase ) ) . Sum ( i = > 10 ) ;
2013-05-25 23:52:41 +00:00
// Find common studios
points + = item1 . Studios . Where ( i = > item2 . Studios . Contains ( i , StringComparer . OrdinalIgnoreCase ) ) . Sum ( i = > 3 ) ;
2013-08-07 20:44:57 +00:00
var item2PeopleNames = item2 . People . Select ( i = > i . Name )
. Distinct ( StringComparer . OrdinalIgnoreCase )
. ToDictionary ( i = > i , StringComparer . OrdinalIgnoreCase ) ;
2013-05-25 23:52:41 +00:00
2013-08-07 20:44:57 +00:00
points + = item1 . People . Where ( i = > item2PeopleNames . ContainsKey ( i . Name ) ) . Sum ( i = >
2013-05-25 23:52:41 +00:00
{
2013-08-07 16:00:20 +00:00
if ( string . Equals ( i . Type , PersonType . Director , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( i . Role , PersonType . Director , StringComparison . OrdinalIgnoreCase ) )
2013-05-25 23:52:41 +00:00
{
return 5 ;
}
2013-08-07 16:00:20 +00:00
if ( string . Equals ( i . Type , PersonType . Actor , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( i . Role , PersonType . Actor , StringComparison . OrdinalIgnoreCase ) )
2013-05-25 23:52:41 +00:00
{
return 3 ;
}
2013-08-07 16:00:20 +00:00
if ( string . Equals ( i . Type , PersonType . Composer , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( i . Role , PersonType . Composer , StringComparison . OrdinalIgnoreCase ) )
2013-05-25 23:52:41 +00:00
{
return 3 ;
}
2013-08-07 16:00:20 +00:00
if ( string . Equals ( i . Type , PersonType . GuestStar , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( i . Role , PersonType . GuestStar , StringComparison . OrdinalIgnoreCase ) )
2013-05-25 23:52:41 +00:00
{
return 3 ;
}
2013-08-07 16:00:20 +00:00
if ( string . Equals ( i . Type , PersonType . Writer , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( i . Role , PersonType . Writer , StringComparison . OrdinalIgnoreCase ) )
2013-05-25 23:52:41 +00: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 15:59:47 +00:00
points + = 2 ;
2013-05-25 23:52:41 +00:00
}
// And more if within five years
if ( diff < 5 )
{
2013-06-13 15:59:47 +00:00
points + = 2 ;
2013-05-25 23:52:41 +00:00
}
}
return points ;
}
}
}