diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs b/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs index 1ea3aa082..3c00a7a29 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs @@ -112,7 +112,7 @@ namespace Ombi.Schedule.Jobs.Plex Logger.LogInformation("We found {0} items", allContent.Count); // Let's now process this. - var contentToAdd = new List(); + var contentToAdd = new HashSet(); foreach (var content in allContent) { if (content.viewGroup.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase)) @@ -291,6 +291,11 @@ namespace Ombi.Schedule.Jobs.Plex show.title); } } + if (contentToAdd.Count > 500) + { + await Repo.AddRange(contentToAdd); + contentToAdd.Clear(); + } } } if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase)) @@ -356,12 +361,18 @@ namespace Ombi.Schedule.Jobs.Plex Logger.LogError(LoggingEvents.PlexContentCacher, e, "Exception when adding new Movie {0}", movie.title); } + + if (contentToAdd.Count > 500) + { + await Repo.AddRange(contentToAdd); + contentToAdd.Clear(); + } } } if (contentToAdd.Count > 500) { await Repo.AddRange(contentToAdd); - contentToAdd = new List(); + contentToAdd.Clear(); } } diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs b/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs index f9fc3fbf8..7d0fde4c7 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs @@ -62,7 +62,6 @@ namespace Ombi.Schedule.Jobs.Plex { if (!Validate(settings)) { - _log.LogWarning("Validation failed"); return; } @@ -101,21 +100,25 @@ namespace Ombi.Schedule.Jobs.Plex { var currentPosition = 0; var resultCount = settings.EpisodeBatchSize == 0 ? 150 : settings.EpisodeBatchSize; + var currentEpisodes = _repo.GetAllEpisodes(); var episodes = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition, resultCount); _log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Total Epsiodes found for {episodes.MediaContainer.librarySectionTitle} = {episodes.MediaContainer.totalSize}"); // Delete all the episodes because we cannot uniquly match an episode to series every time, // see comment below. - await _repo.ExecuteSql("DELETE FROM PlexEpisode"); - await ProcessEpsiodes(episodes); + // 12.03.2017 - I think we should be able to match them now + //await _repo.ExecuteSql("DELETE FROM PlexEpisode"); + + await ProcessEpsiodes(episodes, currentEpisodes); currentPosition += resultCount; while (currentPosition < episodes.MediaContainer.totalSize) { var ep = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition, resultCount); - await ProcessEpsiodes(ep); + + await ProcessEpsiodes(ep, currentEpisodes); _log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Processed {resultCount} more episodes. Total Remaining {episodes.MediaContainer.totalSize - currentPosition}"); currentPosition += resultCount; } @@ -125,58 +128,56 @@ namespace Ombi.Schedule.Jobs.Plex await _repo.SaveChangesAsync(); } - private async Task ProcessEpsiodes(PlexContainer episodes) + private async Task ProcessEpsiodes(PlexContainer episodes, IQueryable currentEpisodes) { var ep = new HashSet(); try { - - - foreach (var episode in episodes?.MediaContainer?.Metadata ?? new Metadata[]{}) - { - // I don't think we need to get the metadata, we only need to get the metadata if we need the provider id (TheTvDbid). Why do we need it for episodes? - // We have the parent and grandparent rating keys to link up to the season and series - //var metadata = _api.GetEpisodeMetaData(server.PlexAuthToken, server.FullUri, episode.ratingKey); - - // This does seem to work, it looks like we can somehow get different rating, grandparent and parent keys with episodes. Not sure how. - //var epExists = currentEpisodes.Any(x => episode.ratingKey == x.Key && - // episode.grandparentRatingKey == x.GrandparentKey); - //if (epExists) - //{ - // continue; - //} - - // Let's check if we have the parent - var seriesExists = await _repo.GetByKey(episode.grandparentRatingKey); - if (seriesExists == null) + foreach (var episode in episodes?.MediaContainer?.Metadata ?? new Metadata[] { }) { - // Ok let's try and match it to a title. TODO (This is experimental) - var seriesMatch = await _repo.GetAll().FirstOrDefaultAsync(x => - x.Title.Equals(episode.grandparentTitle, StringComparison.CurrentCultureIgnoreCase)); - if (seriesMatch == null) + // I don't think we need to get the metadata, we only need to get the metadata if we need the provider id (TheTvDbid). Why do we need it for episodes? + // We have the parent and grandparent rating keys to link up to the season and series + //var metadata = _api.GetEpisodeMetaData(server.PlexAuthToken, server.FullUri, episode.ratingKey); + + // This does seem to work, it looks like we can somehow get different rating, grandparent and parent keys with episodes. Not sure how. + var epExists = currentEpisodes.Any(x => episode.ratingKey == x.Key && + episode.grandparentRatingKey == x.GrandparentKey); + if (epExists) { - _log.LogWarning( - "The episode title {0} we cannot find the parent series. The episode grandparentKey = {1}, grandparentTitle = {2}", - episode.title, episode.grandparentRatingKey, episode.grandparentTitle); continue; } - // Set the rating key to the correct one - episode.grandparentRatingKey = seriesMatch.Key; - } + // Let's check if we have the parent + var seriesExists = await _repo.GetByKey(episode.grandparentRatingKey); + if (seriesExists == null) + { + // Ok let's try and match it to a title. TODO (This is experimental) + seriesExists = await _repo.GetAll().FirstOrDefaultAsync(x => + x.Title.Equals(episode.grandparentTitle, StringComparison.CurrentCultureIgnoreCase)); + if (seriesExists == null) + { + _log.LogWarning( + "The episode title {0} we cannot find the parent series. The episode grandparentKey = {1}, grandparentTitle = {2}", + episode.title, episode.grandparentRatingKey, episode.grandparentTitle); + continue; + } - ep.Add(new PlexEpisode - { - EpisodeNumber = episode.index, - SeasonNumber = episode.parentIndex, - GrandparentKey = episode.grandparentRatingKey, - ParentKey = episode.parentRatingKey, - Key = episode.ratingKey, - Title = episode.title - }); - } + // Set the rating key to the correct one + episode.grandparentRatingKey = seriesExists.Key; + } + + ep.Add(new PlexEpisode + { + EpisodeNumber = episode.index, + SeasonNumber = episode.parentIndex, + GrandparentKey = episode.grandparentRatingKey, + ParentKey = episode.parentRatingKey, + Key = episode.ratingKey, + Title = episode.title + }); + } - await _repo.AddRange(ep); + await _repo.AddRange(ep); } catch (Exception e) { @@ -189,7 +190,7 @@ namespace Ombi.Schedule.Jobs.Plex { if (string.IsNullOrEmpty(settings.PlexAuthToken)) { - return false ; + return false; } return true;