Ejemplo n.º 1
0
        private async Task <ProcessedContent> ProcessServer(PlexServers servers, bool recentlyAddedSearch)
        {
            var retVal            = new ProcessedContent();
            var contentProcessed  = new Dictionary <int, int>();
            var episodesProcessed = new List <int>();

            Logger.LogDebug("Getting all content from server {0}", servers.Name);
            var allContent = await GetAllContent(servers, recentlyAddedSearch);

            Logger.LogDebug("We found {0} items", allContent.Count);

            // Let's now process this.
            var contentToAdd = new HashSet <PlexServerContent>();

            var allEps = Repo.GetAllEpisodes();

            foreach (var content in allContent)
            {
                if (content.viewGroup.Equals(PlexMediaType.Episode.ToString(), StringComparison.CurrentCultureIgnoreCase))
                {
                    Logger.LogDebug("Found some episodes, this must be a recently added sync");
                    var count = 0;
                    foreach (var epInfo in content.Metadata ?? new Metadata[] {})
                    {
                        count++;
                        var grandParentKey = epInfo.grandparentRatingKey;
                        // Lookup the rating key
                        var showMetadata = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri, grandParentKey);

                        var show = showMetadata.MediaContainer.Metadata.FirstOrDefault();
                        if (show == null)
                        {
                            continue;
                        }

                        await ProcessTvShow(servers, show, contentToAdd, contentProcessed);

                        if (contentToAdd.Any())
                        {
                            await Repo.AddRange(contentToAdd, false);

                            if (recentlyAddedSearch)
                            {
                                foreach (var plexServerContent in contentToAdd)
                                {
                                    contentProcessed.Add(plexServerContent.Id, plexServerContent.Key);
                                }
                            }
                            contentToAdd.Clear();
                        }
                        if (count > 200)
                        {
                            await Repo.SaveChangesAsync();
                        }
                    }

                    // Save just to make sure we don't leave anything hanging
                    await Repo.SaveChangesAsync();

                    if (content.Metadata != null)
                    {
                        var episodesAdded = await EpisodeSync.ProcessEpsiodes(content.Metadata, allEps);

                        episodesProcessed.AddRange(episodesAdded.Select(x => x.Id));
                    }
                }
                if (content.viewGroup.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase))
                {
                    // Process Shows
                    Logger.LogDebug("Processing TV Shows");
                    var count = 0;
                    foreach (var show in content.Metadata ?? new Metadata[] { })
                    {
                        count++;
                        await ProcessTvShow(servers, show, contentToAdd, contentProcessed);

                        if (contentToAdd.Any())
                        {
                            await Repo.AddRange(contentToAdd, false);

                            if (recentlyAddedSearch)
                            {
                                foreach (var plexServerContent in contentToAdd)
                                {
                                    contentProcessed.Add(plexServerContent.Id, plexServerContent.Key);
                                }
                            }
                            contentToAdd.Clear();
                        }
                        if (count > 200)
                        {
                            await Repo.SaveChangesAsync();
                        }
                    }

                    await Repo.SaveChangesAsync();
                }
                if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase))
                {
                    Logger.LogDebug("Processing Movies");
                    foreach (var movie in content?.Metadata ?? new Metadata[] { })
                    {
                        // Let's check if we have this movie

                        try
                        {
                            var existing = await Repo.GetFirstContentByCustom(x => x.Title == movie.title &&
                                                                              x.ReleaseYear == movie.year.ToString() &&
                                                                              x.Type == PlexMediaTypeEntity.Movie);

                            // The rating key keeps changing
                            //var existing = await Repo.GetByKey(movie.ratingKey);
                            if (existing != null)
                            {
                                Logger.LogDebug("We already have movie {0}", movie.title);
                                continue;
                            }

                            var hasSameKey = await Repo.GetByKey(movie.ratingKey);

                            if (hasSameKey != null)
                            {
                                await Repo.Delete(hasSameKey);
                            }

                            Logger.LogDebug("Adding movie {0}", movie.title);
                            var metaData = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
                                                                     movie.ratingKey);

                            var providerIds = PlexHelper.GetProviderIdFromPlexGuid(metaData.MediaContainer.Metadata
                                                                                   .FirstOrDefault()
                                                                                   .guid);

                            var item = new PlexServerContent
                            {
                                AddedAt     = DateTime.Now,
                                Key         = movie.ratingKey,
                                ReleaseYear = movie.year.ToString(),
                                Type        = PlexMediaTypeEntity.Movie,
                                Title       = movie.title,
                                Url         = PlexHelper.GetPlexMediaUrl(servers.MachineIdentifier, movie.ratingKey),
                                Seasons     = new List <PlexSeasonsContent>(),
                                Quality     = movie.Media?.FirstOrDefault()?.videoResolution ?? string.Empty
                            };
                            if (providerIds.Type == ProviderType.ImdbId)
                            {
                                item.ImdbId = providerIds.ImdbId;
                            }
                            if (providerIds.Type == ProviderType.TheMovieDbId)
                            {
                                item.TheMovieDbId = providerIds.TheMovieDb;
                            }
                            if (providerIds.Type == ProviderType.TvDbId)
                            {
                                item.TvDbId = providerIds.TheTvDb;
                            }
                            contentToAdd.Add(item);
                        }
                        catch (Exception e)
                        {
                            Logger.LogError(LoggingEvents.PlexContentCacher, e, "Exception when adding new Movie {0}",
                                            movie.title);
                        }

                        if (contentToAdd.Count > 500)
                        {
                            await Repo.AddRange(contentToAdd);

                            foreach (var c in contentToAdd)
                            {
                                contentProcessed.Add(c.Id, c.Key);
                            }
                            contentToAdd.Clear();
                        }
                    }
                }
                if (contentToAdd.Count > 500)
                {
                    await Repo.AddRange(contentToAdd);

                    foreach (var c in contentToAdd)
                    {
                        contentProcessed.Add(c.Id, c.Key);
                    }
                    contentToAdd.Clear();
                }
            }

            if (contentToAdd.Any())
            {
                await Repo.AddRange(contentToAdd);

                foreach (var c in contentToAdd)
                {
                    contentProcessed.Add(c.Id, c.Key);
                }
            }

            retVal.Content  = contentProcessed.Values;
            retVal.Episodes = episodesProcessed;
            return(retVal);
        }
Ejemplo n.º 2
0
        private async Task <ProcessedContent> ProcessServer(PlexServers servers, bool recentlyAddedSearch)
        {
            var retVal            = new ProcessedContent();
            var contentProcessed  = new Dictionary <int, int>();
            var episodesProcessed = new List <int>();

            Logger.LogDebug("Getting all content from server {0}", servers.Name);
            var allContent = await GetAllContent(servers, recentlyAddedSearch);

            Logger.LogDebug("We found {0} items", allContent.Count);


            // Let's now process this.
            var contentToAdd = new HashSet <PlexServerContent>();

            var allEps = Repo.GetAllEpisodes();

            foreach (var content in allContent.OrderByDescending(x => x.viewGroup))
            {
                Logger.LogDebug($"Got type '{content.viewGroup}' to process");
                if (content.viewGroup.Equals(PlexMediaType.Episode.ToString(), StringComparison.InvariantCultureIgnoreCase))
                {
                    Logger.LogDebug("Found some episodes, this must be a recently added sync");
                    var count = 0;
                    foreach (var epInfo in content.Metadata ?? new Metadata[] { })
                    {
                        count++;
                        var grandParentKey = epInfo.grandparentRatingKey;
                        // Lookup the rating key
                        var showMetadata = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri, grandParentKey);

                        var show = showMetadata.MediaContainer.Metadata.FirstOrDefault();
                        if (show == null)
                        {
                            continue;
                        }

                        await ProcessTvShow(servers, show, contentToAdd, contentProcessed);

                        if (contentToAdd.Any())
                        {
                            await Repo.AddRange(contentToAdd, recentlyAddedSearch?true : false);

                            if (recentlyAddedSearch)
                            {
                                foreach (var plexServerContent in contentToAdd)
                                {
                                    if (plexServerContent.Id <= 0)
                                    {
                                        Logger.LogInformation($"Item '{plexServerContent.Title}' has an Plex ID of {plexServerContent.Id} and a Plex Key of {plexServerContent.Key}");
                                    }
                                    contentProcessed.Add(plexServerContent.Id, plexServerContent.Key);
                                }
                            }
                            contentToAdd.Clear();
                        }
                        if (count > 30)
                        {
                            await Repo.SaveChangesAsync();

                            count = 0;
                        }
                    }

                    // Save just to make sure we don't leave anything hanging
                    await Repo.SaveChangesAsync();

                    if (content.Metadata != null)
                    {
                        var episodesAdded = await EpisodeSync.ProcessEpsiodes(content.Metadata, allEps);

                        episodesProcessed.AddRange(episodesAdded.Select(x => x.Id));
                    }
                }
                if (content.viewGroup.Equals(PlexMediaType.Show.ToString(), StringComparison.InvariantCultureIgnoreCase))
                {
                    // Process Shows
                    Logger.LogDebug("Processing TV Shows");
                    var count = 0;
                    foreach (var show in content.Metadata ?? new Metadata[] { })
                    {
                        count++;
                        await ProcessTvShow(servers, show, contentToAdd, contentProcessed);

                        if (contentToAdd.Any())
                        {
                            await Repo.AddRange(contentToAdd, false);

                            if (recentlyAddedSearch)
                            {
                                foreach (var plexServerContent in contentToAdd)
                                {
                                    contentProcessed.Add(plexServerContent.Id, plexServerContent.Key);
                                }
                            }
                            contentToAdd.Clear();
                        }
                        if (count > 30)
                        {
                            await Repo.SaveChangesAsync();
                        }
                    }

                    await Repo.SaveChangesAsync();
                }
                if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.InvariantCultureIgnoreCase))
                {
                    await MovieLoop(servers, content, contentToAdd, contentProcessed);
                }
                if (contentToAdd.Count > 500)
                {
                    await Repo.AddRange(contentToAdd);

                    foreach (var c in contentToAdd)
                    {
                        contentProcessed.Add(c.Id, c.Key);
                    }
                    contentToAdd.Clear();
                }
            }

            if (contentToAdd.Any())
            {
                await Repo.AddRange(contentToAdd);

                foreach (var c in contentToAdd)
                {
                    contentProcessed.Add(c.Id, c.Key);
                }
            }

            retVal.Content  = contentProcessed.Values;
            retVal.Episodes = episodesProcessed;
            return(retVal);
        }