diff --git a/PlexRequests.UI/Helpers/HeadphonesSender.cs b/PlexRequests.Core/HeadphonesSender.cs similarity index 99% rename from PlexRequests.UI/Helpers/HeadphonesSender.cs rename to PlexRequests.Core/HeadphonesSender.cs index 29c428612..19e438be2 100644 --- a/PlexRequests.UI/Helpers/HeadphonesSender.cs +++ b/PlexRequests.Core/HeadphonesSender.cs @@ -24,18 +24,16 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion + using System.Linq; using System.Threading; using System.Threading.Tasks; - using NLog; - using PlexRequests.Api.Interfaces; -using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.Store; -namespace PlexRequests.UI.Helpers +namespace PlexRequests.Core { public class HeadphonesSender { diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 17bb7ceca..506ac3a62 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -90,6 +90,7 @@ + diff --git a/PlexRequests.Services/Jobs/FaultQueueHandler.cs b/PlexRequests.Services/Jobs/FaultQueueHandler.cs index 0a540d5ad..9238a12ec 100644 --- a/PlexRequests.Services/Jobs/FaultQueueHandler.cs +++ b/PlexRequests.Services/Jobs/FaultQueueHandler.cs @@ -51,32 +51,43 @@ namespace PlexRequests.Services.Jobs private static readonly Logger Log = LogManager.GetCurrentClassLogger(); public FaultQueueHandler(IJobRecord record, IRepository repo, ISonarrApi sonarrApi, - ISickRageApi srApi, - ISettingsService sonarrSettings, ISettingsService srSettings) + ISickRageApi srApi, ISettingsService sonarrSettings, ISettingsService srSettings, + ICouchPotatoApi cpApi, ISettingsService cpsettings, IRequestService requestService, + ISettingsService hpSettings, IHeadphonesApi headphonesApi) { Record = record; Repo = repo; SonarrApi = sonarrApi; SrApi = srApi; + CpApi = cpApi; + HpApi = headphonesApi; + + RequestService = requestService; + SickrageSettings = srSettings; SonarrSettings = sonarrSettings; + CpSettings = cpsettings; + HeadphoneSettings = hpSettings; } private IRepository Repo { get; } private IJobRecord Record { get; } private ISonarrApi SonarrApi { get; } private ISickRageApi SrApi { get; } - private ISettingsService SonarrSettings { get; set; } - private ISettingsService SickrageSettings { get; set; } - + private ICouchPotatoApi CpApi { get; } + private IHeadphonesApi HpApi { get; } + private IRequestService RequestService { get; } + private ISettingsService SonarrSettings { get; } + private ISettingsService SickrageSettings { get; } + private ISettingsService CpSettings { get; } + private ISettingsService HeadphoneSettings { get; } public void Execute(IJobExecutionContext context) { try { var faultedRequests = Repo.GetAll().ToList(); - - + var missingInfo = faultedRequests.Where(x => x.FaultType == FaultType.MissingInformation).ToList(); ProcessMissingInformation(missingInfo); @@ -90,7 +101,7 @@ namespace PlexRequests.Services.Jobs } finally { - Record.Record(JobNames.RequestLimitReset); + Record.Record(JobNames.FaultQueueHandler); } } @@ -163,9 +174,66 @@ namespace PlexRequests.Services.Jobs // Couldn't send it return false; } + + // Approve it + tvModel.Approved = true; + RequestService.UpdateRequest(tvModel); return true; } + return false; + } + catch (Exception e) + { + Log.Error(e); + return false; // It fails so it will get added back into the queue + } + } + + private bool ProcessMovies(RequestedModel model, CouchPotatoSettings cp) + { + try + { + if (cp.Enabled) + { + var result = CpApi.AddMovie(model.ImdbId, cp.ApiKey, model.Title, + cp.FullUri, cp.ProfileId); + + if (result) + { + // Approve it now + model.Approved = true; + RequestService.UpdateRequest(model); + }; + return result; + } + return false; + } + catch (Exception e) + { + Log.Error(e); + return false; // It fails so it will get added back into the queue + } + } + + private bool ProcessAlbums(RequestedModel model, HeadphonesSettings hp) + { + try + { + if (hp.Enabled) + { + var sender = new HeadphonesSender(HpApi, hp, RequestService); + var result = sender.AddAlbum(model).Result; + + if (result) + { + // Approve it now + model.Approved = true; + RequestService.UpdateRequest(model); + }; + + return result; + } return false; } catch (Exception e) @@ -179,36 +247,45 @@ namespace PlexRequests.Services.Jobs { var sonarrSettings = SonarrSettings.GetSettings(); var sickrageSettings = SickrageSettings.GetSettings(); + var cpSettings = CpSettings.GetSettings(); + var hpSettings = HeadphoneSettings.GetSettings(); if (!requests.Any()) { return; } - var tv = requests.Where(x => x.Type == RequestType.TvShow); - var movie = requests.Where(x => x.Type == RequestType.Movie); - var album = requests.Where(x => x.Type == RequestType.Album); - - - foreach (var t in tv) + foreach (var request in requests) { - var tvModel = ByteConverterHelper.ReturnObject(t.Content); - var result = ProcessTvShow(tvModel, sonarrSettings, sickrageSettings); + var model = ByteConverterHelper.ReturnObject(request.Content); + var result = false; + switch (request.Type) + { + case RequestType.Movie: + result = ProcessMovies(model, cpSettings); + break; + case RequestType.TvShow: + result = ProcessTvShow(model, sonarrSettings, sickrageSettings); + break; + case RequestType.Album: + result = ProcessAlbums(model, hpSettings); + break; + default: + throw new ArgumentOutOfRangeException(); + } if (!result) { // we now have the info but couldn't add it, so do nothing now. - t.LastRetry = DateTime.UtcNow; - Repo.Update(t); + request.LastRetry = DateTime.UtcNow; + Repo.Update(request); } else { // Successful, remove from the fault queue - Repo.Delete(t); + Repo.Delete(request); } } - - } } } \ No newline at end of file diff --git a/PlexRequests.Services/Jobs/JobNames.cs b/PlexRequests.Services/Jobs/JobNames.cs index 77f177713..0f8b4c96e 100644 --- a/PlexRequests.Services/Jobs/JobNames.cs +++ b/PlexRequests.Services/Jobs/JobNames.cs @@ -37,5 +37,6 @@ namespace PlexRequests.Services.Jobs public const string RequestLimitReset = "Request Limit Reset"; public const string EpisodeCacher = "Plex Episode Cacher"; public const string RecentlyAddedEmail = "Recently Added Email Notification"; + public const string FaultQueueHandler = "Request Fault Queue Handler"; } } \ No newline at end of file diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index fcd82d964..1e0e4c490 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -909,9 +909,7 @@ namespace PlexRequests.UI.Modules var result = sender.SendToSickRage(srSettings, model); if (result?.result == "success") { - return - await - AddRequest(model, settings, + return await AddRequest(model, settings, $"{fullShowName} {Resources.UI.Search_SuccessfullyAdded}"); } return @@ -924,8 +922,7 @@ namespace PlexRequests.UI.Modules if (!srSettings.Enabled && !s.Enabled) { - return - await AddRequest(model, settings, $"{fullShowName} {Resources.UI.Search_SuccessfullyAdded}"); + return await AddRequest(model, settings, $"{fullShowName} {Resources.UI.Search_SuccessfullyAdded}"); } return @@ -1102,7 +1099,7 @@ namespace PlexRequests.UI.Modules catch (Exception e) { Log.Error(e); - await FaultQueue.QueueItemAsync(model, albumInfo.id, RequestType.Movie, FaultType.RequestFault); + await FaultQueue.QueueItemAsync(model, albumInfo.id, RequestType.Album, FaultType.RequestFault); await NotificationService.Publish(new NotificationModel { diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index 949f29117..75ecd4701 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -210,7 +210,6 @@ -