Esempio n. 1
0
        private async Task CacheEpisodes(JellyfinServers server)
        {
            var allEpisodes = await Api.GetAllEpisodes(server.ApiKey, 0, 200, server.AdministratorId, server.FullUri);

            var total     = allEpisodes.TotalRecordCount;
            var processed = 1;
            var epToAdd   = new HashSet <JellyfinEpisode>();

            while (processed < total)
            {
                foreach (var ep in allEpisodes.Items)
                {
                    processed++;

                    if (ep.LocationType?.Equals("Virtual", StringComparison.InvariantCultureIgnoreCase) ?? false)
                    {
                        // For some reason Jellyfin is not respecting the `IsVirtualItem` field.
                        continue;
                    }

                    // Let's make sure we have the parent request, stop those pesky forign key errors,
                    // Damn me having data integrity
                    var parent = await _repo.GetByJellyfinId(ep.SeriesId);

                    if (parent == null)
                    {
                        _logger.LogInformation("The episode {0} does not relate to a series, so we cannot save this",
                                               ep.Name);
                        continue;
                    }

                    var existingEpisode = await _repo.GetEpisodeByJellyfinId(ep.Id);

                    // Make sure it's not in the hashset too
                    var existingInList = epToAdd.Any(x => x.JellyfinId == ep.Id);

                    if (existingEpisode == null && !existingInList)
                    {
                        _logger.LogDebug("Adding new episode {0} to parent {1}", ep.Name, ep.SeriesName);
                        // add it
                        epToAdd.Add(new JellyfinEpisode
                        {
                            JellyfinId    = ep.Id,
                            EpisodeNumber = ep.IndexNumber,
                            SeasonNumber  = ep.ParentIndexNumber,
                            ParentId      = ep.SeriesId,
                            TvDbId        = ep.ProviderIds.Tvdb,
                            TheMovieDbId  = ep.ProviderIds.Tmdb,
                            ImdbId        = ep.ProviderIds.Imdb,
                            Title         = ep.Name,
                            AddedAt       = DateTime.UtcNow
                        });

                        if (ep.IndexNumberEnd.HasValue && ep.IndexNumberEnd.Value != ep.IndexNumber)
                        {
                            epToAdd.Add(new JellyfinEpisode
                            {
                                JellyfinId    = ep.Id,
                                EpisodeNumber = ep.IndexNumberEnd.Value,
                                SeasonNumber  = ep.ParentIndexNumber,
                                ParentId      = ep.SeriesId,
                                TvDbId        = ep.ProviderIds.Tvdb,
                                TheMovieDbId  = ep.ProviderIds.Tmdb,
                                ImdbId        = ep.ProviderIds.Imdb,
                                Title         = ep.Name,
                                AddedAt       = DateTime.UtcNow
                            });
                        }
                    }
                }

                await _repo.AddRange(epToAdd);

                epToAdd.Clear();
                allEpisodes = await Api.GetAllEpisodes(server.ApiKey, processed, 200, server.AdministratorId, server.FullUri);
            }

            if (epToAdd.Any())
            {
                await _repo.AddRange(epToAdd);
            }
        }
Esempio n. 2
0
        private async Task ProcessTv(JellyfinServers server, string parentId = default)
        {
            // TV Time
            var mediaToAdd = new HashSet <JellyfinContent>();
            var tv         = await Api.GetAllShows(server.ApiKey, parentId, 0, 200, server.AdministratorId, server.FullUri);

            var totalTv   = tv.TotalRecordCount;
            var processed = 1;

            while (processed < totalTv)
            {
                foreach (var tvShow in tv.Items)
                {
                    try
                    {
                        processed++;
                        if (string.IsNullOrEmpty(tvShow.ProviderIds?.Tvdb))
                        {
                            _logger.LogInformation("Provider Id on tv {0} is null", tvShow.Name);
                            continue;
                        }

                        var existingTv = await _repo.GetByJellyfinId(tvShow.Id);

                        if (existingTv == null)
                        {
                            _logger.LogDebug("Adding new TV Show {0}", tvShow.Name);
                            mediaToAdd.Add(new JellyfinContent
                            {
                                TvDbId       = tvShow.ProviderIds?.Tvdb,
                                ImdbId       = tvShow.ProviderIds?.Imdb,
                                TheMovieDbId = tvShow.ProviderIds?.Tmdb,
                                Title        = tvShow.Name,
                                Type         = JellyfinMediaType.Series,
                                JellyfinId   = tvShow.Id,
                                Url          = JellyfinHelper.GetJellyfinMediaUrl(tvShow.Id, server?.ServerId, server.ServerHostname),
                                AddedAt      = DateTime.UtcNow
                            });
                        }
                        else
                        {
                            _logger.LogDebug("We already have TV Show {0}", tvShow.Name);
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                // Get the next batch
                tv = await Api.GetAllShows(server.ApiKey, parentId, processed, 200, server.AdministratorId, server.FullUri);

                await _repo.AddRange(mediaToAdd);

                mediaToAdd.Clear();
            }

            if (mediaToAdd.Any())
            {
                await _repo.AddRange(mediaToAdd);
            }
        }
Esempio n. 3
0
        private async Task StartServerCache(JellyfinServers server, JellyfinSettings settings)
        {
            if (!ValidateSettings(server))
            {
                return;
            }

            //await _repo.ExecuteSql("DELETE FROM JellyfinEpisode");
            //await _repo.ExecuteSql("DELETE FROM JellyfinContent");

            var movies = await Api.GetAllMovies(server.ApiKey, 0, 200, server.AdministratorId, server.FullUri);

            var totalCount = movies.TotalRecordCount;
            var processed  = 1;

            var mediaToAdd = new HashSet <JellyfinContent>();

            while (processed < totalCount)
            {
                foreach (var movie in movies.Items)
                {
                    if (movie.Type.Equals("boxset", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var movieInfo =
                            await Api.GetCollection(movie.Id, server.ApiKey, server.AdministratorId, server.FullUri);

                        foreach (var item in movieInfo.Items)
                        {
                            await ProcessMovies(item, mediaToAdd, server);
                        }

                        processed++;
                    }
                    else
                    {
                        processed++;
                        // Regular movie
                        await ProcessMovies(movie, mediaToAdd, server);
                    }
                }

                // Get the next batch
                movies = await Api.GetAllMovies(server.ApiKey, processed, 200, server.AdministratorId, server.FullUri);

                await _repo.AddRange(mediaToAdd);

                mediaToAdd.Clear();
            }


            // TV Time
            var tv = await Api.GetAllShows(server.ApiKey, 0, 200, server.AdministratorId, server.FullUri);

            var totalTv = tv.TotalRecordCount;

            processed = 1;
            while (processed < totalTv)
            {
                foreach (var tvShow in tv.Items)
                {
                    try
                    {
                        processed++;
                        if (string.IsNullOrEmpty(tvShow.ProviderIds?.Tvdb))
                        {
                            _logger.LogInformation("Provider Id on tv {0} is null", tvShow.Name);
                            continue;
                        }

                        var existingTv = await _repo.GetByJellyfinId(tvShow.Id);

                        if (existingTv == null)
                        {
                            _logger.LogDebug("Adding new TV Show {0}", tvShow.Name);
                            mediaToAdd.Add(new JellyfinContent
                            {
                                TvDbId       = tvShow.ProviderIds?.Tvdb,
                                ImdbId       = tvShow.ProviderIds?.Imdb,
                                TheMovieDbId = tvShow.ProviderIds?.Tmdb,
                                Title        = tvShow.Name,
                                Type         = JellyfinMediaType.Series,
                                JellyfinId   = tvShow.Id,
                                Url          = JellyfinHelper.GetJellyfinMediaUrl(tvShow.Id, server?.ServerId, server.ServerHostname),
                                AddedAt      = DateTime.UtcNow
                            });
                        }
                        else
                        {
                            _logger.LogDebug("We already have TV Show {0}", tvShow.Name);
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                // Get the next batch
                tv = await Api.GetAllShows(server.ApiKey, processed, 200, server.AdministratorId, server.FullUri);

                await _repo.AddRange(mediaToAdd);

                mediaToAdd.Clear();
            }

            if (mediaToAdd.Any())
            {
                await _repo.AddRange(mediaToAdd);
            }
        }