created custom endpoints for the mbt default theme

pull/702/head
Luke Pulverenti 11 years ago
parent a790ec237b
commit d078edfb96

@ -0,0 +1,243 @@
using MediaBrowser.Controller;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using ServiceStack.ServiceHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Api.DefaultTheme
{
[Route("/MBT/DefaultTheme/TV", "GET")]
public class GetTvView : IReturn<TvView>
{
[ApiMember(Name = "UserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid UserId { get; set; }
}
[Route("/MBT/DefaultTheme/Movies", "GET")]
public class GetMovieView : IReturn<MoviesView>
{
[ApiMember(Name = "UserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid UserId { get; set; }
}
public class DefaultThemeService : BaseApiService
{
private readonly IUserManager _userManager;
private readonly IDtoService _dtoService;
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
public DefaultThemeService(IUserManager userManager, IDtoService dtoService, ILogger logger, ILibraryManager libraryManager)
{
_userManager = userManager;
_dtoService = dtoService;
_logger = logger;
_libraryManager = libraryManager;
}
public object Get(GetTvView request)
{
var result = GetTvView(request).Result;
return ToOptimizedResult(result);
}
private async Task<TvView> GetTvView(GetTvView request)
{
var user = _userManager.GetUserById(request.UserId);
var series = user.RootFolder.GetRecursiveChildren(user)
.OfType<Series>()
.ToList();
var seriesWithBackdrops = series.Where(i => i.BackdropImagePaths.Count > 0).ToList();
var view = new TvView();
view.BackdropItems = seriesWithBackdrops
.OrderBy(i => Guid.NewGuid())
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
.Take(30)
.ToArray();
view.SpotlightItems = seriesWithBackdrops
.OrderBy(i => Guid.NewGuid())
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
.Take(30)
.ToArray();
view.ShowsItems = series
.Where(i => !string.IsNullOrEmpty(i.PrimaryImagePath))
.OrderBy(i => Guid.NewGuid())
.Select(i => GetItemStub(i, ImageType.Primary))
.Where(i => i != null)
.Take(3)
.ToArray();
view.ActorItems = await GetActors(series).ConfigureAwait(false);
return view;
}
public object Get(GetMovieView request)
{
var result = GetMovieView(request).Result;
return ToOptimizedResult(result);
}
private async Task<MoviesView> GetMovieView(GetMovieView request)
{
var user = _userManager.GetUserById(request.UserId);
var items = user.RootFolder.GetRecursiveChildren(user)
.Where(i => i is Movie || i is Trailer || i is BoxSet)
.ToList();
// Exclude trailers from backdrops because they're not always 1080p
var itemsWithBackdrops = items.Where(i => i.BackdropImagePaths.Count > 0 && !(i is Trailer))
.ToList();
var movies = items.OfType<Movie>().ToList();
var moviesWithImages = movies
.Where(i => !string.IsNullOrEmpty(i.PrimaryImagePath))
.ToList();
var view = new MoviesView();
view.BackdropItems = itemsWithBackdrops
.OrderBy(i => Guid.NewGuid())
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
.Take(30)
.ToArray();
view.SpotlightItems = itemsWithBackdrops
.OrderBy(i => Guid.NewGuid())
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
.Take(30)
.ToArray();
view.MovieItems = moviesWithImages
.OrderBy(i => Guid.NewGuid())
.Select(i => GetItemStub(i, ImageType.Primary))
.Where(i => i != null)
.Take(3)
.ToArray();
view.TrailerItems = items
.OfType<Trailer>()
.Where(i => !string.IsNullOrEmpty(i.PrimaryImagePath))
.OrderBy(i => Guid.NewGuid())
.Select(i => GetItemStub(i, ImageType.Primary))
.Where(i => i != null)
.Take(3)
.ToArray();
view.BoxSetItems = items
.OfType<BoxSet>()
.Where(i => !string.IsNullOrEmpty(i.PrimaryImagePath))
.OrderBy(i => Guid.NewGuid())
.Select(i => GetItemStub(i, ImageType.Primary))
.Where(i => i != null)
.Take(3)
.ToArray();
view.PeopleItems = await GetActors(items).ConfigureAwait(false);
view.ThreeDItems = moviesWithImages
.Where(i => i.Is3D)
.OrderBy(i => Guid.NewGuid())
.Select(i => GetItemStub(i, ImageType.Primary))
.Where(i => i != null)
.Take(3)
.ToArray();
view.HDItems = moviesWithImages
.Where(i => i.IsHd)
.OrderBy(i => Guid.NewGuid())
.Select(i => GetItemStub(i, ImageType.Primary))
.Where(i => i != null)
.Take(3)
.ToArray();
return view;
}
private async Task<ItemStub[]> GetActors(IEnumerable<BaseItem> mediaItems)
{
var actorStubs = new List<ItemStub>();
var actors = mediaItems.SelectMany(i => i.People)
.Select(i => i.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
foreach (var actor in actors)
{
if (actorStubs.Count >= 3)
{
break;
}
try
{
var person = await _libraryManager.GetPerson(actor).ConfigureAwait(false);
if (!string.IsNullOrEmpty(person.PrimaryImagePath))
{
var stub = GetItemStub(person, ImageType.Primary);
if (stub != null)
{
actorStubs.Add(stub);
}
}
}
catch (Exception ex)
{
_logger.ErrorException("Error getting person {0}", ex, actor);
}
}
return actorStubs.ToArray();
}
private ItemStub GetItemStub(BaseItem item, ImageType imageType)
{
var stub = new ItemStub
{
Id = _dtoService.GetDtoId(item),
Name = item.Name
};
var imageManager = Kernel.Instance.ImageManager;
try
{
var imagePath = imageManager.GetImagePath(item, imageType, 0);
stub.ImageTag = imageManager.GetImageCacheTag(item, imageType, imagePath);
}
catch (Exception ex)
{
_logger.ErrorException("Error getting image tag for {0}", ex, item.Path);
return null;
}
return stub;
}
}
}

@ -0,0 +1,11 @@
using System;
namespace MediaBrowser.Api.DefaultTheme
{
public class ItemStub
{
public string Name { get; set; }
public string Id { get; set; }
public Guid ImageTag { get; set; }
}
}

@ -0,0 +1,16 @@

namespace MediaBrowser.Api.DefaultTheme
{
public class MoviesView
{
public ItemStub[] SpotlightItems { get; set; }
public ItemStub[] BackdropItems { get; set; }
public ItemStub[] MovieItems { get; set; }
public ItemStub[] PeopleItems { get; set; }
public ItemStub[] BoxSetItems { get; set; }
public ItemStub[] TrailerItems { get; set; }
public ItemStub[] HDItems { get; set; }
public ItemStub[] ThreeDItems { get; set; }
}
}

@ -0,0 +1,11 @@

namespace MediaBrowser.Api.DefaultTheme
{
public class TvView
{
public ItemStub[] SpotlightItems { get; set; }
public ItemStub[] BackdropItems { get; set; }
public ItemStub[] ShowsItems { get; set; }
public ItemStub[] ActorItems { get; set; }
}
}

@ -66,6 +66,10 @@
</Compile> </Compile>
<Compile Include="AlbumsService.cs" /> <Compile Include="AlbumsService.cs" />
<Compile Include="BaseApiService.cs" /> <Compile Include="BaseApiService.cs" />
<Compile Include="DefaultTheme\DefaultThemeService.cs" />
<Compile Include="DefaultTheme\ItemStub.cs" />
<Compile Include="DefaultTheme\MoviesView.cs" />
<Compile Include="DefaultTheme\TvView.cs" />
<Compile Include="DisplayPreferencesService.cs" /> <Compile Include="DisplayPreferencesService.cs" />
<Compile Include="EnvironmentService.cs" /> <Compile Include="EnvironmentService.cs" />
<Compile Include="GamesService.cs" /> <Compile Include="GamesService.cs" />

Loading…
Cancel
Save