2014-10-20 22:23:40 +02:00
|
|
|
|
using MediaBrowser.Api.UserLibrary;
|
|
|
|
|
using MediaBrowser.Controller.Channels;
|
|
|
|
|
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-10-20 22:23:40 +02:00
|
|
|
|
using MediaBrowser.Model.Channels;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Querying;
|
2013-12-07 16:52:38 +01:00
|
|
|
|
using ServiceStack;
|
2014-10-20 22:23:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
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 GetSimilarTrailers
|
|
|
|
|
/// </summary>
|
2014-03-25 22:13:55 +01:00
|
|
|
|
[Route("/Trailers/{Id}/Similar", "GET", Summary = "Finds movies and trailers similar to a given trailer.")]
|
2013-08-09 17:55:22 +02:00
|
|
|
|
public class GetSimilarTrailers : BaseGetSimilarItemsFromItem
|
2013-05-26 01:52:41 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 22:23:40 +02:00
|
|
|
|
[Route("/Trailers", "GET", Summary = "Finds movies and trailers similar to a given trailer.")]
|
|
|
|
|
public class Getrailers : BaseItemsRequest, IReturn<ItemsResult>
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the user id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The user id.</value>
|
|
|
|
|
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public Guid? UserId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-26 01:52:41 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class TrailersService
|
|
|
|
|
/// </summary>
|
2014-07-02 20:34:08 +02:00
|
|
|
|
[Authenticated]
|
2013-05-26 01:52:41 +02:00
|
|
|
|
public class TrailersService : 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;
|
2014-10-20 22:23:40 +02:00
|
|
|
|
private readonly IChannelManager _channelManager;
|
2013-09-04 19:02:19 +02:00
|
|
|
|
|
2013-05-26 01:52:41 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="TrailersService"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userManager">The user manager.</param>
|
|
|
|
|
/// <param name="userDataRepository">The user data repository.</param>
|
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
2014-10-20 22:23:40 +02:00
|
|
|
|
public TrailersService(IUserManager userManager, IUserDataManager userDataRepository, ILibraryManager libraryManager, IItemRepository itemRepo, IDtoService dtoService, IChannelManager channelManager)
|
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;
|
2014-10-20 22:23:40 +02:00
|
|
|
|
_channelManager = channelManager;
|
2013-05-26 01:52:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object Get(GetSimilarTrailers request)
|
|
|
|
|
{
|
2013-08-08 22:18:05 +02:00
|
|
|
|
var result = SimilarItemsHelper.GetSimilarItemsResult(_userManager,
|
2013-06-18 21:16:27 +02:00
|
|
|
|
_itemRepo,
|
2013-05-26 01:52:41 +02:00
|
|
|
|
_libraryManager,
|
|
|
|
|
_userDataRepository,
|
2013-09-04 19:02:19 +02:00
|
|
|
|
_dtoService,
|
2013-05-26 01:52:41 +02:00
|
|
|
|
Logger,
|
2014-03-19 05:59:45 +01:00
|
|
|
|
|
|
|
|
|
// Strip out secondary versions
|
2014-12-03 04:13:03 +01:00
|
|
|
|
request, item => (item is Movie) && !((Video)item).PrimaryVersionId.HasValue,
|
2014-03-19 05:59:45 +01:00
|
|
|
|
|
2013-05-26 01:52:41 +02:00
|
|
|
|
SimilarItemsHelper.GetSimiliarityScore);
|
|
|
|
|
|
2014-02-04 05:04:19 +01:00
|
|
|
|
return ToOptimizedSerializedResultUsingCache(result);
|
2013-05-26 01:52:41 +02:00
|
|
|
|
}
|
2014-10-20 22:23:40 +02:00
|
|
|
|
|
|
|
|
|
public async Task<object> Get(Getrailers request)
|
|
|
|
|
{
|
|
|
|
|
var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null;
|
|
|
|
|
var result = await GetAllTrailers(user).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
IEnumerable<BaseItem> items = result.Items;
|
|
|
|
|
|
|
|
|
|
// Apply filters
|
|
|
|
|
// Run them starting with the ones that are likely to reduce the list the most
|
|
|
|
|
foreach (var filter in request.GetFilters().OrderByDescending(f => (int)f))
|
|
|
|
|
{
|
|
|
|
|
items = ItemsService.ApplyFilter(items, filter, user, _userDataRepository);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items = _libraryManager.Sort(items, user, request.GetOrderBy(), request.SortOrder ?? SortOrder.Ascending);
|
|
|
|
|
|
|
|
|
|
var itemsArray = items.ToList();
|
|
|
|
|
|
|
|
|
|
var pagedItems = ApplyPaging(request, itemsArray);
|
|
|
|
|
|
|
|
|
|
var fields = request.GetItemFields().ToList();
|
|
|
|
|
|
|
|
|
|
var returnItems = pagedItems.Select(i => _dtoService.GetBaseItemDto(i, fields, user)).ToArray();
|
|
|
|
|
|
|
|
|
|
return new ItemsResult
|
|
|
|
|
{
|
|
|
|
|
TotalRecordCount = itemsArray.Count,
|
|
|
|
|
Items = returnItems
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<BaseItem> ApplyPaging(Getrailers request, IEnumerable<BaseItem> items)
|
|
|
|
|
{
|
|
|
|
|
// Start at
|
|
|
|
|
if (request.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
items = items.Skip(request.StartIndex.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return limit
|
|
|
|
|
if (request.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
items = items.Take(request.Limit.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<QueryResult<BaseItem>> GetAllTrailers(User user)
|
|
|
|
|
{
|
|
|
|
|
var trailerResult = await _channelManager.GetAllMediaInternal(new AllChannelMediaQuery
|
|
|
|
|
{
|
|
|
|
|
ContentTypes = new[] { ChannelMediaContentType.MovieExtra },
|
|
|
|
|
ExtraTypes = new[] { ExtraType.Trailer },
|
|
|
|
|
UserId = user.Id.ToString("N")
|
|
|
|
|
|
|
|
|
|
}, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new QueryResult<BaseItem>
|
|
|
|
|
{
|
|
|
|
|
Items = trailerResult.Items,
|
|
|
|
|
TotalRecordCount = trailerResult.TotalRecordCount
|
|
|
|
|
};
|
|
|
|
|
}
|
2013-05-26 01:52:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|