|
|
@ -37,6 +37,7 @@ using PlexRequests.Core.SettingModels;
|
|
|
|
using PlexRequests.Helpers;
|
|
|
|
using PlexRequests.Helpers;
|
|
|
|
using PlexRequests.Services.Interfaces;
|
|
|
|
using PlexRequests.Services.Interfaces;
|
|
|
|
using PlexRequests.Services.Models;
|
|
|
|
using PlexRequests.Services.Models;
|
|
|
|
|
|
|
|
using PlexRequests.Services.Notification;
|
|
|
|
using PlexRequests.Store;
|
|
|
|
using PlexRequests.Store;
|
|
|
|
|
|
|
|
|
|
|
|
using Quartz;
|
|
|
|
using Quartz;
|
|
|
@ -45,13 +46,15 @@ namespace PlexRequests.Services.Jobs
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class PlexAvailabilityChecker : IJob, IAvailabilityChecker
|
|
|
|
public class PlexAvailabilityChecker : IJob, IAvailabilityChecker
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public PlexAvailabilityChecker(ISettingsService<PlexSettings> plexSettings, ISettingsService<AuthenticationSettings> auth, IRequestService request, IPlexApi plex, ICacheProvider cache)
|
|
|
|
public PlexAvailabilityChecker(ISettingsService<PlexSettings> plexSettings, ISettingsService<AuthenticationSettings> auth, IRequestService request, IPlexApi plex, ICacheProvider cache,
|
|
|
|
|
|
|
|
INotificationService notify)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Plex = plexSettings;
|
|
|
|
Plex = plexSettings;
|
|
|
|
Auth = auth;
|
|
|
|
Auth = auth;
|
|
|
|
RequestService = request;
|
|
|
|
RequestService = request;
|
|
|
|
PlexApi = plex;
|
|
|
|
PlexApi = plex;
|
|
|
|
Cache = cache;
|
|
|
|
Cache = cache;
|
|
|
|
|
|
|
|
Notification = notify;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ISettingsService<PlexSettings> Plex { get; }
|
|
|
|
private ISettingsService<PlexSettings> Plex { get; }
|
|
|
@ -60,6 +63,7 @@ namespace PlexRequests.Services.Jobs
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private IPlexApi PlexApi { get; }
|
|
|
|
private IPlexApi PlexApi { get; }
|
|
|
|
private ICacheProvider Cache { get; }
|
|
|
|
private ICacheProvider Cache { get; }
|
|
|
|
|
|
|
|
private INotificationService Notification { get; }
|
|
|
|
|
|
|
|
|
|
|
|
public void CheckAndUpdateAll()
|
|
|
|
public void CheckAndUpdateAll()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -101,16 +105,6 @@ namespace PlexRequests.Services.Jobs
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Log.Trace("We are going to see if Plex has the following title: {0}", r.Title);
|
|
|
|
Log.Trace("We are going to see if Plex has the following title: {0}", r.Title);
|
|
|
|
|
|
|
|
|
|
|
|
if (libraries == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
libraries = new List<PlexSearch> { PlexApi.SearchContent(authSettings.PlexAuthToken, r.Title, plexSettings.FullUri) };
|
|
|
|
|
|
|
|
if (libraries.Count == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Trace("Could not find any matching result for this title.");
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.Trace("Search results from Plex for the following request: {0}", r.Title);
|
|
|
|
Log.Trace("Search results from Plex for the following request: {0}", r.Title);
|
|
|
|
|
|
|
|
|
|
|
|
var releaseDate = r.ReleaseDate == DateTime.MinValue ? string.Empty : r.ReleaseDate.ToString("yyyy");
|
|
|
|
var releaseDate = r.ReleaseDate == DateTime.MinValue ? string.Empty : r.ReleaseDate.ToString("yyyy");
|
|
|
@ -146,8 +140,10 @@ namespace PlexRequests.Services.Jobs
|
|
|
|
|
|
|
|
|
|
|
|
if (modifiedModel.Any())
|
|
|
|
if (modifiedModel.Any())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
NotifyUsers(modifiedModel, authSettings.PlexAuthToken);
|
|
|
|
RequestService.BatchUpdate(modifiedModel);
|
|
|
|
RequestService.BatchUpdate(modifiedModel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<PlexMovie> GetPlexMovies()
|
|
|
|
public List<PlexMovie> GetPlexMovies()
|
|
|
@ -271,7 +267,8 @@ namespace PlexRequests.Services.Jobs
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Log.Trace("Plex Lib GetSet Call");
|
|
|
|
Log.Trace("Plex Lib GetSet Call");
|
|
|
|
results = Cache.GetOrSet(CacheKeys.PlexLibaries, () => {
|
|
|
|
results = Cache.GetOrSet(CacheKeys.PlexLibaries, () =>
|
|
|
|
|
|
|
|
{
|
|
|
|
Log.Trace("Plex Lib API Call (inside getset)");
|
|
|
|
Log.Trace("Plex Lib API Call (inside getset)");
|
|
|
|
return GetLibraries(authSettings, plexSettings);
|
|
|
|
return GetLibraries(authSettings, plexSettings);
|
|
|
|
}, CacheKeys.TimeFrameMinutes.SchedulerCaching);
|
|
|
|
}, CacheKeys.TimeFrameMinutes.SchedulerCaching);
|
|
|
@ -317,6 +314,46 @@ namespace PlexRequests.Services.Jobs
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void NotifyUsers(IEnumerable<RequestedModel> modelChanged, string apiKey)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var plexUser = PlexApi.GetUsers(apiKey);
|
|
|
|
|
|
|
|
if (plexUser?.User == null || plexUser.User.Length == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var model in modelChanged)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var usersToNotify = model.UsersToNotify; // Users that selected the notification button when requesting a movie/tv show
|
|
|
|
|
|
|
|
foreach (var user in usersToNotify)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var email = plexUser.User.FirstOrDefault(x => x.Username == user);
|
|
|
|
|
|
|
|
if (email == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// We do not have a plex user that requested this!
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var notificationModel = new NotificationModel
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
User = email.Username,
|
|
|
|
|
|
|
|
UserEmail = email.Email,
|
|
|
|
|
|
|
|
NotificationType = NotificationType.RequestAvailable,
|
|
|
|
|
|
|
|
Title = model.Title
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send the notification to the user.
|
|
|
|
|
|
|
|
Notification.Publish(notificationModel);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Error(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Execute(IJobExecutionContext context)
|
|
|
|
public void Execute(IJobExecutionContext context)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
CheckAndUpdateAll();
|
|
|
|
CheckAndUpdateAll();
|
|
|
|