public object Get(GetMovieView request)
        {
            var user = _userManager.GetUserById(request.UserId);

            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
            var itemsWithBackdrops = items.Where(i => i.BackdropImagePaths.Count > 0)
                                     .ToList();

            var view = new MoviesView();

            var movies = items.OfType <Movie>()
                         .ToList();

            SetFavoriteGenres(view, movies, user);
            SetFavoriteStudios(view, movies, user);

            var trailers = items.OfType <Trailer>()
                           .ToList();

            var hdMovies = movies.Where(i => i.IsHD).ToList();

            var familyGenres = request.FamilyGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);

            var familyMovies = movies.Where(i => i.Genres.Any(familyGenres.ContainsKey)).ToList();

            view.HDMoviePercentage  = 100 * hdMovies.Count;
            view.HDMoviePercentage /= movies.Count;

            view.FamilyMoviePercentage  = 100 * familyMovies.Count;
            view.FamilyMoviePercentage /= movies.Count;

            var moviesWithBackdrops = movies
                                      .Where(i => i.BackdropImagePaths.Count > 0)
                                      .ToList();

            var fields = new List <ItemFields>();

            var itemsWithTopBackdrops = FilterItemsForBackdropDisplay(itemsWithBackdrops).ToList();

            view.BackdropItems = itemsWithTopBackdrops
                                 .OrderBy(i => Guid.NewGuid())
                                 .Take(10)
                                 .AsParallel()
                                 .Select(i => _dtoService.GetBaseItemDto(i, fields, user))
                                 .ToList();

            view.MovieItems = moviesWithBackdrops
                              .OrderBy(i => Guid.NewGuid())
                              .Select(i => GetItemStub(i, ImageType.Backdrop))
                              .Where(i => i != null)
                              .Take(1)
                              .ToList();

            view.TrailerItems = trailers
                                .Where(i => !string.IsNullOrEmpty(i.PrimaryImagePath))
                                .OrderBy(i => Guid.NewGuid())
                                .Select(i => GetItemStub(i, ImageType.Primary))
                                .Where(i => i != null)
                                .Take(1)
                                .ToList();

            view.BoxSetItems = items
                               .OfType <BoxSet>()
                               .Where(i => i.BackdropImagePaths.Count > 0)
                               .OrderBy(i => Guid.NewGuid())
                               .Select(i => GetItemStub(i, ImageType.Backdrop))
                               .Where(i => i != null)
                               .Take(1)
                               .ToList();

            view.ThreeDItems = moviesWithBackdrops
                               .Where(i => i.Is3D)
                               .OrderBy(i => Guid.NewGuid())
                               .Select(i => GetItemStub(i, ImageType.Backdrop))
                               .Where(i => i != null)
                               .Take(1)
                               .ToList();

            var romanceGenres = request.RomanceGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
            var comedyGenres  = request.ComedyGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);

            view.RomanceItems = moviesWithBackdrops
                                .Where(i => i.Genres.Any(romanceGenres.ContainsKey))
                                .OrderBy(i => Guid.NewGuid())
                                .Select(i => GetItemStub(i, ImageType.Backdrop))
                                .Where(i => i != null)
                                .Take(1)
                                .ToList();

            view.ComedyItems = moviesWithBackdrops
                               .Where(i => i.Genres.Any(comedyGenres.ContainsKey))
                               .OrderBy(i => Guid.NewGuid())
                               .Select(i => GetItemStub(i, ImageType.Backdrop))
                               .Where(i => i != null)
                               .Take(1)
                               .ToList();

            view.HDItems = hdMovies
                           .Where(i => i.BackdropImagePaths.Count > 0)
                           .OrderBy(i => Guid.NewGuid())
                           .Select(i => GetItemStub(i, ImageType.Backdrop))
                           .Where(i => i != null)
                           .Take(1)
                           .ToList();

            view.FamilyMovies = familyMovies
                                .Where(i => i.BackdropImagePaths.Count > 0)
                                .OrderBy(i => Guid.NewGuid())
                                .Select(i => GetItemStub(i, ImageType.Backdrop))
                                .Where(i => i != null)
                                .Take(1)
                                .ToList();

            view.PeopleItems = GetActors(items, user.Id);

            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
                                 .OrderBy(i => Guid.NewGuid())
                                 .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)
                                  .Select(i => _dtoService.GetBaseItemDto(i, fields, user))
                                  .ToList();

            view.LatestMovies = movies
                                .OrderByDescending(i => i.DateCreated)
                                .Where(i => !_userDataManager.GetUserData(user.Id, i.GetUserDataKey()).Played)
                                .Take(request.LatestMoviesLimit)
                                .Select(i => _dtoService.GetBaseItemDto(i, fields, user))
                                .ToList();

            view.LatestTrailers = trailers
                                  .OrderByDescending(i => i.DateCreated)
                                  .Where(i => !_userDataManager.GetUserData(user.Id, i.GetUserDataKey()).Played)
                                  .Take(request.LatestTrailersLimit)
                                  .Select(i => _dtoService.GetBaseItemDto(i, fields, user))
                                  .ToList();

            return(ToOptimizedResult(view));
        }
Exemple #2
0
        public object Get(GetMovieView request)
        {
            var result = GetMovieView(request).Result;

            return(ToOptimizedResult(result));
        }
        public object Get(GetMovieView request)
        {
            var user = _userManager.GetUserById(request.UserId);

            var items = GetAllLibraryItems(user.Id, _userManager, _libraryManager, request.ParentId)
                        .Where(i => i is Movie || i is Trailer || i is BoxSet)
                        .ToList();

            var view = new MoviesView();

            var movies = items.OfType <Movie>()
                         .ToList();

            var trailers = items.OfType <Trailer>()
                           .ToList();

            var hdMovies = movies.Where(i => i.IsHD).ToList();

            var familyGenres = request.FamilyGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);

            var familyMovies = movies.Where(i => i.Genres.Any(familyGenres.ContainsKey)).ToList();

            view.HDMoviePercentage  = 100 * hdMovies.Count;
            view.HDMoviePercentage /= movies.Count;

            view.FamilyMoviePercentage  = 100 * familyMovies.Count;
            view.FamilyMoviePercentage /= movies.Count;

            var moviesWithBackdrops = movies
                                      .Where(i => i.GetImages(ImageType.Backdrop).Any())
                                      .ToList();

            var fields = new List <ItemFields>();

            var itemsWithTopBackdrops = FilterItemsForBackdropDisplay(moviesWithBackdrops).ToList();

            view.BackdropItems = itemsWithTopBackdrops
                                 .OrderBy(i => Guid.NewGuid())
                                 .Take(10)
                                 .AsParallel()
                                 .Select(i => _dtoService.GetBaseItemDto(i, fields, user))
                                 .ToList();

            view.MovieItems = moviesWithBackdrops
                              .Randomize("all")
                              .Select(i => GetItemStub(i, ImageType.Backdrop))
                              .Where(i => i != null)
                              .Take(1)
                              .ToList();

            view.TrailerItems = trailers
                                .Where(i => !string.IsNullOrEmpty(i.PrimaryImagePath))
                                .Randomize()
                                .Select(i => GetItemStub(i, ImageType.Primary))
                                .Where(i => i != null)
                                .Take(1)
                                .ToList();

            view.BoxSetItems = items
                               .OfType <BoxSet>()
                               .Where(i => i.GetImages(ImageType.Backdrop).Any())
                               .Randomize()
                               .Select(i => GetItemStub(i, ImageType.Backdrop))
                               .Where(i => i != null)
                               .Take(1)
                               .ToList();

            view.ThreeDItems = moviesWithBackdrops
                               .Where(i => i.Is3D)
                               .Randomize("3d")
                               .Select(i => GetItemStub(i, ImageType.Backdrop))
                               .Where(i => i != null)
                               .Take(1)
                               .ToList();

            var romanceGenres = request.RomanceGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
            var comedyGenres  = request.ComedyGenre.Split(',').ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);

            view.RomanceItems = moviesWithBackdrops
                                .Where(i => i.Genres.Any(romanceGenres.ContainsKey))
                                .Randomize("romance")
                                .Select(i => GetItemStub(i, ImageType.Backdrop))
                                .Where(i => i != null)
                                .Take(1)
                                .ToList();

            view.ComedyItems = moviesWithBackdrops
                               .Where(i => i.Genres.Any(comedyGenres.ContainsKey))
                               .Randomize("comedy")
                               .Select(i => GetItemStub(i, ImageType.Backdrop))
                               .Where(i => i != null)
                               .Take(1)
                               .ToList();

            view.HDItems = hdMovies
                           .Where(i => i.GetImages(ImageType.Backdrop).Any())
                           .Randomize("hd")
                           .Select(i => GetItemStub(i, ImageType.Backdrop))
                           .Where(i => i != null)
                           .Take(1)
                           .ToList();

            view.FamilyMovies = familyMovies
                                .Where(i => i.GetImages(ImageType.Backdrop).Any())
                                .Randomize("family")
                                .Select(i => GetItemStub(i, ImageType.Backdrop))
                                .Where(i => i != null)
                                .Take(1)
                                .ToList();

            var currentUserId  = user.Id;
            var spotlightItems = itemsWithTopBackdrops
                                 .Where(i => i.CommunityRating.HasValue && i.CommunityRating >= 8)
                                 .Where(i => !_userDataManager.GetUserData(currentUserId, i.GetUserDataKey()).Played)
                                 .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
                                 .Randomize("minispotlight")
                                 .ToList();

            // Avoid implicitly captured closure
            miniSpotlightItems.InsertRange(0, 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(3)
                                  .Select(i => _dtoService.GetBaseItemDto(i, fields, user))
                                  .ToList();

            // Avoid implicitly captured closure
            var currentUserId1 = user.Id;

            view.LatestMovies = movies
                                .OrderByDescending(i => i.DateCreated)
                                .Where(i => !_userDataManager.GetUserData(currentUserId1, i.GetUserDataKey()).Played)
                                .Take(request.LatestMoviesLimit)
                                .Select(i => _dtoService.GetBaseItemDto(i, fields, user))
                                .ToList();

            view.LatestTrailers = trailers
                                  .OrderByDescending(i => i.DateCreated)
                                  .Where(i => !_userDataManager.GetUserData(currentUserId1, i.GetUserDataKey()).Played)
                                  .Take(request.LatestTrailersLimit)
                                  .Select(i => _dtoService.GetBaseItemDto(i, fields, user))
                                  .ToList();

            return(ToOptimizedSerializedResultUsingCache(view));
        }
Exemple #4
0
        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);
        }