@ -46,14 +46,15 @@ using Ombi.Services.Interfaces;
using Ombi.Services.Jobs.Templates ;
using Quartz ;
namespace Ombi.Services.Jobs
namespace Ombi.Services.Jobs .RecentlyAddedNewsletter
{
public class RecentlyAdded : HtmlTemplateGenerator , IJob , IRecentlyAdded , IMassEmail
public class RecentlyAdded Newsletter : HtmlTemplateGenerator , IJob , IRecentlyAdded , IMassEmail
{
public RecentlyAdded ( IPlexApi api , ISettingsService < PlexSettings > plexSettings ,
public RecentlyAdded Newsletter ( IPlexApi api , ISettingsService < PlexSettings > plexSettings ,
ISettingsService < EmailNotificationSettings > email , IJobRecord rec ,
ISettingsService < NewletterSettings > newsletter ,
IPlexReadOnlyDatabase db , IUserHelper userHelper )
IPlexReadOnlyDatabase db , IUserHelper userHelper , IEmbyAddedNewsletter embyNews ,
ISettingsService < EmbySettings > embyS )
{
JobRecord = rec ;
Api = api ;
@ -62,23 +63,25 @@ namespace Ombi.Services.Jobs
NewsletterSettings = newsletter ;
PlexDb = db ;
UserHelper = userHelper ;
EmbyNewsletter = embyNews ;
EmbySettings = embyS ;
}
private IPlexApi Api { get ; }
private TvMazeApi TvApi = new TvMazeApi ( ) ;
private readonly TheMovieDbApi _movieApi = new TheMovieDbApi ( ) ;
private const int MetadataTypeTv = 4 ;
private const int MetadataTypeMovie = 1 ;
private ISettingsService < PlexSettings > PlexSettings { get ; }
private ISettingsService < EmbySettings > EmbySettings { get ; }
private ISettingsService < EmailNotificationSettings > EmailSettings { get ; }
private ISettingsService < NewletterSettings > NewsletterSettings { get ; }
private IJobRecord JobRecord { get ; }
private IPlexReadOnlyDatabase PlexDb { get ; }
private IUserHelper UserHelper { get ; }
private IEmbyAddedNewsletter EmbyNewsletter { get ; }
private static readonly Logger Log = LogManager . GetCurrentClassLogger ( ) ;
public void Start ( )
public void Start NewsLetter ( )
{
try
{
@ -88,7 +91,7 @@ namespace Ombi.Services.Jobs
return ;
}
JobRecord . SetRunning ( true , JobNames . RecentlyAddedEmail ) ;
Start ( settings ) ;
Start NewsLetter ( settings ) ;
}
catch ( Exception e )
{
@ -102,127 +105,135 @@ namespace Ombi.Services.Jobs
}
public void Execute ( IJobExecutionContext context )
{
Start ( ) ;
Start NewsLetter ( ) ;
}
public void RecentlyAddedAdminTest ( )
{
Log . Debug ( "Starting Recently Added Newsletter Test" ) ;
var settings = NewsletterSettings . GetSettings ( ) ;
Start ( settings , true ) ;
Start NewsLetter ( settings , true ) ;
}
public void MassEmailAdminTest ( string html , string subject )
{
Log . Debug ( "Starting Mass Email Test" ) ;
var settings = NewsletterSettings . GetSettings ( ) ;
var plexSettings = PlexSettings . GetSettings ( ) ;
var template = new MassEmailTemplate ( ) ;
var body = template . LoadTemplate ( html ) ;
Send ( settings , body , plexSettings , true , subject ) ;
Send MassEmail( body , subject , true ) ;
}
public void SendMassEmail ( string html , string subject )
{
Log . Debug ( "Starting Mass Email Test" ) ;
var settings = NewsletterSettings . GetSettings ( ) ;
var plexSettings = PlexSettings . GetSettings ( ) ;
var template = new MassEmailTemplate ( ) ;
var body = template . LoadTemplate ( html ) ;
Send ( settings , body , plexSettings , false , subject ) ;
Send MassEmail( body , subject , false ) ;
}
private void Start ( NewletterSettings newletterSettings , bool testEmail = false )
private void Start NewsLetter ( NewletterSettings newletterSettings , bool testEmail = false )
{
var sb = new StringBuilder ( ) ;
var plexSettings = PlexSettings . GetSettings ( ) ;
Log . Debug ( "Got Plex Settings" ) ;
var embySettings = EmbySettings . GetSettings ( ) ;
if ( embySettings . Enable )
{
var html = EmbyNewsletter . GetNewsletterHtml ( testEmail ) ;
var libs = Api . GetLibrarySections ( plexSettings . PlexAuthToken , plexSettings . FullUri ) ;
Log . Debug ( "Getting Plex Library Sections" ) ;
var escapedHtml = new string ( html . Where ( c = > ! char . IsControl ( c ) ) . ToArray ( ) ) ;
Log . Debug ( escapedHtml ) ;
SendNewsletter ( newletterSettings , escapedHtml , testEmail ) ;
}
else
{
var sb = new StringBuilder ( ) ;
var plexSettings = PlexSettings . GetSettings ( ) ;
Log . Debug ( "Got Plex Settings" ) ;
var tvSections = libs . Directories . Where ( x = > x . type . Equals ( PlexMediaType . Show . ToString ( ) , StringComparison . CurrentCultureIgnoreCase ) ) ; // We could have more than 1 lib
Log . Debug ( "Filtered sections for TV" ) ;
var movieSection = libs . Directories . Where ( x = > x . type . Equals ( PlexMediaType . Movie . ToString ( ) , StringComparison . CurrentCultureIgnoreCase ) ) ; // We could have more than 1 lib
Log . Debug ( "Filtered sections for Movies" ) ;
var libs = Api . GetLibrarySections ( plexSettings . PlexAuthToken , plexSettings . FullUri ) ;
Log . Debug ( "Getting Plex Library Sections" ) ;
var plexVersion = Api . GetStatus ( plexSettings . PlexAuthToken , plexSettings . FullUri ) . Version ;
var tvSections = libs . Directories . Where ( x = > x . type . Equals ( PlexMediaType . Show . ToString ( ) , StringComparison . CurrentCultureIgnoreCase ) ) ; // We could have more than 1 lib
Log . Debug ( "Filtered sections for TV" ) ;
var movieSection = libs . Directories . Where ( x = > x . type . Equals ( PlexMediaType . Movie . ToString ( ) , StringComparison . CurrentCultureIgnoreCase ) ) ; // We could have more than 1 lib
Log . Debug ( "Filtered sections for Movies" ) ;
var html = string . Empty ;
if ( plexVersion . StartsWith ( "1.3" ) )
{
var tvMetadata = new List < Metadata > ( ) ;
var movieMetadata = new List < Metadata > ( ) ;
foreach ( var tvSection in tvSections )
var plexVersion = Api . GetStatus ( plexSettings . PlexAuthToken , plexSettings . FullUri ) . Version ;
var html = string . Empty ;
if ( plexVersion . StartsWith ( "1.3" ) )
{
var item = Api . RecentlyAdded ( plexSettings . PlexAuthToken , plexSettings . FullUri ,
tvSection ? . Key ) ;
if ( item ? . MediaContainer ? . Metadata ! = null )
var tvMetadata = new List < Metadata > ( ) ;
var movieMetadata = new List < Metadata > ( ) ;
foreach ( var tvSection in tvSections )
{
tvMetadata . AddRange ( item ? . MediaContainer ? . Metadata ) ;
var item = Api . RecentlyAdded ( plexSettings . PlexAuthToken , plexSettings . FullUri ,
tvSection ? . Key ) ;
if ( item ? . MediaContainer ? . Metadata ! = null )
{
tvMetadata . AddRange ( item ? . MediaContainer ? . Metadata ) ;
}
}
}
Log . Debug ( "Got RecentlyAdded TV Shows" ) ;
foreach ( var movie in movieSection )
{
var recentlyAddedMovies = Api . RecentlyAdded ( plexSettings . PlexAuthToken , plexSettings . FullUri , movie ? . Key ) ;
if ( recentlyAddedMovies ? . MediaContainer ? . Metadata ! = null )
Log . Debug ( "Got RecentlyAdded TV Shows" ) ;
foreach ( var movie in movieSection )
{
movieMetadata . AddRange ( recentlyAddedMovies ? . MediaContainer ? . Metadata ) ;
var recentlyAddedMovies = Api . RecentlyAdded ( plexSettings . PlexAuthToken , plexSettings . FullUri , movie ? . Key ) ;
if ( recentlyAddedMovies ? . MediaContainer ? . Metadata ! = null )
{
movieMetadata . AddRange ( recentlyAddedMovies ? . MediaContainer ? . Metadata ) ;
}
}
Log . Debug ( "Got RecentlyAdded Movies" ) ;
Log . Debug ( "Started Generating Movie HTML" ) ;
GenerateMovieHtml ( movieMetadata , plexSettings , sb ) ;
Log . Debug ( "Finished Generating Movie HTML" ) ;
Log . Debug ( "Started Generating TV HTML" ) ;
GenerateTvHtml ( tvMetadata , plexSettings , sb ) ;
Log . Debug ( "Finished Generating TV HTML" ) ;
var template = new RecentlyAddedTemplate ( ) ;
html = template . LoadTemplate ( sb . ToString ( ) ) ;
Log . Debug ( "Loaded the template" ) ;
}
Log . Debug ( "Got RecentlyAdded Movies" ) ;
Log . Debug ( "Started Generating Movie HTML" ) ;
GenerateMovieHtml ( movieMetadata , plexSettings , sb ) ;
Log . Debug ( "Finished Generating Movie HTML" ) ;
Log . Debug ( "Started Generating TV HTML" ) ;
GenerateTvHtml ( tvMetadata , plexSettings , sb ) ;
Log . Debug ( "Finished Generating TV HTML" ) ;
var template = new RecentlyAddedTemplate ( ) ;
html = template . LoadTemplate ( sb . ToString ( ) ) ;
Log . Debug ( "Loaded the template" ) ;
}
else
{
// Old API
var tvChild = new List < RecentlyAddedChild > ( ) ;
var movieChild = new List < RecentlyAddedChild > ( ) ;
foreach ( var tvSection in tvSections )
else
{
var recentlyAddedTv = Api . RecentlyAddedOld ( plexSettings . PlexAuthToken , plexSettings . FullUri , tvSection ? . Key ) ;
if ( recentlyAddedTv ? . _children ! = null )
// Old API
var tvChild = new List < RecentlyAddedChild > ( ) ;
var movieChild = new List < RecentlyAddedChild > ( ) ;
foreach ( var tvSection in tvSections )
{
tvChild . AddRange ( recentlyAddedTv ? . _children ) ;
var recentlyAddedTv = Api . RecentlyAddedOld ( plexSettings . PlexAuthToken , plexSettings . FullUri , tvSection ? . Key ) ;
if ( recentlyAddedTv ? . _children ! = null )
{
tvChild . AddRange ( recentlyAddedTv ? . _children ) ;
}
}
}
Log . Debug ( "Got RecentlyAdded TV Shows" ) ;
foreach ( var movie in movieSection )
{
var recentlyAddedMovies = Api . RecentlyAddedOld ( plexSettings . PlexAuthToken , plexSettings . FullUri , movie ? . Key ) ;
if ( recentlyAddedMovies ? . _children ! = null )
Log . Debug ( "Got RecentlyAdded TV Shows" ) ;
foreach ( var movie in movieSection )
{
tvChild . AddRange ( recentlyAddedMovies ? . _children ) ;
var recentlyAddedMovies = Api . RecentlyAddedOld ( plexSettings . PlexAuthToken , plexSettings . FullUri , movie ? . Key ) ;
if ( recentlyAddedMovies ? . _children ! = null )
{
tvChild . AddRange ( recentlyAddedMovies ? . _children ) ;
}
}
Log . Debug ( "Got RecentlyAdded Movies" ) ;
Log . Debug ( "Started Generating Movie HTML" ) ;
GenerateMovieHtml ( movieChild , plexSettings , sb ) ;
Log . Debug ( "Finished Generating Movie HTML" ) ;
Log . Debug ( "Started Generating TV HTML" ) ;
GenerateTvHtml ( tvChild , plexSettings , sb ) ;
Log . Debug ( "Finished Generating TV HTML" ) ;
var template = new RecentlyAddedTemplate ( ) ;
html = template . LoadTemplate ( sb . ToString ( ) ) ;
Log . Debug ( "Loaded the template" ) ;
}
Log . Debug ( "Got RecentlyAdded Movies" ) ;
Log . Debug ( "Started Generating Movie HTML" ) ;
GenerateMovieHtml ( movieChild , plexSettings , sb ) ;
Log . Debug ( "Finished Generating Movie HTML" ) ;
Log . Debug ( "Started Generating TV HTML" ) ;
GenerateTvHtml ( tvChild , plexSettings , sb ) ;
Log . Debug ( "Finished Generating TV HTML" ) ;
var template = new RecentlyAddedTemplate ( ) ;
html = template . LoadTemplate ( sb . ToString ( ) ) ;
Log . Debug ( "Loaded the template" ) ;
string escapedHtml = new string ( html . Where ( c = > ! char . IsControl ( c ) ) . ToArray ( ) ) ;
Log . Debug ( escapedHtml ) ;
SendNewsletter ( newletterSettings , escapedHtml , testEmail ) ;
}
string escapedHtml = new string ( html . Where ( c = > ! char . IsControl ( c ) ) . ToArray ( ) ) ;
Log . Debug ( escapedHtml ) ;
Send ( newletterSettings , escapedHtml , plexSettings , testEmail ) ;
}
private void GenerateMovieHtml ( List < RecentlyAddedChild > movies , PlexSettings plexSettings , StringBuilder sb )
@ -457,9 +468,49 @@ namespace Ombi.Services.Jobs
sb . Append ( "</table><br /><br />" ) ;
}
private void Send ( NewletterSettings newletterSettings , string html , PlexSettings plexSettings , bool testEmail = false , string subject = "New Content on Plex!" )
private void SendMassEmail ( string html , string subject , bool testEmail )
{
Log . Debug ( "Entering Send" ) ;
var settings = EmailSettings . GetSettings ( ) ;
if ( ! settings . Enabled | | string . IsNullOrEmpty ( settings . EmailHost ) )
{
return ;
}
var body = new BodyBuilder { HtmlBody = html , TextBody = "This email is only available on devices that support HTML." } ;
var message = new MimeMessage
{
Body = body . ToMessageBody ( ) ,
Subject = subject
} ;
Log . Debug ( "Created Plain/HTML MIME body" ) ;
if ( ! testEmail )
{
var users = UserHelper . GetUsers ( ) ; // Get all users
if ( users ! = null )
{
foreach ( var user in users )
{
if ( ! string . IsNullOrEmpty ( user . EmailAddress ) )
{
message . Bcc . Add ( new MailboxAddress ( user . Username , user . EmailAddress ) ) ; // BCC everyone
}
}
}
}
message . Bcc . Add ( new MailboxAddress ( settings . EmailUsername , settings . RecipientEmail ) ) ; // Include the admin
message . From . Add ( new MailboxAddress ( settings . EmailUsername , settings . EmailSender ) ) ;
SendMail ( settings , message ) ;
}
// TODO Emby
private void SendNewsletter ( NewletterSettings newletterSettings , string html , bool testEmail = false , string subject = "New Content on Plex!" )
{
Log . Debug ( "Entering SendNewsletter" ) ;
var settings = EmailSettings . GetSettings ( ) ;
if ( ! settings . Enabled | | string . IsNullOrEmpty ( settings . EmailHost ) )
@ -506,6 +557,11 @@ namespace Ombi.Services.Jobs
message . Bcc . Add ( new MailboxAddress ( settings . EmailUsername , settings . RecipientEmail ) ) ; // Include the admin
message . From . Add ( new MailboxAddress ( settings . EmailUsername , settings . EmailSender ) ) ;
SendMail ( settings , message ) ;
}
private void SendMail ( EmailNotificationSettings settings , MimeMessage message )
{
try
{
using ( var client = new SmtpClient ( ) )