|
|
@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
|
|
|
using MailKit;
|
|
|
|
using MailKit;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Ombi.Api.TheMovieDb;
|
|
|
|
using Ombi.Api.TheMovieDb;
|
|
|
|
using Ombi.Api.TheMovieDb.Models;
|
|
|
|
using Ombi.Api.TheMovieDb.Models;
|
|
|
|
using Ombi.Api.TvMaze;
|
|
|
|
using Ombi.Api.TvMaze;
|
|
|
@ -28,7 +29,7 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
public NewsletterJob(IPlexContentRepository plex, IEmbyContentRepository emby, IRepository<RecentlyAddedLog> addedLog,
|
|
|
|
public NewsletterJob(IPlexContentRepository plex, IEmbyContentRepository emby, IRepository<RecentlyAddedLog> addedLog,
|
|
|
|
IMovieDbApi movieApi, ITvMazeApi tvApi, IEmailProvider email, ISettingsService<CustomizationSettings> custom,
|
|
|
|
IMovieDbApi movieApi, ITvMazeApi tvApi, IEmailProvider email, ISettingsService<CustomizationSettings> custom,
|
|
|
|
ISettingsService<EmailNotificationSettings> emailSettings, INotificationTemplatesRepository templateRepo,
|
|
|
|
ISettingsService<EmailNotificationSettings> emailSettings, INotificationTemplatesRepository templateRepo,
|
|
|
|
UserManager<OmbiUser> um, ISettingsService<NewsletterSettings> newsletter)
|
|
|
|
UserManager<OmbiUser> um, ISettingsService<NewsletterSettings> newsletter, ILogger<NewsletterJob> log)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_plex = plex;
|
|
|
|
_plex = plex;
|
|
|
|
_emby = emby;
|
|
|
|
_emby = emby;
|
|
|
@ -44,6 +45,7 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
_emailSettings.ClearCache();
|
|
|
|
_emailSettings.ClearCache();
|
|
|
|
_customizationSettings.ClearCache();
|
|
|
|
_customizationSettings.ClearCache();
|
|
|
|
_newsletterSettings.ClearCache();
|
|
|
|
_newsletterSettings.ClearCache();
|
|
|
|
|
|
|
|
_log = log;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private readonly IPlexContentRepository _plex;
|
|
|
|
private readonly IPlexContentRepository _plex;
|
|
|
@ -57,6 +59,7 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
private readonly ISettingsService<EmailNotificationSettings> _emailSettings;
|
|
|
|
private readonly ISettingsService<EmailNotificationSettings> _emailSettings;
|
|
|
|
private readonly ISettingsService<NewsletterSettings> _newsletterSettings;
|
|
|
|
private readonly ISettingsService<NewsletterSettings> _newsletterSettings;
|
|
|
|
private readonly UserManager<OmbiUser> _userManager;
|
|
|
|
private readonly UserManager<OmbiUser> _userManager;
|
|
|
|
|
|
|
|
private readonly ILogger _log;
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Start(NewsletterSettings settings, bool test)
|
|
|
|
public async Task Start(NewsletterSettings settings, bool test)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -76,15 +79,11 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var customization = await _customizationSettings.GetSettingsAsync();
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
// Get the Content
|
|
|
|
|
|
|
|
var plexContent = _plex.GetAll().Include(x => x.Episodes).AsNoTracking();
|
|
|
|
|
|
|
|
var embyContent = _emby.GetAll().Include(x => x.Episodes).AsNoTracking();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var addedLog = _recentlyAddedLog.GetAll();
|
|
|
|
var customization = await _customizationSettings.GetSettingsAsync();
|
|
|
|
var addedPlexMovieLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Parent).Select(x => x.ContentId);
|
|
|
|
|
|
|
|
var addedEmbyMoviesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Parent).Select(x => x.ContentId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var addedPlexEpisodesLogIds =
|
|
|
|
var addedPlexEpisodesLogIds =
|
|
|
|
addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Episode);
|
|
|
|
addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Episode);
|
|
|
@ -94,13 +93,16 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
// Filter out the ones that we haven't sent yet
|
|
|
|
// Filter out the ones that we haven't sent yet
|
|
|
|
var plexContentMoviesToSend = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie && !addedPlexMovieLogIds.Contains(int.Parse(x.TheMovieDbId)));
|
|
|
|
var plexContentMoviesToSend = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie && !addedPlexMovieLogIds.Contains(int.Parse(x.TheMovieDbId)));
|
|
|
|
var embyContentMoviesToSend = embyContent.Where(x => x.Type == EmbyMediaType.Movie && !addedEmbyMoviesLogIds.Contains(int.Parse(x.TheMovieDbId)));
|
|
|
|
var embyContentMoviesToSend = embyContent.Where(x => x.Type == EmbyMediaType.Movie && !addedEmbyMoviesLogIds.Contains(int.Parse(x.TheMovieDbId)));
|
|
|
|
|
|
|
|
_log.LogInformation("Plex Movies to send: {0}", plexContentMoviesToSend.Count());
|
|
|
|
|
|
|
|
_log.LogInformation("Emby Movies to send: {0}", embyContentMoviesToSend.Count());
|
|
|
|
|
|
|
|
|
|
|
|
var plexEpisodesToSend =
|
|
|
|
var plexEpisodesToSend =
|
|
|
|
FilterPlexEpisodes(_plex.GetAllEpisodes().Include(x => x.Series).AsNoTracking(), addedPlexEpisodesLogIds);
|
|
|
|
FilterPlexEpisodes(_plex.GetAllEpisodes().Include(x => x.Series).AsNoTracking(), addedPlexEpisodesLogIds);
|
|
|
|
var embyEpisodesToSend = FilterEmbyEpisodes(_emby.GetAllEpisodes().Include(x => x.Series).AsNoTracking(),
|
|
|
|
var embyEpisodesToSend = FilterEmbyEpisodes(_emby.GetAllEpisodes().Include(x => x.Series).AsNoTracking(),
|
|
|
|
addedEmbyEpisodesLogIds);
|
|
|
|
addedEmbyEpisodesLogIds);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_log.LogInformation("Plex Episodes to send: {0}", plexEpisodesToSend.Count());
|
|
|
|
|
|
|
|
_log.LogInformation("Emby Episodes to send: {0}", embyEpisodesToSend.Count());
|
|
|
|
var body = string.Empty;
|
|
|
|
var body = string.Empty;
|
|
|
|
if (test)
|
|
|
|
if (test)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -128,14 +130,32 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//foreach (var emails in settings.ExternalEmails)
|
|
|
|
foreach (var emails in settings.ExternalEmails)
|
|
|
|
//{
|
|
|
|
{
|
|
|
|
// users.Add(new OmbiUser
|
|
|
|
users.Add(new OmbiUser
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// UserName = emails,
|
|
|
|
UserName = emails,
|
|
|
|
// Email = emails
|
|
|
|
Email = emails
|
|
|
|
// });
|
|
|
|
});
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
var emailTasks = new List<Task>();
|
|
|
|
|
|
|
|
foreach (var user in users)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// Get the users to send it to
|
|
|
|
|
|
|
|
var users = await _userManager.GetUsersInRoleAsync(OmbiRoles.RecievesNewsletter);
|
|
|
|
|
|
|
|
if (!users.Any())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var emails in settings.ExternalEmails)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
users.Add(new OmbiUser
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
UserName = emails,
|
|
|
|
|
|
|
|
Email = emails
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
var emailTasks = new List<Task>();
|
|
|
|
var emailTasks = new List<Task>();
|
|
|
|
foreach (var user in users)
|
|
|
|
foreach (var user in users)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -180,7 +200,6 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
SeasonNumber = p.SeasonNumber
|
|
|
|
SeasonNumber = p.SeasonNumber
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var e in embyContentMoviesToSend)
|
|
|
|
foreach (var e in embyContentMoviesToSend)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (e.Type == EmbyMediaType.Movie)
|
|
|
|
if (e.Type == EmbyMediaType.Movie)
|
|
|
@ -208,7 +227,6 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await _recentlyAddedLog.AddRange(recentlyAddedLog);
|
|
|
|
await _recentlyAddedLog.AddRange(recentlyAddedLog);
|
|
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(emailTasks.ToArray());
|
|
|
|
await Task.WhenAll(emailTasks.ToArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
@ -231,6 +249,13 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
emailSettings);
|
|
|
|
emailSettings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_log.LogError(e, "Error when attempting to create newsletter");
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Start()
|
|
|
|
public async Task Start()
|
|
|
@ -329,8 +354,7 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
_log.LogError(e, "Error when Processing Plex Movies {0}", info.Title);
|
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -371,8 +395,7 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
_log.LogError(e, "Error when processing Emby Movies {0}", info.Title);
|
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -527,7 +550,7 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//Log.Error(e);
|
|
|
|
_log.LogError(e, "Error when processing Plex TV {0}", t.Title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -628,7 +651,7 @@ namespace Ombi.Schedule.Jobs.Ombi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//Log.Error(e);
|
|
|
|
_log.LogError(e, "Error when processing Emby TV {0}", t.Title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
{
|
|
|
|