2019-01-13 20:01:16 +00:00
using System ;
2019-01-13 19:24:58 +00:00
using System.Collections.Generic ;
using System.Linq ;
using MediaBrowser.Common.Extensions ;
using MediaBrowser.Controller.Configuration ;
2014-03-12 02:11:01 +00:00
using MediaBrowser.Controller.Dto ;
2013-09-04 17:02:19 +00:00
using MediaBrowser.Controller.Entities ;
2013-05-25 23:52:41 +00:00
using MediaBrowser.Controller.Entities.Movies ;
using MediaBrowser.Controller.Library ;
2019-01-13 19:24:58 +00:00
using MediaBrowser.Controller.LiveTv ;
2014-07-02 18:34:08 +00:00
using MediaBrowser.Controller.Net ;
2014-03-12 02:11:01 +00:00
using MediaBrowser.Model.Dto ;
using MediaBrowser.Model.Entities ;
2016-10-22 02:08:34 +00:00
using MediaBrowser.Model.Extensions ;
2019-01-13 19:24:58 +00:00
using MediaBrowser.Model.Querying ;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services ;
2013-05-25 23:52:41 +00:00
2014-03-07 15:53:23 +00:00
namespace MediaBrowser.Api.Movies
2013-05-25 23:52:41 +00:00
{
2014-03-25 21:13:55 +00:00
[Route("/Movies/Recommendations", "GET", Summary = "Gets movie recommendations")]
2017-05-21 07:25:49 +00:00
public class GetMovieRecommendations : IReturn < RecommendationDto [ ] > , IHasDtoOptions
2014-03-12 02:11:01 +00:00
{
[ApiMember(Name = "CategoryLimit", Description = "The max number of categories to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int CategoryLimit { get ; set ; }
[ApiMember(Name = "ItemLimit", Description = "The max number of items to return per category", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int ItemLimit { get ; set ; }
/// <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 17:26:21 +00:00
public Guid UserId { get ; set ; }
2014-03-12 02:11:01 +00:00
2014-05-02 02:54:33 +00:00
/// <summary>
/// Specify this to localize the search to a specific item or folder. Omit to use the root.
/// </summary>
/// <value>The parent id.</value>
[ApiMember(Name = "ParentId", Description = "Specify this to localize the search to a specific item or folder. Omit to use the root", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string ParentId { get ; set ; }
2014-09-30 04:47:30 +00:00
2017-05-21 07:25:49 +00: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 ; }
2014-03-12 02:11:01 +00:00
public GetMovieRecommendations ( )
{
CategoryLimit = 5 ;
ItemLimit = 8 ;
}
public string Fields { get ; set ; }
}
2013-05-25 23:52:41 +00:00
/// <summary>
/// Class MoviesService
/// </summary>
2014-07-02 18:34:08 +00:00
[Authenticated]
2013-05-25 23:52:41 +00:00
public class MoviesService : BaseApiService
{
/// <summary>
/// The _user manager
/// </summary>
private readonly IUserManager _userManager ;
private readonly ILibraryManager _libraryManager ;
2013-09-04 17:02:19 +00:00
private readonly IDtoService _dtoService ;
2016-08-31 21:07:02 +00:00
private readonly IServerConfigurationManager _config ;
2016-11-10 14:41:24 +00:00
private readonly IAuthorizationContext _authContext ;
2013-09-04 17:02:19 +00:00
2013-05-25 23:52:41 +00:00
/// <summary>
2016-03-19 15:38:05 +00:00
/// Initializes a new instance of the <see cref="MoviesService" /> class.
2013-05-25 23:52:41 +00:00
/// </summary>
2016-11-21 08:54:53 +00:00
public MoviesService ( IUserManager userManager , ILibraryManager libraryManager , IDtoService dtoService , IServerConfigurationManager config , IAuthorizationContext authContext )
2013-05-25 23:52:41 +00:00
{
_userManager = userManager ;
_libraryManager = libraryManager ;
2013-09-04 17:02:19 +00:00
_dtoService = dtoService ;
2016-08-31 21:07:02 +00:00
_config = config ;
2016-11-10 14:41:24 +00:00
_authContext = authContext ;
2013-05-25 23:52:41 +00:00
}
2017-05-21 07:25:49 +00:00
public object Get ( GetMovieRecommendations request )
2014-03-12 02:11:01 +00:00
{
2015-05-29 23:51:33 +00:00
var user = _userManager . GetUserById ( request . UserId ) ;
2014-03-12 02:11:01 +00:00
2016-11-10 14:41:24 +00:00
var dtoOptions = GetDtoOptions ( _authContext , request ) ;
2014-12-27 05:08:39 +00:00
2016-06-01 05:50:00 +00:00
var result = GetRecommendationCategories ( user , request . ParentId , request . CategoryLimit , request . ItemLimit , dtoOptions ) ;
2014-03-12 02:11:01 +00:00
return ToOptimizedResult ( result ) ;
}
2018-09-12 17:26:21 +00:00
public QueryResult < BaseItemDto > GetSimilarItemsResult ( BaseGetSimilarItemsFromItem request )
2014-09-30 04:47:30 +00:00
{
2018-09-12 17:26:21 +00:00
var user = ! request . UserId . Equals ( Guid . Empty ) ? _userManager . GetUserById ( request . UserId ) : null ;
2014-09-30 04:47:30 +00:00
var item = string . IsNullOrEmpty ( request . Id ) ?
2018-09-12 17:26:21 +00:00
( ! request . UserId . Equals ( Guid . Empty ) ? _libraryManager . GetUserRootFolder ( ) :
2014-09-30 04:47:30 +00:00
_libraryManager . RootFolder ) : _libraryManager . GetItemById ( request . Id ) ;
2016-05-30 18:16:44 +00:00
2016-08-31 21:07:02 +00:00
var itemTypes = new List < string > { typeof ( Movie ) . Name } ;
if ( _config . Configuration . EnableExternalContentInSuggestions )
{
itemTypes . Add ( typeof ( Trailer ) . Name ) ;
itemTypes . Add ( typeof ( LiveTvProgram ) . Name ) ;
}
2016-11-10 14:41:24 +00:00
var dtoOptions = GetDtoOptions ( _authContext , request ) ;
2016-10-08 05:57:38 +00:00
2016-06-01 05:50:00 +00:00
var itemsResult = _libraryManager . GetItemList ( new InternalItemsQuery ( user )
2015-11-10 18:58:05 +00:00
{
2016-06-01 05:50:00 +00:00
Limit = request . Limit ,
2018-12-28 15:48:26 +00:00
IncludeItemTypes = itemTypes . ToArray ( ) ,
2016-06-01 05:50:00 +00:00
IsMovie = true ,
2016-06-29 03:17:27 +00:00
SimilarTo = item ,
2016-10-08 05:57:38 +00:00
EnableGroupByMetadataKey = true ,
2016-10-10 18:18:28 +00:00
DtoOptions = dtoOptions
2016-06-29 03:17:27 +00:00
2017-08-09 19:56:38 +00:00
} ) ;
2017-08-28 00:33:05 +00:00
var returnList = _dtoService . GetBaseItemDtos ( itemsResult , dtoOptions , user ) ;
2015-01-24 19:03:55 +00:00
2016-06-01 05:50:00 +00:00
var result = new QueryResult < BaseItemDto >
2014-09-30 04:47:30 +00:00
{
2017-08-19 19:43:35 +00:00
Items = returnList ,
2014-09-30 04:47:30 +00:00
2016-06-01 05:50:00 +00:00
TotalRecordCount = itemsResult . Count
2014-09-30 04:47:30 +00:00
} ;
return result ;
}
2016-06-01 05:50:00 +00:00
private IEnumerable < RecommendationDto > GetRecommendationCategories ( User user , string parentId , int categoryLimit , int itemLimit , DtoOptions dtoOptions )
2014-03-12 02:11:01 +00:00
{
var categories = new List < RecommendationDto > ( ) ;
2018-09-12 17:26:21 +00:00
var parentIdGuid = string . IsNullOrWhiteSpace ( parentId ) ? Guid . Empty : new Guid ( parentId ) ;
2016-06-30 19:01:48 +00:00
2016-05-31 18:42:32 +00:00
var query = new InternalItemsQuery ( user )
{
IncludeItemTypes = new [ ]
2014-03-12 02:11:01 +00:00
{
2016-05-31 18:42:32 +00:00
typeof ( Movie ) . Name ,
//typeof(Trailer).Name,
//typeof(LiveTvProgram).Name
} ,
// IsMovie = true
2018-09-12 17:26:21 +00:00
OrderBy = new [ ] { ItemSortBy . DatePlayed , ItemSortBy . Random } . Select ( i = > new ValueTuple < string , SortOrder > ( i , SortOrder . Descending ) ) . ToArray ( ) ,
2016-06-30 19:01:48 +00:00
Limit = 7 ,
ParentId = parentIdGuid ,
2016-08-25 03:12:25 +00:00
Recursive = true ,
2016-10-08 05:57:38 +00:00
IsPlayed = true ,
2016-10-10 18:18:28 +00:00
DtoOptions = dtoOptions
2016-05-31 18:42:32 +00:00
} ;
2014-03-12 02:11:01 +00:00
2017-08-09 19:56:38 +00:00
var recentlyPlayedMovies = _libraryManager . GetItemList ( query ) ;
2014-03-12 02:11:01 +00:00
2016-08-31 21:07:02 +00:00
var itemTypes = new List < string > { typeof ( Movie ) . Name } ;
if ( _config . Configuration . EnableExternalContentInSuggestions )
{
itemTypes . Add ( typeof ( Trailer ) . Name ) ;
itemTypes . Add ( typeof ( LiveTvProgram ) . Name ) ;
}
2016-05-31 18:42:32 +00:00
var likedMovies = _libraryManager . GetItemList ( new InternalItemsQuery ( user )
{
2018-12-28 15:48:26 +00:00
IncludeItemTypes = itemTypes . ToArray ( ) ,
2016-05-31 18:42:32 +00:00
IsMovie = true ,
2018-09-12 17:26:21 +00:00
OrderBy = new [ ] { ItemSortBy . Random } . Select ( i = > new ValueTuple < string , SortOrder > ( i , SortOrder . Descending ) ) . ToArray ( ) ,
2016-05-31 18:42:32 +00:00
Limit = 10 ,
IsFavoriteOrLiked = true ,
2018-12-28 15:48:26 +00:00
ExcludeItemIds = recentlyPlayedMovies . Select ( i = > i . Id ) . ToArray ( ) ,
2016-06-30 19:01:48 +00:00
EnableGroupByMetadataKey = true ,
ParentId = parentIdGuid ,
2016-10-08 05:57:38 +00:00
Recursive = true ,
2016-10-10 18:18:28 +00:00
DtoOptions = dtoOptions
2014-03-12 02:11:01 +00:00
2017-08-09 19:56:38 +00:00
} ) ;
2014-03-12 02:11:01 +00:00
var mostRecentMovies = recentlyPlayedMovies . Take ( 6 ) . ToList ( ) ;
// Get recently played directors
var recentDirectors = GetDirectors ( mostRecentMovies )
. OrderBy ( i = > Guid . NewGuid ( ) )
. ToList ( ) ;
// Get recently played actors
var recentActors = GetActors ( mostRecentMovies )
. OrderBy ( i = > Guid . NewGuid ( ) )
. ToList ( ) ;
2016-06-01 05:50:00 +00:00
var similarToRecentlyPlayed = GetSimilarTo ( user , recentlyPlayedMovies , itemLimit , dtoOptions , RecommendationType . SimilarToRecentlyPlayed ) . GetEnumerator ( ) ;
var similarToLiked = GetSimilarTo ( user , likedMovies , itemLimit , dtoOptions , RecommendationType . SimilarToLikedItem ) . GetEnumerator ( ) ;
2014-03-12 02:11:01 +00:00
2016-05-31 18:42:32 +00:00
var hasDirectorFromRecentlyPlayed = GetWithDirector ( user , recentDirectors , itemLimit , dtoOptions , RecommendationType . HasDirectorFromRecentlyPlayed ) . GetEnumerator ( ) ;
var hasActorFromRecentlyPlayed = GetWithActor ( user , recentActors , itemLimit , dtoOptions , RecommendationType . HasActorFromRecentlyPlayed ) . GetEnumerator ( ) ;
2014-03-12 02:11:01 +00:00
var categoryTypes = new List < IEnumerator < RecommendationDto > >
{
// Give this extra weight
similarToRecentlyPlayed ,
similarToRecentlyPlayed ,
// Give this extra weight
similarToLiked ,
similarToLiked ,
hasDirectorFromRecentlyPlayed ,
hasActorFromRecentlyPlayed
} ;
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 ) . ThenBy ( i = > Guid . NewGuid ( ) ) ;
}
2016-05-31 18:42:32 +00:00
private IEnumerable < RecommendationDto > GetWithDirector ( User user , IEnumerable < string > names , int itemLimit , DtoOptions dtoOptions , RecommendationType type )
2014-03-12 02:11:01 +00:00
{
2016-08-31 21:07:02 +00:00
var itemTypes = new List < string > { typeof ( Movie ) . Name } ;
if ( _config . Configuration . EnableExternalContentInSuggestions )
{
itemTypes . Add ( typeof ( Trailer ) . Name ) ;
itemTypes . Add ( typeof ( LiveTvProgram ) . Name ) ;
}
2016-05-31 18:42:32 +00:00
foreach ( var name in names )
2014-03-12 02:11:01 +00:00
{
2016-05-31 18:42:32 +00:00
var items = _libraryManager . GetItemList ( new InternalItemsQuery ( user )
{
Person = name ,
// Account for duplicates by imdb id, since the database doesn't support this yet
Limit = itemLimit + 2 ,
PersonTypes = new [ ] { PersonType . Director } ,
2018-12-28 15:48:26 +00:00
IncludeItemTypes = itemTypes . ToArray ( ) ,
2016-06-29 03:17:27 +00:00
IsMovie = true ,
2016-10-08 05:57:38 +00:00
EnableGroupByMetadataKey = true ,
2016-10-10 18:18:28 +00:00
DtoOptions = dtoOptions
2016-05-31 18:42:32 +00:00
2019-02-02 11:27:06 +00:00
} ) . GroupBy ( i = > i . GetProviderId ( MetadataProviders . Imdb ) ? ? Guid . NewGuid ( ) . ToString ( "N" ) )
. Select ( x = > x . First ( ) )
2016-05-31 18:42:32 +00:00
. Take ( itemLimit )
. ToList ( ) ;
2014-03-12 02:11:01 +00:00
if ( items . Count > 0 )
{
2017-08-28 00:33:05 +00:00
var returnItems = _dtoService . GetBaseItemDtos ( items , dtoOptions , user ) ;
2017-08-09 19:56:38 +00:00
2014-03-12 02:11:01 +00:00
yield return new RecommendationDto
{
2016-05-31 18:42:32 +00:00
BaselineItemName = name ,
2018-09-12 17:26:21 +00:00
CategoryId = name . GetMD5 ( ) ,
2014-03-12 02:11:01 +00:00
RecommendationType = type ,
2017-08-19 19:43:35 +00:00
Items = returnItems
2014-03-12 02:11:01 +00:00
} ;
}
}
}
2016-05-31 18:42:32 +00:00
private IEnumerable < RecommendationDto > GetWithActor ( User user , IEnumerable < string > names , int itemLimit , DtoOptions dtoOptions , RecommendationType type )
2014-03-12 02:11:01 +00:00
{
2016-08-31 21:07:02 +00:00
var itemTypes = new List < string > { typeof ( Movie ) . Name } ;
if ( _config . Configuration . EnableExternalContentInSuggestions )
{
itemTypes . Add ( typeof ( Trailer ) . Name ) ;
itemTypes . Add ( typeof ( LiveTvProgram ) . Name ) ;
}
2014-03-12 02:11:01 +00:00
foreach ( var name in names )
{
2016-05-31 18:42:32 +00:00
var items = _libraryManager . GetItemList ( new InternalItemsQuery ( user )
2015-07-08 16:10:34 +00:00
{
2016-05-31 18:42:32 +00:00
Person = name ,
// Account for duplicates by imdb id, since the database doesn't support this yet
Limit = itemLimit + 2 ,
2018-12-28 15:48:26 +00:00
IncludeItemTypes = itemTypes . ToArray ( ) ,
2016-06-29 03:17:27 +00:00
IsMovie = true ,
2016-10-08 05:57:38 +00:00
EnableGroupByMetadataKey = true ,
2016-10-10 18:18:28 +00:00
DtoOptions = dtoOptions
2016-05-31 18:42:32 +00:00
2019-02-02 11:27:06 +00:00
} ) . GroupBy ( i = > i . GetProviderId ( MetadataProviders . Imdb ) ? ? Guid . NewGuid ( ) . ToString ( "N" ) )
. Select ( x = > x . First ( ) )
2016-05-31 18:42:32 +00:00
. Take ( itemLimit )
. ToList ( ) ;
2014-03-12 02:11:01 +00:00
if ( items . Count > 0 )
{
2017-08-28 00:33:05 +00:00
var returnItems = _dtoService . GetBaseItemDtos ( items , dtoOptions , user ) ;
2017-08-09 19:56:38 +00:00
2014-03-12 02:11:01 +00:00
yield return new RecommendationDto
{
BaselineItemName = name ,
2018-09-12 17:26:21 +00:00
CategoryId = name . GetMD5 ( ) ,
2014-03-12 02:11:01 +00:00
RecommendationType = type ,
2017-08-19 19:43:35 +00:00
Items = returnItems
2014-03-12 02:11:01 +00:00
} ;
}
}
}
2016-06-01 05:50:00 +00:00
private IEnumerable < RecommendationDto > GetSimilarTo ( User user , List < BaseItem > baselineItems , int itemLimit , DtoOptions dtoOptions , RecommendationType type )
2014-03-12 02:11:01 +00:00
{
2016-08-31 21:07:02 +00:00
var itemTypes = new List < string > { typeof ( Movie ) . Name } ;
if ( _config . Configuration . EnableExternalContentInSuggestions )
{
itemTypes . Add ( typeof ( Trailer ) . Name ) ;
itemTypes . Add ( typeof ( LiveTvProgram ) . Name ) ;
}
2014-03-12 02:11:01 +00:00
foreach ( var item in baselineItems )
{
2016-06-01 05:50:00 +00:00
var similar = _libraryManager . GetItemList ( new InternalItemsQuery ( user )
{
Limit = itemLimit ,
2018-12-28 15:48:26 +00:00
IncludeItemTypes = itemTypes . ToArray ( ) ,
2016-06-01 05:50:00 +00:00
IsMovie = true ,
2016-06-29 03:17:27 +00:00
SimilarTo = item ,
2016-10-08 05:57:38 +00:00
EnableGroupByMetadataKey = true ,
2016-10-10 18:18:28 +00:00
DtoOptions = dtoOptions
2016-06-01 05:50:00 +00:00
2017-08-09 19:56:38 +00:00
} ) ;
2014-03-12 02:11:01 +00:00
if ( similar . Count > 0 )
{
2017-08-28 00:33:05 +00:00
var returnItems = _dtoService . GetBaseItemDtos ( similar , dtoOptions , user ) ;
2017-08-09 19:56:38 +00:00
2014-03-12 02:11:01 +00:00
yield return new RecommendationDto
{
BaselineItemName = item . Name ,
2018-09-12 17:26:21 +00:00
CategoryId = item . Id ,
2014-03-12 02:11:01 +00:00
RecommendationType = type ,
2017-08-19 19:43:35 +00:00
Items = returnItems
2014-03-12 02:11:01 +00:00
} ;
}
}
}
2017-08-09 19:56:38 +00:00
private IEnumerable < string > GetActors ( List < BaseItem > items )
2014-03-12 02:11:01 +00:00
{
2015-07-08 16:10:34 +00:00
var people = _libraryManager . GetPeople ( new InternalPeopleQuery
{
2019-01-13 19:24:58 +00:00
ExcludePersonTypes = new [ ]
2015-07-08 16:10:34 +00:00
{
PersonType . Director
} ,
MaxListOrder = 3
} ) ;
2017-08-09 21:08:01 +00:00
var itemIds = items . Select ( i = > i . Id ) . ToList ( ) ;
2015-07-08 16:10:34 +00:00
return people
. Where ( i = > itemIds . Contains ( i . ItemId ) )
2014-03-12 02:11:01 +00:00
. Select ( i = > i . Name )
2015-04-09 21:11:57 +00:00
. DistinctNames ( ) ;
2014-03-12 02:11:01 +00:00
}
2017-08-09 19:56:38 +00:00
private IEnumerable < string > GetDirectors ( List < BaseItem > items )
2014-03-12 02:11:01 +00:00
{
2015-07-08 16:10:34 +00:00
var people = _libraryManager . GetPeople ( new InternalPeopleQuery
{
2017-08-24 19:52:19 +00:00
PersonTypes = new string [ ]
2015-07-08 16:10:34 +00:00
{
PersonType . Director
}
} ) ;
2017-08-09 21:08:01 +00:00
var itemIds = items . Select ( i = > i . Id ) . ToList ( ) ;
2015-07-08 16:10:34 +00:00
return people
. Where ( i = > itemIds . Contains ( i . ItemId ) )
2014-03-12 02:11:01 +00:00
. Select ( i = > i . Name )
2015-04-09 21:11:57 +00:00
. DistinctNames ( ) ;
2014-03-12 02:11:01 +00:00
}
2013-05-25 23:52:41 +00:00
}
}