jellyfin/MediaBrowser.Api/DefaultTheme/DefaultThemeService.cs

763 lines
28 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;
using ServiceStack.ServiceHost;
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; }
}
[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-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();
var itemsWithBackdrops = allFavoriteItems.Where(i => i.BackdropImagePaths.Count > 0)
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-10-21 01:03:33 +00:00
view.BackdropItems = FilterItemsForBackdropDisplay(itemsWithBackdrops.OrderBy(i => Guid.NewGuid()))
.Take(10)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
var spotlightItems = itemsWithBackdrops.OrderBy(i => Guid.NewGuid())
.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-10-21 01:03:33 +00:00
view.Albums = itemsWithImages
.OfType<MusicAlbum>()
2013-09-15 17:34:48 +00:00
.OrderBy(i => Guid.NewGuid())
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>()
.OrderBy(i => Guid.NewGuid())
.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>()
.OrderBy(i => Guid.NewGuid())
.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>()
.OrderBy(i => Guid.NewGuid())
.Take(6)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
view.Movies = itemsWithImages
.OfType<Movie>()
.OrderBy(i => Guid.NewGuid())
.Take(6)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
view.Series = itemsWithImages
.OfType<Series>()
.OrderBy(i => Guid.NewGuid())
.Take(6)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-10-21 01:03:33 +00:00
view.Songs = itemsWithImages
.OfType<Audio>()
.OrderBy(i => Guid.NewGuid())
.Take(4)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
view.MiniSpotlights = itemsWithBackdrops
.Except(spotlightItems)
.OrderBy(i => Guid.NewGuid())
.Take(5)
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-10-21 03:01:22 +00:00
var artists = allItems.OfType<Audio>()
.SelectMany(i =>
{
var list = new List<string>();
if (!string.IsNullOrEmpty(i.AlbumArtist))
{
list.Add(i.AlbumArtist);
}
list.AddRange(i.Artists);
return list;
})
.Distinct(StringComparer.OrdinalIgnoreCase)
.OrderBy(i => Guid.NewGuid())
.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();
2013-10-21 01:03:33 +00:00
return ToOptimizedResult(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
var itemsWithBackdrops = FilterItemsForBackdropDisplay(items.Where(i => i.BackdropImagePaths.Count > 0)).ToList();
2013-10-21 01:03:33 +00:00
var gamesWithBackdrops = itemsWithBackdrops.OfType<Game>().ToList();
var view = new GamesView();
var fields = new List<ItemFields>();
2013-10-21 01:03:33 +00:00
view.BackdropItems = gamesWithBackdrops
.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
.OrderBy(i => Guid.NewGuid())
.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)
.OrderBy(i => Guid.NewGuid())
.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
2013-10-21 01:03:33 +00:00
view.MiniSpotlights = gamesWithBackdrops
2013-10-18 16:09:47 +00:00
.OrderBy(i => Guid.NewGuid())
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
return ToOptimizedResult(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();
var seriesWithBackdrops = series.Where(i => i.BackdropImagePaths.Count > 0).ToList();
2013-11-03 16:19:33 +00:00
var view = new TvView
{
SeriesCount = series.Count,
FavoriteSeriesCount = series.Count(i => _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).IsFavorite),
2013-11-03 20:48:16 +00:00
TopCommunityRatedSeriesCount = series.Count(i => i.CommunityRating.HasValue && i.CommunityRating.Value >= request.TopCommunityRating),
ComedySeriesCount = series.Count(i => i.Genres.Any(comedyGenres.ContainsKey)),
RomanticSeriesCount = series.Count(i => i.Genres.Any(romanceGenres.ContainsKey))
2013-11-03 16:19:33 +00:00
};
2013-11-03 20:48:16 +00:00
SetFavoriteGenres(view, series, user);
SetFavoriteStudios(view, series, user);
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
.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
2013-09-12 01:35:18 +00:00
.Where(i => i.BackdropImagePaths.Count > 0)
.OrderBy(i => Guid.NewGuid())
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))
.OrderBy(i => Guid.NewGuid())
.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))
.OrderBy(i => Guid.NewGuid())
.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-18 16:09:47 +00:00
view.ActorItems = GetActors(series, user.Id);
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
.OrderBy(i => Guid.NewGuid())
.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-10-18 16:09:47 +00:00
.OrderBy(i => Guid.NewGuid())
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();
var ownedEpisodes = series
.SelectMany(i => i.GetRecursiveChildren(user, j => j.LocationType != LocationType.Virtual))
.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();
2013-10-21 01:03:33 +00:00
return ToOptimizedResult(view);
}
2013-11-03 20:48:16 +00:00
private void SetFavoriteGenres(TvView view, IEnumerable<BaseItem> inputItems, User user)
{
var all = inputItems.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase);
view.FavoriteGenres = all.Select(i =>
{
try
{
var itemByName = _libraryManager.GetGenre(i);
var counts = itemByName.GetItemByNameCounts(user);
var count = counts == null ? 0 : counts.SeriesCount;
if (count > 0 && _userDataManager.GetUserData(user.Id, itemByName.GetUserDataKey()).IsFavorite)
{
return new ItemByNameInfo
{
Name = itemByName.Name,
ItemCount = count
};
}
}
catch (Exception ex)
{
_logger.ErrorException("Error getting genre {0}", ex, i);
}
return null;
}).Where(i => i != null).ToList();
}
private void SetFavoriteStudios(TvView view, IEnumerable<BaseItem> inputItems, User user)
{
var all = inputItems.SelectMany(i => i.Studios)
.Distinct(StringComparer.OrdinalIgnoreCase);
view.FavoriteStudios = all.Select(i =>
{
try
{
var itemByName = _libraryManager.GetStudio(i);
var counts = itemByName.GetItemByNameCounts(user);
var count = counts == null ? 0 : counts.SeriesCount;
if (count > 0 && _userDataManager.GetUserData(user.Id, itemByName.GetUserDataKey()).IsFavorite)
{
return new ItemByNameInfo
{
Name = itemByName.Name,
ItemCount = count
};
}
}
catch (Exception ex)
{
_logger.ErrorException("Error getting studio {0}", ex, i);
}
return null;
}).Where(i => i != null).ToList();
}
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();
// Exclude trailers from backdrops because they're not always 1080p
2013-10-21 01:03:33 +00:00
var itemsWithBackdrops = items.Where(i => i.BackdropImagePaths.Count > 0)
.ToList();
2013-09-11 17:54:59 +00:00
var view = new MoviesView();
var movies = items.OfType<Movie>()
.ToList();
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
.Where(i => i.BackdropImagePaths.Count > 0)
.ToList();
var fields = new List<ItemFields>();
2013-10-21 01:03:33 +00:00
var itemsWithTopBackdrops = FilterItemsForBackdropDisplay(itemsWithBackdrops).ToList();
2013-10-21 01:03:33 +00:00
view.BackdropItems = itemsWithTopBackdrops
.OrderBy(i => Guid.NewGuid())
.Take(10)
.AsParallel()
.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
.ToList();
2013-09-12 01:35:18 +00:00
view.MovieItems = moviesWithBackdrops
.OrderBy(i => Guid.NewGuid())
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();
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)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
view.BoxSetItems = items
.OfType<BoxSet>()
2013-09-12 01:35:18 +00:00
.Where(i => i.BackdropImagePaths.Count > 0)
.OrderBy(i => Guid.NewGuid())
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)
.OrderBy(i => Guid.NewGuid())
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))
.OrderBy(i => Guid.NewGuid())
.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))
.OrderBy(i => Guid.NewGuid())
.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
2013-09-12 01:35:18 +00:00
.Where(i => i.BackdropImagePaths.Count > 0)
2013-09-11 17:54:59 +00:00
.OrderBy(i => Guid.NewGuid())
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
2013-09-12 01:35:18 +00:00
.Where(i => i.BackdropImagePaths.Count > 0)
.OrderBy(i => Guid.NewGuid())
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-10-18 16:09:47 +00:00
view.PeopleItems = GetActors(items, user.Id);
2013-10-21 01:03:33 +00:00
var spotlightItems = itemsWithTopBackdrops
.Where(i => i.CommunityRating.HasValue && i.CommunityRating >= 8)
.ToList();
if (spotlightItems.Count < 20)
{
spotlightItems = itemsWithTopBackdrops;
}
spotlightItems = spotlightItems
.OrderBy(i => Guid.NewGuid())
.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-10-18 16:09:47 +00:00
.OrderBy(i => Guid.NewGuid())
2013-10-21 01:03:33 +00:00
.ToList();
// Avoid implicitly captured closure
var currentUserId = user.Id;
miniSpotlightItems.InsertRange(2, moviesWithBackdrops
.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(5)
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-10-21 01:03:33 +00:00
return ToOptimizedResult(view);
}
2013-09-15 17:34:48 +00:00
private IEnumerable<BaseItem> FilterItemsForBackdropDisplay(IEnumerable<BaseItem> items)
{
var tuples = items
.Select(i => new Tuple<BaseItem, double>(i, GetResolution(i, i.BackdropImagePaths[0])))
.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);
}
2013-09-12 01:35:18 +00:00
private double GetResolution(BaseItem item, string path)
{
try
{
2013-09-18 18:49:06 +00:00
var date = item.GetImageDateModified(path);
2013-09-12 01:35:18 +00:00
2013-09-18 18:49:06 +00:00
var size = _imageProcessor.GetImageSize(path, date);
2013-09-12 01:35:18 +00:00
return size.Width;
}
catch
{
return 0;
}
}
2013-10-18 16:09:47 +00:00
private List<ItemStub> GetActors(IEnumerable<BaseItem> mediaItems, Guid userId)
{
var actors = mediaItems.SelectMany(i => i.People)
.Select(i => i.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
2013-09-11 17:54:59 +00:00
.OrderBy(i => Guid.NewGuid())
.ToList();
2013-10-18 16:09:47 +00:00
var result = actors.Select(actor =>
{
try
{
var person = _libraryManager.GetPerson(actor);
if (!string.IsNullOrEmpty(person.PrimaryImagePath))
{
2013-10-18 16:09:47 +00:00
var userdata = _userDataManager.GetUserData(userId, person.GetUserDataKey());
if (userdata.IsFavorite || (userdata.Likes ?? false))
{
return GetItemStub(person, ImageType.Primary);
}
}
}
catch (Exception ex)
{
_logger.ErrorException("Error getting person {0}", ex, actor);
}
return null;
})
.Where(i => i != null)
2013-10-18 16:09:47 +00:00
.Take(1)
.ToList();
2013-10-18 16:09:47 +00:00
if (result.Count == 0)
{
result = actors.Select(actor =>
{
try
{
var person = _libraryManager.GetPerson(actor);
if (!string.IsNullOrEmpty(person.PrimaryImagePath))
{
return GetItemStub(person, ImageType.Primary);
}
}
catch (Exception ex)
{
_logger.ErrorException("Error getting person {0}", ex, actor);
}
return null;
})
.Where(i => i != null)
.Take(1)
.ToList();
}
return result;
}
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
{
2013-09-18 18:49:06 +00:00
var imagePath = item.GetImagePath(imageType, 0);
2013-09-18 18:49:06 +00:00
stub.ImageTag = _imageProcessor.GetImageCacheTag(item, imageType, imagePath);
}
catch (Exception ex)
{
_logger.ErrorException("Error getting image tag for {0}", ex, item.Path);
return null;
}
return stub;
}
}
}