@ -93,13 +93,26 @@ namespace Ombi.Schedule.Jobs.Plex
{
foreach ( var servers in plexSettings . Servers ? ? new List < PlexServers > ( ) )
{
try
{
Logger . LogInformation ( "Starting to cache the content on server {0}" , servers . Name ) ;
await ProcessServer ( servers ) ;
}
catch ( Exception e )
{
Logger . LogWarning ( LoggingEvents . PlexContentCacher , e , "Exception thrown when attempting to cache the Plex Content in server {0}" , servers . Name ) ;
}
}
}
private async Task ProcessServer ( PlexServers servers )
{
Logger . LogInformation ( "Getting all content from server {0}" , servers . Name ) ;
var allContent = await GetAllContent ( servers ) ;
Logger . LogInformation ( "We found {0} items" , allContent . Count ) ;
// Let's now process this.
var contentToAdd = new List < PlexServerContent > ( ) ;
var contentToAdd = new HashSe t< PlexServerContent > ( ) ;
foreach ( var content in allContent )
{
if ( content . viewGroup . Equals ( PlexMediaType . Show . ToString ( ) , StringComparison . CurrentCultureIgnoreCase ) )
@ -128,10 +141,27 @@ namespace Ombi.Schedule.Jobs.Plex
& & x . ReleaseYear = = show . year . ToString ( )
& & x . Type = = PlexMediaTypeEntity . Show ) ;
// Just double check the rating key, since this is our unique constraint
var existingKey = await Repo . GetByKey ( show . ratingKey ) ;
if ( existingKey ! = null )
{
// Damn son.
// Let's check if they match up
var doesMatch = show . title . Equals ( existingKey . Title ,
StringComparison . CurrentCulture ) ;
if ( ! doesMatch )
{
// Something fucked up on Plex at somepoint... Damn, rebuild of lib maybe?
// Lets delete the matching key
await Repo . Delete ( existingKey ) ;
existingKey = null ;
}
}
if ( existingContent ! = null )
{
// Just check the key
var existingKey = await Repo . GetByKey ( show . ratingKey ) ;
if ( existingKey ! = null )
{
// The rating key is all good!
@ -155,7 +185,6 @@ namespace Ombi.Schedule.Jobs.Plex
await Repo . SaveChangesAsync ( ) ;
existingContent = null ;
}
}
// The ratingKey keeps changing...
//var existingContent = await Repo.GetByKey(show.ratingKey);
@ -163,12 +192,14 @@ namespace Ombi.Schedule.Jobs.Plex
{
try
{
Logger . LogInformation ( "We already have show {0} checking for new seasons" , existingContent . Title ) ;
Logger . LogInformation ( "We already have show {0} checking for new seasons" ,
existingContent . Title ) ;
// Ok so we have it, let's check if there are any new seasons
var itemAdded = false ;
foreach ( var season in seasonsContent )
{
var seasonExists = existingContent . Seasons . FirstOrDefault ( x = > x . SeasonKey = = season . SeasonKey ) ;
var seasonExists =
existingContent . Seasons . FirstOrDefault ( x = > x . SeasonKey = = season . SeasonKey ) ;
if ( seasonExists ! = null )
{
@ -184,21 +215,23 @@ namespace Ombi.Schedule.Jobs.Plex
}
catch ( Exception e )
{
Logger . LogError ( LoggingEvents . PlexContentCacher , e , "Exception when adding new seasons to title {0}" , existingContent . Title ) ;
Logger . LogError ( LoggingEvents . PlexContentCacher , e ,
"Exception when adding new seasons to title {0}" , existingContent . Title ) ;
}
}
else
{
try
{
Logger . LogInformation ( "New show {0}, so add it" , show . title ) ;
// Get the show metadata... This sucks since the `metadata` var contains all information about the show
// But it does not contain the `guid` property that we need to pull out thetvdb id...
var showMetadata = await PlexApi . GetMetadata ( servers . PlexAuthToken , servers . FullUri ,
show . ratingKey ) ;
var providerIds = PlexHelper . GetProviderIdFromPlexGuid ( showMetadata . MediaContainer . Metadata . FirstOrDefault ( ) . guid ) ;
var providerIds =
PlexHelper . GetProviderIdFromPlexGuid ( showMetadata . MediaContainer . Metadata . FirstOrDefault ( )
. guid ) ;
var item = new PlexServerContent
{
@ -251,13 +284,17 @@ namespace Ombi.Schedule.Jobs.Plex
item . Seasons . ToList ( ) . AddRange ( seasonsContent ) ;
contentToAdd . Add ( item ) ;
}
catch ( Exception e )
{
Logger . LogError ( LoggingEvents . PlexContentCacher , e , "Exception when adding tv show {0}" , show . title ) ;
Logger . LogError ( LoggingEvents . PlexContentCacher , e , "Exception when adding tv show {0}" ,
show . title ) ;
}
}
if ( contentToAdd . Count > 500 )
{
await Repo . AddRange ( contentToAdd ) ;
contentToAdd . Clear ( ) ;
}
}
}
@ -321,14 +358,21 @@ namespace Ombi.Schedule.Jobs.Plex
}
catch ( Exception e )
{
Logger . LogError ( LoggingEvents . PlexContentCacher , e , "Exception when adding new Movie {0}" , movie . title ) ;
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 < PlexServerContent > ( ) ;
contentToAdd . Clear ( ) ;
}
}
@ -336,8 +380,6 @@ namespace Ombi.Schedule.Jobs.Plex
{
await Repo . AddRange ( contentToAdd ) ;
}
}
}
/// <summary>