2014-03-12 03:11:01 +01:00
using MediaBrowser.Common.Extensions ;
using MediaBrowser.Controller.Dto ;
2013-09-04 19:02:19 +02:00
using MediaBrowser.Controller.Entities ;
2013-05-26 01:52:41 +02:00
using MediaBrowser.Controller.Entities.Movies ;
using MediaBrowser.Controller.Library ;
2014-07-02 20:34:08 +02:00
using MediaBrowser.Controller.Net ;
2013-05-26 01:52:41 +02:00
using MediaBrowser.Controller.Persistence ;
2014-03-12 03:11:01 +01:00
using MediaBrowser.Model.Dto ;
using MediaBrowser.Model.Entities ;
using MediaBrowser.Model.Querying ;
2014-09-30 06:47:30 +02:00
using MoreLinq ;
2013-12-07 16:52:38 +01:00
using ServiceStack ;
2014-03-12 03:11:01 +01:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2014-09-30 06:47:30 +02:00
using System.Threading.Tasks ;
2016-05-30 20:16:44 +02:00
using MediaBrowser.Controller.LiveTv ;
2013-05-26 01:52:41 +02:00
2014-03-07 16:53:23 +01:00
namespace MediaBrowser.Api.Movies
2013-05-26 01:52:41 +02:00
{
/// <summary>
/// Class GetSimilarMovies
/// </summary>
2014-03-25 22:13:55 +01:00
[Route("/Movies/{Id}/Similar", "GET", Summary = "Finds movies and trailers similar to a given movie.")]
2013-08-09 17:55:22 +02:00
public class GetSimilarMovies : BaseGetSimilarItemsFromItem
2013-05-26 01:52:41 +02:00
{
}
2015-08-21 04:36:30 +02:00
/// <summary>
/// Class GetSimilarTrailers
/// </summary>
[Route("/Trailers/{Id}/Similar", "GET", Summary = "Finds movies and trailers similar to a given trailer.")]
public class GetSimilarTrailers : BaseGetSimilarItemsFromItem
{
}
2014-03-25 22:13:55 +01:00
[Route("/Movies/Recommendations", "GET", Summary = "Gets movie recommendations")]
2014-03-12 03:11:01 +01:00
public class GetMovieRecommendations : IReturn < RecommendationDto [ ] > , IHasItemFields
{
[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")]
2015-05-30 01:51:33 +02:00
public string UserId { get ; set ; }
2014-03-12 03:11:01 +01:00
2014-05-02 04:54:33 +02: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 06:47:30 +02:00
2014-03-12 03:11:01 +01:00
public GetMovieRecommendations ( )
{
CategoryLimit = 5 ;
ItemLimit = 8 ;
}
public string Fields { get ; set ; }
}
2013-05-26 01:52:41 +02:00
/// <summary>
/// Class MoviesService
/// </summary>
2014-07-02 20:34:08 +02:00
[Authenticated]
2013-05-26 01:52:41 +02:00
public class MoviesService : BaseApiService
{
/// <summary>
/// The _user manager
/// </summary>
private readonly IUserManager _userManager ;
/// <summary>
/// The _user data repository
/// </summary>
2013-10-02 18:08:58 +02:00
private readonly IUserDataManager _userDataRepository ;
2013-05-26 01:52:41 +02:00
/// <summary>
/// The _library manager
/// </summary>
private readonly ILibraryManager _libraryManager ;
2013-06-18 21:16:27 +02:00
private readonly IItemRepository _itemRepo ;
2013-09-04 19:02:19 +02:00
private readonly IDtoService _dtoService ;
2013-05-26 01:52:41 +02:00
/// <summary>
2016-03-19 16:38:05 +01:00
/// Initializes a new instance of the <see cref="MoviesService" /> class.
2013-05-26 01:52:41 +02:00
/// </summary>
/// <param name="userManager">The user manager.</param>
/// <param name="userDataRepository">The user data repository.</param>
/// <param name="libraryManager">The library manager.</param>
2016-03-19 16:38:05 +01:00
/// <param name="itemRepo">The item repo.</param>
/// <param name="dtoService">The dto service.</param>
public MoviesService ( IUserManager userManager , IUserDataManager userDataRepository , ILibraryManager libraryManager , IItemRepository itemRepo , IDtoService dtoService )
2013-05-26 01:52:41 +02:00
{
_userManager = userManager ;
_userDataRepository = userDataRepository ;
_libraryManager = libraryManager ;
2013-06-18 21:16:27 +02:00
_itemRepo = itemRepo ;
2013-09-04 19:02:19 +02:00
_dtoService = dtoService ;
2013-05-26 01:52:41 +02:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2016-06-19 08:18:29 +02:00
public async Task < object > Get ( GetSimilarMovies request )
2013-05-26 01:52:41 +02:00
{
2016-06-19 08:18:29 +02:00
var result = await GetSimilarItemsResult ( request ) . ConfigureAwait ( false ) ;
2013-05-26 01:52:41 +02:00
2014-02-04 05:04:19 +01:00
return ToOptimizedSerializedResultUsingCache ( result ) ;
2013-05-26 01:52:41 +02:00
}
2014-03-12 03:11:01 +01:00
2016-06-19 08:18:29 +02:00
public async Task < object > Get ( GetSimilarTrailers request )
2015-08-21 04:36:30 +02:00
{
2016-06-19 08:18:29 +02:00
var result = await GetSimilarItemsResult ( request ) . ConfigureAwait ( false ) ;
2015-08-21 04:36:30 +02:00
return ToOptimizedSerializedResultUsingCache ( result ) ;
}
2014-09-30 06:47:30 +02:00
public async Task < object > Get ( GetMovieRecommendations request )
2014-03-12 03:11:01 +01:00
{
2015-05-30 01:51:33 +02:00
var user = _userManager . GetUserById ( request . UserId ) ;
2014-03-12 03:11:01 +01:00
2015-01-24 20:03:55 +01:00
var dtoOptions = GetDtoOptions ( request ) ;
2014-12-27 06:08:39 +01:00
dtoOptions . Fields = request . GetItemFields ( ) . ToList ( ) ;
2015-08-21 04:36:30 +02:00
2016-06-01 07:50:00 +02:00
var result = GetRecommendationCategories ( user , request . ParentId , request . CategoryLimit , request . ItemLimit , dtoOptions ) ;
2014-03-12 03:11:01 +01:00
return ToOptimizedResult ( result ) ;
}
2016-06-19 08:18:29 +02:00
private async Task < QueryResult < BaseItemDto > > GetSimilarItemsResult ( BaseGetSimilarItemsFromItem request )
2014-09-30 06:47:30 +02:00
{
2015-05-30 01:51:33 +02:00
var user = ! string . IsNullOrWhiteSpace ( request . UserId ) ? _userManager . GetUserById ( request . UserId ) : null ;
2014-09-30 06:47:30 +02:00
var item = string . IsNullOrEmpty ( request . Id ) ?
2015-05-30 01:51:33 +02:00
( ! string . IsNullOrWhiteSpace ( request . UserId ) ? user . RootFolder :
2014-09-30 06:47:30 +02:00
_libraryManager . RootFolder ) : _libraryManager . GetItemById ( request . Id ) ;
2016-05-30 20:16:44 +02:00
2016-06-01 07:50:00 +02:00
var itemsResult = _libraryManager . GetItemList ( new InternalItemsQuery ( user )
2015-11-10 19:58:05 +01:00
{
2016-06-01 07:50:00 +02:00
Limit = request . Limit ,
2016-05-30 20:16:44 +02:00
IncludeItemTypes = new [ ]
{
2016-06-01 07:50:00 +02:00
typeof ( Movie ) . Name ,
typeof ( Trailer ) . Name ,
typeof ( LiveTvProgram ) . Name
2016-05-30 20:16:44 +02:00
} ,
2016-06-01 07:50:00 +02:00
IsMovie = true ,
2016-06-29 05:17:27 +02:00
SimilarTo = item ,
EnableGroupByMetadataKey = true
2016-06-01 07:50:00 +02:00
} ) . ToList ( ) ;
2015-01-24 20:03:55 +01:00
var dtoOptions = GetDtoOptions ( request ) ;
2015-08-21 04:36:30 +02:00
2016-06-01 07:50:00 +02:00
var result = new QueryResult < BaseItemDto >
2014-09-30 06:47:30 +02:00
{
2016-06-19 08:18:29 +02:00
Items = ( await _dtoService . GetBaseItemDtos ( itemsResult , dtoOptions , user ) . ConfigureAwait ( false ) ) . ToArray ( ) ,
2014-09-30 06:47:30 +02:00
2016-06-01 07:50:00 +02:00
TotalRecordCount = itemsResult . Count
2014-09-30 06:47:30 +02:00
} ;
return result ;
}
2016-06-01 07:50:00 +02:00
private IEnumerable < RecommendationDto > GetRecommendationCategories ( User user , string parentId , int categoryLimit , int itemLimit , DtoOptions dtoOptions )
2014-03-12 03:11:01 +01:00
{
var categories = new List < RecommendationDto > ( ) ;
2016-05-31 20:42:32 +02:00
var parentIds = string . IsNullOrWhiteSpace ( parentId ) ? new string [ ] { } : new [ ] { parentId } ;
var query = new InternalItemsQuery ( user )
{
IncludeItemTypes = new [ ]
2014-03-12 03:11:01 +01:00
{
2016-05-31 20:42:32 +02:00
typeof ( Movie ) . Name ,
//typeof(Trailer).Name,
//typeof(LiveTvProgram).Name
} ,
// IsMovie = true
SortBy = new [ ] { ItemSortBy . DatePlayed , ItemSortBy . Random } ,
SortOrder = SortOrder . Descending ,
Limit = 7
} ;
2014-03-12 03:11:01 +01:00
2016-05-31 20:42:32 +02:00
var recentlyPlayedMovies = _libraryManager . GetItemList ( query , parentIds ) . ToList ( ) ;
2014-03-12 03:11:01 +01:00
2016-05-31 20:42:32 +02:00
var likedMovies = _libraryManager . GetItemList ( new InternalItemsQuery ( user )
{
IncludeItemTypes = new [ ]
{
typeof ( Movie ) . Name ,
typeof ( Trailer ) . Name ,
typeof ( LiveTvProgram ) . Name
} ,
IsMovie = true ,
SortBy = new [ ] { ItemSortBy . Random } ,
SortOrder = SortOrder . Descending ,
Limit = 10 ,
IsFavoriteOrLiked = true ,
2016-06-29 05:17:27 +02:00
ExcludeItemIds = recentlyPlayedMovies . Select ( i = > i . Id . ToString ( "N" ) ) . ToArray ( ) ,
EnableGroupByMetadataKey = true
2014-03-12 03:11:01 +01:00
2016-06-01 07:50:00 +02:00
} , parentIds ) . ToList ( ) ;
2014-03-12 03:11:01 +01: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 07:50:00 +02:00
var similarToRecentlyPlayed = GetSimilarTo ( user , recentlyPlayedMovies , itemLimit , dtoOptions , RecommendationType . SimilarToRecentlyPlayed ) . GetEnumerator ( ) ;
var similarToLiked = GetSimilarTo ( user , likedMovies , itemLimit , dtoOptions , RecommendationType . SimilarToLikedItem ) . GetEnumerator ( ) ;
2014-03-12 03:11:01 +01:00
2016-05-31 20:42:32 +02:00
var hasDirectorFromRecentlyPlayed = GetWithDirector ( user , recentDirectors , itemLimit , dtoOptions , RecommendationType . HasDirectorFromRecentlyPlayed ) . GetEnumerator ( ) ;
var hasActorFromRecentlyPlayed = GetWithActor ( user , recentActors , itemLimit , dtoOptions , RecommendationType . HasActorFromRecentlyPlayed ) . GetEnumerator ( ) ;
2014-03-12 03:11:01 +01: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 20:42:32 +02:00
private IEnumerable < RecommendationDto > GetWithDirector ( User user , IEnumerable < string > names , int itemLimit , DtoOptions dtoOptions , RecommendationType type )
2014-03-12 03:11:01 +01:00
{
2016-05-31 20:42:32 +02:00
foreach ( var name in names )
2014-03-12 03:11:01 +01:00
{
2016-05-31 20:42:32 +02: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 } ,
IncludeItemTypes = new [ ]
{
typeof ( Movie ) . Name ,
typeof ( Trailer ) . Name ,
typeof ( LiveTvProgram ) . Name
} ,
2016-06-29 05:17:27 +02:00
IsMovie = true ,
EnableGroupByMetadataKey = true
2016-05-31 20:42:32 +02:00
} ) . DistinctBy ( i = > i . GetProviderId ( MetadataProviders . Imdb ) ? ? Guid . NewGuid ( ) . ToString ( "N" ) )
. Take ( itemLimit )
. ToList ( ) ;
2014-03-12 03:11:01 +01:00
if ( items . Count > 0 )
{
yield return new RecommendationDto
{
2016-05-31 20:42:32 +02:00
BaselineItemName = name ,
CategoryId = name . GetMD5 ( ) . ToString ( "N" ) ,
2014-03-12 03:11:01 +01:00
RecommendationType = type ,
2016-06-19 08:18:29 +02:00
Items = _dtoService . GetBaseItemDtos ( items , dtoOptions , user ) . Result . ToArray ( )
2014-03-12 03:11:01 +01:00
} ;
}
}
}
2016-05-31 20:42:32 +02:00
private IEnumerable < RecommendationDto > GetWithActor ( User user , IEnumerable < string > names , int itemLimit , DtoOptions dtoOptions , RecommendationType type )
2014-03-12 03:11:01 +01:00
{
foreach ( var name in names )
{
2016-05-31 20:42:32 +02:00
var items = _libraryManager . GetItemList ( new InternalItemsQuery ( user )
2015-07-08 18:10:34 +02:00
{
2016-05-31 20:42:32 +02:00
Person = name ,
// Account for duplicates by imdb id, since the database doesn't support this yet
Limit = itemLimit + 2 ,
IncludeItemTypes = new [ ]
{
typeof ( Movie ) . Name ,
typeof ( Trailer ) . Name ,
typeof ( LiveTvProgram ) . Name
} ,
2016-06-29 05:17:27 +02:00
IsMovie = true ,
EnableGroupByMetadataKey = true
2016-05-31 20:42:32 +02:00
} ) . DistinctBy ( i = > i . GetProviderId ( MetadataProviders . Imdb ) ? ? Guid . NewGuid ( ) . ToString ( "N" ) )
. Take ( itemLimit )
. ToList ( ) ;
2014-03-12 03:11:01 +01:00
if ( items . Count > 0 )
{
yield return new RecommendationDto
{
BaselineItemName = name ,
CategoryId = name . GetMD5 ( ) . ToString ( "N" ) ,
RecommendationType = type ,
2016-06-19 08:18:29 +02:00
Items = _dtoService . GetBaseItemDtos ( items , dtoOptions , user ) . Result . ToArray ( )
2014-03-12 03:11:01 +01:00
} ;
}
}
}
2016-06-01 07:50:00 +02:00
private IEnumerable < RecommendationDto > GetSimilarTo ( User user , List < BaseItem > baselineItems , int itemLimit , DtoOptions dtoOptions , RecommendationType type )
2014-03-12 03:11:01 +01:00
{
foreach ( var item in baselineItems )
{
2016-06-01 07:50:00 +02:00
var similar = _libraryManager . GetItemList ( new InternalItemsQuery ( user )
{
Limit = itemLimit ,
IncludeItemTypes = new [ ]
{
typeof ( Movie ) . Name ,
typeof ( Trailer ) . Name ,
typeof ( LiveTvProgram ) . Name
} ,
IsMovie = true ,
2016-06-29 05:17:27 +02:00
SimilarTo = item ,
EnableGroupByMetadataKey = true
2016-06-01 07:50:00 +02:00
} ) . ToList ( ) ;
2014-03-12 03:11:01 +01:00
if ( similar . Count > 0 )
{
yield return new RecommendationDto
{
BaselineItemName = item . Name ,
CategoryId = item . Id . ToString ( "N" ) ,
RecommendationType = type ,
2016-06-19 08:18:29 +02:00
Items = _dtoService . GetBaseItemDtos ( similar , dtoOptions , user ) . Result . ToArray ( )
2014-03-12 03:11:01 +01:00
} ;
}
}
}
private IEnumerable < string > GetActors ( IEnumerable < BaseItem > items )
{
2015-07-08 18:10:34 +02:00
var people = _libraryManager . GetPeople ( new InternalPeopleQuery
{
ExcludePersonTypes = new List < string >
{
PersonType . Director
} ,
MaxListOrder = 3
} ) ;
var itemIds = items . Select ( i = > i . Id ) . ToList ( ) ;
return people
. Where ( i = > itemIds . Contains ( i . ItemId ) )
2014-03-12 03:11:01 +01:00
. Select ( i = > i . Name )
2015-04-09 23:11:57 +02:00
. DistinctNames ( ) ;
2014-03-12 03:11:01 +01:00
}
private IEnumerable < string > GetDirectors ( IEnumerable < BaseItem > items )
{
2015-07-08 18:10:34 +02:00
var people = _libraryManager . GetPeople ( new InternalPeopleQuery
{
PersonTypes = new List < string >
{
PersonType . Director
}
} ) ;
var itemIds = items . Select ( i = > i . Id ) . ToList ( ) ;
return people
. Where ( i = > itemIds . Contains ( i . ItemId ) )
2014-03-12 03:11:01 +01:00
. Select ( i = > i . Name )
2015-04-09 23:11:57 +02:00
. DistinctNames ( ) ;
2014-03-12 03:11:01 +01:00
}
2013-05-26 01:52:41 +02:00
}
}