From e34170f55aa35da6471b27c6d24c8a0fac75a496 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sat, 19 Mar 2016 00:16:44 +0000 Subject: [PATCH] Added the option to set a CP quality #38 --- .../ICouchPotatoApi.cs | 3 +- .../Movie/CouchPotatoProfiles.cs | 53 ++++++++++++++ .../PlexRequests.Api.Models.csproj | 1 + PlexRequests.Api/CouchPotatoApi.cs | 26 ++++++- .../SettingModels/CouchPotatoSettings.cs | 1 + PlexRequests.UI/Modules/AdminModule.cs | 14 +++- PlexRequests.UI/Modules/ApprovalModule.cs | 4 +- PlexRequests.UI/Modules/SearchModule.cs | 2 +- .../Views/Admin/CouchPotato.cshtml | 71 ++++++++++++++++++- 9 files changed, 166 insertions(+), 9 deletions(-) create mode 100644 PlexRequests.Api.Models/Movie/CouchPotatoProfiles.cs diff --git a/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs b/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs index bcfdb3dfd..288f09d67 100644 --- a/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs +++ b/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs @@ -33,7 +33,8 @@ namespace PlexRequests.Api.Interfaces { public interface ICouchPotatoApi { - bool AddMovie(string imdbid, string apiKey, string title, Uri baseUrl); + bool AddMovie(string imdbid, string apiKey, string title, Uri baseUrl, string profileID = default(string)); CouchPotatoStatus GetStatus(Uri url, string apiKey); + CouchPotatoProfiles GetProfiles(Uri url, string apiKey); } } \ No newline at end of file diff --git a/PlexRequests.Api.Models/Movie/CouchPotatoProfiles.cs b/PlexRequests.Api.Models/Movie/CouchPotatoProfiles.cs new file mode 100644 index 000000000..f63661ecb --- /dev/null +++ b/PlexRequests.Api.Models/Movie/CouchPotatoProfiles.cs @@ -0,0 +1,53 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: CouchPotatoProfiles.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System.Collections.Generic; + +namespace PlexRequests.Api.Models.Movie +{ + public class ProfileList + { + public bool core { get; set; } + public string _rev { get; set; } + public List finish { get; set; } + public List qualities { get; set; } + public string _id { get; set; } + public string _t { get; set; } + public string label { get; set; } + public int minimum_score { get; set; } + public List stop_after { get; set; } + public List wait_for { get; set; } + public int order { get; set; } + public List __invalid_name__3d { get; set; } + } + + public class CouchPotatoProfiles + { + public List list { get; set; } + public bool success { get; set; } + } +} \ No newline at end of file diff --git a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj index 402cdd22a..1bdc4ffe9 100644 --- a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj +++ b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj @@ -46,6 +46,7 @@ + diff --git a/PlexRequests.Api/CouchPotatoApi.cs b/PlexRequests.Api/CouchPotatoApi.cs index bdb238ee2..7e660a38d 100644 --- a/PlexRequests.Api/CouchPotatoApi.cs +++ b/PlexRequests.Api/CouchPotatoApi.cs @@ -45,9 +45,17 @@ public CouchPotatoApi() private ApiRequest Api { get; set; } private static Logger Log = LogManager.GetCurrentClassLogger(); - public bool AddMovie(string imdbid, string apiKey, string title, Uri baseUrl) + public bool AddMovie(string imdbid, string apiKey, string title, Uri baseUrl, string profileId = default(string)) { - var request = new RestRequest { Resource = "/api/{apikey}/movie.add?title={title}&identifier={imdbid}" }; + RestRequest request; + request = string.IsNullOrEmpty(profileId) + ? new RestRequest {Resource = "/api/{apikey}/movie.add?title={title}&identifier={imdbid}"} + : new RestRequest { Resource = "/api/{apikey}/movie.add?title={title}&identifier={imdbid}&profile_id={profileId}" }; + + if (!string.IsNullOrEmpty(profileId)) + { + request.AddUrlSegment("profileId", profileId); + } request.AddUrlSegment("apikey", apiKey); request.AddUrlSegment("imdbid", imdbid); @@ -93,5 +101,19 @@ public CouchPotatoStatus GetStatus(Uri url, string apiKey) return Api.Execute(request,url); } + + public CouchPotatoProfiles GetProfiles(Uri url, string apiKey) + { + Log.Trace("Getting CP Profiles, ApiKey = {0}", apiKey); + var request = new RestRequest + { + Resource = "api/{apikey}/profile.list/", + Method = Method.GET + }; + + request.AddUrlSegment("apikey", apiKey); + + return Api.Execute(request, url); + } } } \ No newline at end of file diff --git a/PlexRequests.Core/SettingModels/CouchPotatoSettings.cs b/PlexRequests.Core/SettingModels/CouchPotatoSettings.cs index 0c4ccd06f..7c18ca5d7 100644 --- a/PlexRequests.Core/SettingModels/CouchPotatoSettings.cs +++ b/PlexRequests.Core/SettingModels/CouchPotatoSettings.cs @@ -37,6 +37,7 @@ public class CouchPotatoSettings : Settings public int Port { get; set; } public string ApiKey { get; set; } public bool Ssl { get; set; } + public string ProfileId { get; set; } [JsonIgnore] public Uri FullUri diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index a41e6c967..fc86ad568 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -59,6 +59,7 @@ public class AdminModule : NancyModule private IPlexApi PlexApi { get; } private ISonarrApi SonarrApi { get; } private PushbulletApi PushbulletApi { get; } + private ICouchPotatoApi CpApi { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); public AdminModule(ISettingsService rpService, @@ -70,7 +71,8 @@ public class AdminModule : NancyModule ISettingsService email, IPlexApi plexApi, ISettingsService pbSettings, - PushbulletApi pbApi) : base("admin") + PushbulletApi pbApi, + ICouchPotatoApi cpApi) : base("admin") { RpService = rpService; CpService = cpService; @@ -82,6 +84,7 @@ public class AdminModule : NancyModule PlexApi = plexApi; PushbulletService = pbSettings; PushbulletApi = pbApi; + CpApi = cpApi; #if !DEBUG this.RequiresAuthentication(); @@ -107,6 +110,7 @@ public class AdminModule : NancyModule Post["/sonarr"] = _ => SaveSonarr(); Post["/sonarrprofiles"] = _ => GetSonarrQualityProfiles(); + Post["/cpprofiles"] = _ => GetCpProfiles(); Get["/emailnotification"] = _ => EmailNotifications(); Post["/emailnotification"] = _ => SaveEmailNotifications(); @@ -354,5 +358,13 @@ private Response SavePushbulletNotifications() ? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for Pushbullet Notifications!" } : new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." }); } + + private Response GetCpProfiles() + { + var settings = this.Bind(); + var profiles = CpApi.GetProfiles(settings.FullUri, settings.ApiKey); + + return Response.AsJson(profiles); + } } } \ No newline at end of file diff --git a/PlexRequests.UI/Modules/ApprovalModule.cs b/PlexRequests.UI/Modules/ApprovalModule.cs index ca3a4bcdb..74a26a6a2 100644 --- a/PlexRequests.UI/Modules/ApprovalModule.cs +++ b/PlexRequests.UI/Modules/ApprovalModule.cs @@ -127,7 +127,7 @@ private Response RequestMovieAndUpdateStatus(RequestedModel request) var cpSettings = CpService.GetSettings(); var cp = new CouchPotatoApi(); Log.Info("Adding movie to CP : {0}", request.Title); - var result = cp.AddMovie(request.ImdbId, cpSettings.ApiKey, request.Title, cpSettings.FullUri); + var result = cp.AddMovie(request.ImdbId, cpSettings.ApiKey, request.Title, cpSettings.FullUri, cpSettings.ProfileId); Log.Trace("Adding movie to CP result {0}", result); if (result) { @@ -212,7 +212,7 @@ private Response ApproveAll() private bool SendMovie(CouchPotatoSettings settings, RequestedModel r, ICouchPotatoApi cp) { Log.Info("Adding movie to CP : {0}", r.Title); - var result = cp.AddMovie(r.ImdbId, settings.ApiKey, r.Title, settings.FullUri); + var result = cp.AddMovie(r.ImdbId, settings.ApiKey, r.Title, settings.FullUri, settings.ProfileId); Log.Trace("Adding movie to CP result {0}", result); return result; } diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 353a99c73..f56ac9b40 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -215,7 +215,7 @@ private Response RequestMovie(int movieId) if (!settings.RequireApproval) { Log.Info("Adding movie to CP (No approval required)"); - var result = CouchPotatoApi.AddMovie(model.ImdbId, cpSettings.ApiKey, model.Title, cpSettings.FullUri); + var result = CouchPotatoApi.AddMovie(model.ImdbId, cpSettings.ApiKey, model.Title, cpSettings.FullUri,cpSettings.ProfileId); Log.Debug("Adding movie to CP result {0}", result); if (result) { diff --git a/PlexRequests.UI/Views/Admin/CouchPotato.cshtml b/PlexRequests.UI/Views/Admin/CouchPotato.cshtml index 0b967436b..4be47db0a 100644 --- a/PlexRequests.UI/Views/Admin/CouchPotato.cshtml +++ b/PlexRequests.UI/Views/Admin/CouchPotato.cshtml @@ -52,6 +52,20 @@ + +
+
+ +
+
+
+ +
+ +
+
+
+
@@ -75,9 +89,58 @@ \ No newline at end of file