jellyfin/MediaBrowser.Api/DefaultTheme/DefaultThemeService.cs

665 lines
25 KiB
C#
Raw Normal View History

2013-09-18 18:49:06 +00:00
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
2013-09-15 17:34:48 +00:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2013-11-03 20:48:16 +00:00
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
2013-09-11 17:54:59 +00:00
using MediaBrowser.Model.Querying;
2013-12-07 15:52:38 +00:00
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MediaBrowser.Api.DefaultTheme
{
[Route("/MBT/DefaultTheme/Games", "GET")]
public class GetGamesView : IReturn<GamesView>
{
[ApiMember(Name = "UserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid UserId { get; set; }
[ApiMember(Name = "RecentlyPlayedGamesLimit", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int RecentlyPlayedGamesLimit { get; set; }
}
[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; }
2013-09-15 17:34:48 +00:00
[ApiMember(Name = "ComedyGenre", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string ComedyGenre { get; set; }
[ApiMember(Name = "RomanceGenre", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string RomanceGenre { get; set; }
2013-11-03 16:19:33 +00:00
[ApiMember(Name = "TopCommunityRating", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public double TopCommunityRating { get; set; }
2013-11-03 20:48:16 +00:00
[ApiMember(Name = "NextUpEpisodeLimit", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int NextUpEpisodeLimit { get; set; }
[ApiMember(Name = "ResumableEpisodeLimit", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int ResumableEpisodeLimit { get; set; }
[ApiMember(Name = "LatestEpisodeLimit", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int LatestEpisodeLimit { 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; }
2013-09-11 17:54:59 +00:00
2013-09-15 19:33:23 +00:00
[ApiMember(Name = "FamilyGenre", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string FamilyGenre { get; set; }
2013-09-15 17:34:48 +00:00
[ApiMember(Name = "ComedyGenre", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string ComedyGenre { get; set; }
[ApiMember(Name = "RomanceGenre", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string RomanceGenre { get; set; }
2013-11-05 18:20:24 +00:00
[ApiMember(Name = "LatestMoviesLimit", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int LatestMoviesLimit { get; set; }
[ApiMember(Name = "LatestTrailersLimit", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int LatestTrailersLimit { get; set; }
2013-09-15 17:34:48 +00:00
}
2013-10-21 01:03:33 +00:00
[Route("/MBT/DefaultTheme/Favorites", "GET")]
public class GetFavoritesView : IReturn<FavoritesView>
2013-09-15 17:34:48 +00:00
{
[ApiMember(Name = "UserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid UserId { get; set; }
}
2013-09-11 17:54:59 +00:00
public class DefaultThemeService : BaseApiService
{
private readonly IUserManager _userManager;
private readonly IDtoService _dtoService;
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
2013-10-18 16:09:47 +00:00
private readonly IUserDataManager _userDataManager;
2013-09-18 18:49:06 +00:00
private readonly IImageProcessor _imageProcessor;
2013-11-03 20:48:16 +00:00
private readonly IItemRepository _itemRepo;
2013-09-12 01:35:18 +00:00
2013-11-03 20:48:16 +00:00
public DefaultThemeService(IUserManager userManager, IDtoService dtoService, ILogger logger, ILibraryManager libraryManager, IImageProcessor imageProcessor, IUserDataManager userDataManager, IItemRepository itemRepo)
{
_userManager = userManager;
_dtoService = dtoService;
_logger = logger;
_libraryManager = libraryManager;
2013-09-18 18:49:06 +00:00
_imageProcessor = imageProcessor;
2013-10-18 16:09:47 +00:00
_userDataManager = userDataManager;
2013-11-03 20:48:16 +00:00
_itemRepo = itemRepo;
}
2013-10-21 01:03:33 +00:00
public object Get(GetFavoritesView request)
2013-09-15 17:34:48 +00:00
{
var user = _userManager.GetUserById(request.UserId);
2013-10-21 03:01:22 +00:00
var allItems = user.RootFolder.GetRecursiveChildren(user)
2013-09-15 17:34:48 +00:00
.ToList();
2013-10-21 03:01:22 +00:00
var allFavoriteItems = allItems.Where(i => _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).IsFavorite)
2013-10-21 01:03:33 +00:00
.ToList();
2013-10-21 03:01:22 +00:00
var itemsWithImages = allFavoriteItems.Where(i => !string.IsNullOrEmpty(i.PrimaryImagePath))
.ToList();
2014-02-07 20:30:41 +00:00
var itemsWithBackdrops = allFavoriteItems.Where(i => i.GetImages(ImageType.Backdrop).Any())
2013-10-21 01:03:33 +00:00
.ToList();
2013-09-15 17:34:48 +00:00
2013-10-21 01:03:33 +00:00
var view = new FavoritesView();
2013-09-15 17:34:48 +00:00
var fields = new List<ItemFields>();
2013-11-11 02:38:32 +00:00
view.BackdropItems = FilterItemsForBackdropDisplay(itemsWithBackdrops)
.Randomize("backdrop")
2013-10-21 01:03:33 +00:00
.Take(10)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-11-11 02:38:32 +00:00
var spotlightItems = itemsWithBackdrops.Randomize("spotlight")
2013-10-21 01:03:33 +00:00
.Take(10)
.ToList();
2013-09-15 17:34:48 +00:00
2013-10-21 01:03:33 +00:00
view.SpotlightItems = spotlightItems
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-11-03 20:48:16 +00:00
fields.Add(ItemFields.PrimaryImageAspectRatio);
2013-11-11 02:38:32 +00:00
2013-10-21 01:03:33 +00:00
view.Albums = itemsWithImages
.OfType<MusicAlbum>()
2013-11-11 02:38:32 +00:00
.Randomize()
2013-10-21 01:03:33 +00:00
.Take(4)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-09-15 17:34:48 +00:00
2013-10-21 01:03:33 +00:00
view.Books = itemsWithImages
.OfType<Book>()
2013-11-11 02:38:32 +00:00
.Randomize()
2013-10-21 01:03:33 +00:00
.Take(6)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-09-15 17:34:48 +00:00
2013-10-21 01:03:33 +00:00
view.Episodes = itemsWithImages
.OfType<Episode>()
2013-11-11 02:38:32 +00:00
.Randomize()
2013-10-21 01:03:33 +00:00
.Take(6)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-09-15 17:34:48 +00:00
2013-10-21 01:03:33 +00:00
view.Games = itemsWithImages
.OfType<Game>()
2013-11-11 02:38:32 +00:00
.Randomize()
2013-10-21 01:03:33 +00:00
.Take(6)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
view.Movies = itemsWithImages
.OfType<Movie>()
2013-11-11 02:38:32 +00:00
.Randomize()
2013-10-21 01:03:33 +00:00
.Take(6)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
view.Series = itemsWithImages
.OfType<Series>()
2013-11-11 02:38:32 +00:00
.Randomize()
2013-10-21 01:03:33 +00:00
.Take(6)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-10-21 01:03:33 +00:00
view.Songs = itemsWithImages
.OfType<Audio>()
2013-11-11 02:38:32 +00:00
.Randomize()
2013-10-21 01:03:33 +00:00
.Take(4)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
view.MiniSpotlights = itemsWithBackdrops
.Except(spotlightItems)
2013-11-11 02:38:32 +00:00
.Randomize()
2013-10-21 01:03:33 +00:00
.Take(5)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
var artists = allItems.OfType<Audio>()
.SelectMany(i => i.AllArtists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.Randomize()
2013-10-21 03:01:22 +00:00
.Select(i =>
{
try
{
return _libraryManager.GetArtist(i);
}
catch
{
return null;
}
})
.Where(i => i != null && _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).IsFavorite)
.Take(4)
.ToList();
view.Artists = artists
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
return ToOptimizedSerializedResultUsingCache(view);
}
2013-10-21 01:03:33 +00:00
public object Get(GetGamesView request)
{
var user = _userManager.GetUserById(request.UserId);
2013-10-05 14:17:27 +00:00
var items = user.RootFolder.GetRecursiveChildren(user, i => i is Game || i is GameSystem)
.ToList();
2013-10-06 01:04:17 +00:00
var gamesWithImages = items.OfType<Game>().Where(i => !string.IsNullOrEmpty(i.PrimaryImagePath)).ToList();
2013-10-18 16:09:47 +00:00
2014-02-07 20:30:41 +00:00
var itemsWithBackdrops = FilterItemsForBackdropDisplay(items.Where(i => i.GetImages(ImageType.Backdrop).Any())).ToList();
2013-10-21 01:03:33 +00:00
var gamesWithBackdrops = itemsWithBackdrops.OfType<Game>().ToList();
var view = new GamesView();
var fields = new List<ItemFields>();
view.GameSystems = items
.OfType<GameSystem>()
.OrderBy(i => i.SortName)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
var currentUserId = user.Id;
view.RecentlyPlayedGames = gamesWithImages
.OrderByDescending(i => _userDataManager.GetUserData(currentUserId, i.GetUserDataKey()).LastPlayedDate ?? DateTime.MinValue)
.Take(request.RecentlyPlayedGamesLimit)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-10-21 01:03:33 +00:00
view.BackdropItems = gamesWithBackdrops
2013-11-13 16:45:41 +00:00
.OrderBy(i => Guid.NewGuid())
2013-10-21 01:03:33 +00:00
.Take(10)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-10-21 01:03:33 +00:00
view.SpotlightItems = gamesWithBackdrops
2013-11-13 16:45:41 +00:00
.OrderBy(i => Guid.NewGuid())
2013-10-21 01:03:33 +00:00
.Take(10)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-10-06 01:04:17 +00:00
view.MultiPlayerItems = gamesWithImages
.Where(i => i.PlayersSupported.HasValue && i.PlayersSupported.Value > 1)
2013-11-11 02:38:32 +00:00
.Randomize()
2013-10-06 01:04:17 +00:00
.Select(i => GetItemStub(i, ImageType.Primary))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
2013-10-06 01:04:17 +00:00
.ToList();
2013-10-18 16:09:47 +00:00
return ToOptimizedSerializedResultUsingCache(view);
}
public object Get(GetTvView request)
{
2013-11-03 20:48:16 +00:00
var romanceGenres = request.RomanceGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
var comedyGenres = request.ComedyGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
var user = _userManager.GetUserById(request.UserId);
var series = user.RootFolder.GetRecursiveChildren(user)
.OfType<Series>()
.ToList();
2014-02-07 20:30:41 +00:00
var seriesWithBackdrops = series.Where(i => i.GetImages(ImageType.Backdrop).Any()).ToList();
2013-11-04 15:25:16 +00:00
var view = new TvView();
2013-09-12 01:35:18 +00:00
var fields = new List<ItemFields>();
2013-09-11 17:54:59 +00:00
2013-10-21 01:03:33 +00:00
var seriesWithBestBackdrops = FilterItemsForBackdropDisplay(seriesWithBackdrops).ToList();
view.BackdropItems = seriesWithBestBackdrops
2013-11-13 16:45:41 +00:00
.OrderBy(i => Guid.NewGuid())
2013-10-21 01:03:33 +00:00
.Take(10)
.AsParallel()
2013-10-21 01:03:33 +00:00
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
view.ShowsItems = series
2014-02-07 20:30:41 +00:00
.Where(i => i.GetImages(ImageType.Backdrop).Any())
2013-11-11 02:38:32 +00:00
.Randomize("all")
2013-09-12 01:35:18 +00:00
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
2013-09-15 17:34:48 +00:00
view.RomanceItems = seriesWithBackdrops
.Where(i => i.Genres.Any(romanceGenres.ContainsKey))
2013-11-11 02:38:32 +00:00
.Randomize("romance")
2013-09-15 17:34:48 +00:00
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
2013-09-15 17:34:48 +00:00
view.ComedyItems = seriesWithBackdrops
.Where(i => i.Genres.Any(comedyGenres.ContainsKey))
2013-11-11 02:38:32 +00:00
.Randomize("comedy")
2013-09-15 17:34:48 +00:00
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
2013-09-15 17:34:48 +00:00
2013-10-21 01:03:33 +00:00
var spotlightSeries = seriesWithBestBackdrops
.Where(i => i.CommunityRating.HasValue && i.CommunityRating >= 8.5)
.ToList();
if (spotlightSeries.Count < 20)
{
spotlightSeries = seriesWithBestBackdrops;
}
spotlightSeries = spotlightSeries
2013-11-13 16:45:41 +00:00
.OrderBy(i => Guid.NewGuid())
2013-10-21 01:03:33 +00:00
.Take(10)
.ToList();
view.SpotlightItems = spotlightSeries
.AsParallel()
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
var miniSpotlightItems = seriesWithBackdrops
.Except(spotlightSeries.OfType<Series>())
.Where(i => i.CommunityRating.HasValue && i.CommunityRating >= 8)
.ToList();
if (miniSpotlightItems.Count < 15)
{
miniSpotlightItems = seriesWithBackdrops;
}
view.MiniSpotlights = miniSpotlightItems
2013-11-11 02:38:32 +00:00
.Randomize("minispotlight")
2013-10-21 01:03:33 +00:00
.Take(5)
2013-10-18 16:09:47 +00:00
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-10-21 01:03:33 +00:00
2013-11-03 20:48:16 +00:00
var nextUpEpisodes = new TvShowsService(_userManager, _userDataManager, _libraryManager, _itemRepo, _dtoService)
.GetNextUpEpisodes(new GetNextUpEpisodes { UserId = user.Id }, series)
.ToList();
fields.Add(ItemFields.PrimaryImageAspectRatio);
view.NextUpEpisodes = nextUpEpisodes
.Take(request.NextUpEpisodeLimit)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
view.SeriesIdsInProgress = nextUpEpisodes.Select(i => i.Series.Id.ToString("N")).ToList();
2013-11-13 16:45:41 +00:00
// Avoid implicitly captured closure
var currentUser1 = user;
2013-11-03 20:48:16 +00:00
var ownedEpisodes = series
2013-11-13 16:45:41 +00:00
.SelectMany(i => i.GetRecursiveChildren(currentUser1, j => j.LocationType != LocationType.Virtual))
2013-11-03 20:48:16 +00:00
.OfType<Episode>()
.ToList();
// Avoid implicitly captured closure
var currentUser = user;
view.LatestEpisodes = ownedEpisodes
.OrderByDescending(i => i.DateCreated)
.Where(i => !_userDataManager.GetUserData(currentUser.Id, i.GetUserDataKey()).Played)
.Take(request.LatestEpisodeLimit)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
view.ResumableEpisodes = ownedEpisodes
.Where(i => _userDataManager.GetUserData(currentUser.Id, i.GetUserDataKey()).PlaybackPositionTicks > 0)
.OrderByDescending(i => _userDataManager.GetUserData(currentUser.Id, i.GetUserDataKey()).LastPlayedDate ?? DateTime.MinValue)
.Take(request.ResumableEpisodeLimit)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
return ToOptimizedSerializedResultUsingCache(view);
}
2013-10-21 01:03:33 +00:00
public object Get(GetMovieView request)
{
var user = _userManager.GetUserById(request.UserId);
2013-10-05 14:17:27 +00:00
var items = user.RootFolder.GetRecursiveChildren(user, i => i is Movie || i is Trailer || i is BoxSet)
.ToList();
2013-09-11 17:54:59 +00:00
var view = new MoviesView();
var movies = items.OfType<Movie>()
.ToList();
2013-11-05 18:20:24 +00:00
var trailers = items.OfType<Trailer>()
.ToList();
2013-11-11 02:38:32 +00:00
var hdMovies = movies.Where(i => i.IsHD).ToList();
2013-09-11 17:54:59 +00:00
2013-09-15 19:33:23 +00:00
var familyGenres = request.FamilyGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
var familyMovies = movies.Where(i => i.Genres.Any(familyGenres.ContainsKey)).ToList();
2013-09-11 17:54:59 +00:00
view.HDMoviePercentage = 100 * hdMovies.Count;
view.HDMoviePercentage /= movies.Count;
view.FamilyMoviePercentage = 100 * familyMovies.Count;
view.FamilyMoviePercentage /= movies.Count;
2013-09-12 01:35:18 +00:00
var moviesWithBackdrops = movies
2014-02-07 20:30:41 +00:00
.Where(i => i.GetImages(ImageType.Backdrop).Any())
2013-09-12 01:35:18 +00:00
.ToList();
var fields = new List<ItemFields>();
var itemsWithTopBackdrops = FilterItemsForBackdropDisplay(moviesWithBackdrops).ToList();
2013-10-21 01:03:33 +00:00
view.BackdropItems = itemsWithTopBackdrops
2013-11-13 16:45:41 +00:00
.OrderBy(i => Guid.NewGuid())
2013-10-21 01:03:33 +00:00
.Take(10)
.AsParallel()
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-09-12 01:35:18 +00:00
view.MovieItems = moviesWithBackdrops
2013-11-11 02:38:32 +00:00
.Randomize("all")
2013-09-12 01:35:18 +00:00
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
2013-11-05 18:20:24 +00:00
view.TrailerItems = trailers
.Where(i => !string.IsNullOrEmpty(i.PrimaryImagePath))
2013-11-11 02:38:32 +00:00
.Randomize()
.Select(i => GetItemStub(i, ImageType.Primary))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
view.BoxSetItems = items
.OfType<BoxSet>()
2014-02-07 20:30:41 +00:00
.Where(i => i.GetImages(ImageType.Backdrop).Any())
2013-11-11 02:38:32 +00:00
.Randomize()
2013-09-12 01:35:18 +00:00
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
2013-09-12 01:35:18 +00:00
view.ThreeDItems = moviesWithBackdrops
.Where(i => i.Is3D)
2013-11-11 02:38:32 +00:00
.Randomize("3d")
2013-09-12 01:35:18 +00:00
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
2013-09-15 17:34:48 +00:00
var romanceGenres = request.RomanceGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
var comedyGenres = request.ComedyGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
2013-09-14 01:56:03 +00:00
2013-09-15 17:34:48 +00:00
view.RomanceItems = moviesWithBackdrops
2013-09-14 01:56:03 +00:00
.Where(i => i.Genres.Any(romanceGenres.ContainsKey))
2013-11-11 02:38:32 +00:00
.Randomize("romance")
2013-09-14 01:56:03 +00:00
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
2013-09-14 01:56:03 +00:00
2013-09-15 17:34:48 +00:00
view.ComedyItems = moviesWithBackdrops
.Where(i => i.Genres.Any(comedyGenres.ContainsKey))
2013-11-11 02:38:32 +00:00
.Randomize("comedy")
2013-09-15 17:34:48 +00:00
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
2013-09-15 17:34:48 +00:00
2013-09-11 17:54:59 +00:00
view.HDItems = hdMovies
2014-02-07 20:30:41 +00:00
.Where(i => i.GetImages(ImageType.Backdrop).Any())
2013-11-11 02:38:32 +00:00
.Randomize("hd")
2013-09-12 01:35:18 +00:00
.Select(i => GetItemStub(i, ImageType.Backdrop))
2013-09-11 17:54:59 +00:00
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
2013-09-11 17:54:59 +00:00
view.FamilyMovies = familyMovies
2014-02-07 20:30:41 +00:00
.Where(i => i.GetImages(ImageType.Backdrop).Any())
2013-11-11 02:38:32 +00:00
.Randomize("family")
2013-09-12 01:35:18 +00:00
.Select(i => GetItemStub(i, ImageType.Backdrop))
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
var currentUserId = user.Id;
2013-10-21 01:03:33 +00:00
var spotlightItems = itemsWithTopBackdrops
.Where(i => i.CommunityRating.HasValue && i.CommunityRating >= 8)
.Where(i => !_userDataManager.GetUserData(currentUserId, i.GetUserDataKey()).Played)
2013-10-21 01:03:33 +00:00
.ToList();
if (spotlightItems.Count < 20)
{
spotlightItems = itemsWithTopBackdrops;
}
spotlightItems = spotlightItems
2013-11-13 16:45:41 +00:00
.OrderBy(i => Guid.NewGuid())
2013-10-21 01:03:33 +00:00
.Take(10)
.ToList();
view.SpotlightItems = spotlightItems
.AsParallel()
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
var miniSpotlightItems = moviesWithBackdrops
.Except(spotlightItems)
.Where(i => i.CommunityRating.HasValue && i.CommunityRating >= 7.5)
.ToList();
if (miniSpotlightItems.Count < 15)
{
miniSpotlightItems = itemsWithTopBackdrops;
}
miniSpotlightItems = miniSpotlightItems
2013-11-11 02:38:32 +00:00
.Randomize("minispotlight")
2013-10-21 01:03:33 +00:00
.ToList();
// Avoid implicitly captured closure
miniSpotlightItems.InsertRange(0, moviesWithBackdrops
2013-10-21 01:03:33 +00:00
.Where(i => _userDataManager.GetUserData(currentUserId, i.GetUserDataKey()).PlaybackPositionTicks > 0)
.OrderByDescending(i => _userDataManager.GetUserData(currentUserId, i.GetUserDataKey()).LastPlayedDate ?? DateTime.MaxValue)
.Take(3));
view.MiniSpotlights = miniSpotlightItems
.Take(3)
2013-10-18 16:09:47 +00:00
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-09-11 17:54:59 +00:00
2013-11-13 16:45:41 +00:00
// Avoid implicitly captured closure
var currentUserId1 = user.Id;
2013-11-05 18:20:24 +00:00
view.LatestMovies = movies
.OrderByDescending(i => i.DateCreated)
2013-11-13 16:45:41 +00:00
.Where(i => !_userDataManager.GetUserData(currentUserId1, i.GetUserDataKey()).Played)
2013-11-05 18:20:24 +00:00
.Take(request.LatestMoviesLimit)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
view.LatestTrailers = trailers
.OrderByDescending(i => i.DateCreated)
2013-11-13 16:45:41 +00:00
.Where(i => !_userDataManager.GetUserData(currentUserId1, i.GetUserDataKey()).Played)
2013-11-05 18:20:24 +00:00
.Take(request.LatestTrailersLimit)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
return ToOptimizedSerializedResultUsingCache(view);
}
2013-09-15 17:34:48 +00:00
private IEnumerable<BaseItem> FilterItemsForBackdropDisplay(IEnumerable<BaseItem> items)
{
var tuples = items
2014-02-07 20:30:41 +00:00
.Select(i => new Tuple<BaseItem, double>(i, GetResolution(i, ImageType.Backdrop, 0)))
2013-09-15 17:34:48 +00:00
.Where(i => i.Item2 > 0)
.ToList();
var topItems = tuples
.Where(i => i.Item2 >= 1920)
.ToList();
if (topItems.Count >= 10)
{
return topItems.Select(i => i.Item1);
}
return tuples.Select(i => i.Item1);
}
2014-02-07 20:30:41 +00:00
private double GetResolution(BaseItem item, ImageType type, int index)
2013-09-12 01:35:18 +00:00
{
try
{
2014-02-07 20:30:41 +00:00
var info = item.GetImageInfo(type, index);
2013-09-12 01:35:18 +00:00
2014-02-07 20:30:41 +00:00
var size = _imageProcessor.GetImageSize(info.Path, info.DateModified);
2013-09-12 01:35:18 +00:00
return size.Width;
}
catch
{
return 0;
}
}
private ItemStub GetItemStub(BaseItem item, ImageType imageType)
{
var stub = new ItemStub
{
Id = _dtoService.GetDtoId(item),
2013-09-14 01:56:03 +00:00
Name = item.Name,
ImageType = imageType
};
try
{
2014-02-07 20:30:41 +00:00
var tag = _imageProcessor.GetImageCacheTag(item, imageType);
2014-02-07 20:30:41 +00:00
if (tag.HasValue)
{
stub.ImageTag = tag.Value;
}
}
catch (Exception ex)
{
_logger.ErrorException("Error getting image tag for {0}", ex, item.Path);
return null;
}
return stub;
}
}
2013-11-11 02:38:32 +00:00
static class RandomExtension
{
public static IEnumerable<T> Randomize<T>(this IEnumerable<T> sequence, string type = "none")
where T : BaseItem
{
2013-11-19 16:44:11 +00:00
var hour = DateTime.Now.Hour + DateTime.Now.Day + 2;
2013-11-11 02:38:32 +00:00
var typeCode = type.GetHashCode();
return sequence.OrderBy(i =>
{
var val = i.Id.GetHashCode() + i.Genres.Count + i.People.Count + (i.ProductionYear ?? 0) + i.DateCreated.Minute + i.DateModified.Minute + typeCode;
return val % hour;
});
}
public static IEnumerable<string> Randomize(this IEnumerable<string> sequence)
{
var hour = DateTime.Now.Hour + 2;
return sequence.OrderBy(i => i.GetHashCode() % hour);
}
}
}