Delete plex episodes on every run due to a bug, need to spend quite a bit of time on this.

pull/1941/head
tidusjar 7 years ago
parent 246b0d6dad
commit c7d88f8808

@ -101,17 +101,20 @@ namespace Ombi.Schedule.Jobs.Plex
var currentPosition = 0; var currentPosition = 0;
var resultCount = settings.EpisodeBatchSize == 0 ? 50 : settings.EpisodeBatchSize; var resultCount = settings.EpisodeBatchSize == 0 ? 50 : settings.EpisodeBatchSize;
var episodes = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition, resultCount); var episodes = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition, resultCount);
var currentData = _repo.GetAllEpisodes().AsNoTracking();
_log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Total Epsiodes found for {episodes.MediaContainer.librarySectionTitle} = {episodes.MediaContainer.totalSize}"); _log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Total Epsiodes found for {episodes.MediaContainer.librarySectionTitle} = {episodes.MediaContainer.totalSize}");
await ProcessEpsiodes(episodes, currentData); // 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);
currentPosition += resultCount; currentPosition += resultCount;
while (currentPosition < episodes.MediaContainer.totalSize) while (currentPosition < episodes.MediaContainer.totalSize)
{ {
var ep = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition, var ep = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition,
resultCount); resultCount);
await ProcessEpsiodes(ep, currentData); await ProcessEpsiodes(ep);
_log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Processed {resultCount} more episodes. Total Remaining {episodes.MediaContainer.totalSize - currentPosition}"); _log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Processed {resultCount} more episodes. Total Remaining {episodes.MediaContainer.totalSize - currentPosition}");
currentPosition += resultCount; currentPosition += resultCount;
} }
@ -121,7 +124,7 @@ namespace Ombi.Schedule.Jobs.Plex
await _repo.SaveChangesAsync(); await _repo.SaveChangesAsync();
} }
private async Task ProcessEpsiodes(PlexContainer episodes, IQueryable<PlexEpisode> currentEpisodes) private async Task ProcessEpsiodes(PlexContainer episodes)
{ {
var ep = new HashSet<PlexEpisode>(); var ep = new HashSet<PlexEpisode>();
@ -131,12 +134,13 @@ namespace Ombi.Schedule.Jobs.Plex
// We have the parent and grandparent rating keys to link up to the season and series // 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); //var metadata = _api.GetEpisodeMetaData(server.PlexAuthToken, server.FullUri, episode.ratingKey);
var epExists = currentEpisodes.Any(x => episode.ratingKey == x.Key && // This does seem to work, it looks like we can somehow get different rating, grandparent and parent keys with episodes. Not sure how.
episode.grandparentRatingKey == x.GrandparentKey); //var epExists = currentEpisodes.Any(x => episode.ratingKey == x.Key &&
if (epExists) // episode.grandparentRatingKey == x.GrandparentKey);
{ //if (epExists)
continue; //{
} // continue;
//}
ep.Add(new PlexEpisode ep.Add(new PlexEpisode
{ {

@ -22,5 +22,7 @@ namespace Ombi.Store.Repository
IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>( IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> navigationPropertyPath) IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> navigationPropertyPath)
where TEntity : class; where TEntity : class;
Task ExecuteSql(string sql);
} }
} }

@ -72,6 +72,11 @@ namespace Ombi.Store.Repository
return source.Include(navigationPropertyPath); return source.Include(navigationPropertyPath);
} }
public async Task ExecuteSql(string sql)
{
await _ctx.Database.ExecuteSqlCommandAsync(sql);
}
private bool _disposed; private bool _disposed;
// Protected implementation of Dispose pattern. // Protected implementation of Dispose pattern.

Loading…
Cancel
Save