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();
        }
Beispiel #2
0
        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(GetTvView request)
        {
            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();

            var view = new TvView();

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

            var fields = new List <ItemFields>();

            var seriesWithBestBackdrops = FilterItemsForBackdropDisplay(seriesWithBackdrops).ToList();

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

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

            view.RomanceItems = seriesWithBackdrops
                                .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 = seriesWithBackdrops
                               .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.ActorItems = GetActors(series, user.Id);

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

            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();

            return(ToOptimizedResult(view));
        }