Skip to content

Commit

Permalink
Added the option to set a CP quality #38
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Mar 19, 2016
1 parent 4136116 commit e34170f
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 9 deletions.
3 changes: 2 additions & 1 deletion PlexRequests.Api.Interfaces/ICouchPotatoApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
53 changes: 53 additions & 0 deletions PlexRequests.Api.Models/Movie/CouchPotatoProfiles.cs
Original file line number Diff line number Diff line change
@@ -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<bool> finish { get; set; }
public List<string> 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<int> stop_after { get; set; }
public List<int> wait_for { get; set; }
public int order { get; set; }
public List<object> __invalid_name__3d { get; set; }
}

public class CouchPotatoProfiles
{
public List<ProfileList> list { get; set; }
public bool success { get; set; }
}
}
1 change: 1 addition & 0 deletions PlexRequests.Api.Models/PlexRequests.Api.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Movie\CouchPotatoAdd.cs" />
<Compile Include="Movie\CouchPotatoProfiles.cs" />
<Compile Include="Movie\CouchPotatoStatus.cs" />
<Compile Include="Notifications\PushbulletPush.cs" />
<Compile Include="Notifications\PushbulletResponse.cs" />
Expand Down
26 changes: 24 additions & 2 deletions PlexRequests.Api/CouchPotatoApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -93,5 +101,19 @@ public CouchPotatoStatus GetStatus(Uri url, string apiKey)

return Api.Execute<CouchPotatoStatus>(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<CouchPotatoProfiles>(request, url);
}
}
}
1 change: 1 addition & 0 deletions PlexRequests.Core/SettingModels/CouchPotatoSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion PlexRequests.UI/Modules/AdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlexRequestSettings> rpService,
Expand All @@ -70,7 +71,8 @@ public class AdminModule : NancyModule
ISettingsService<EmailNotificationSettings> email,
IPlexApi plexApi,
ISettingsService<PushbulletNotificationSettings> pbSettings,
PushbulletApi pbApi) : base("admin")
PushbulletApi pbApi,
ICouchPotatoApi cpApi) : base("admin")
{
RpService = rpService;
CpService = cpService;
Expand All @@ -82,6 +84,7 @@ public class AdminModule : NancyModule
PlexApi = plexApi;
PushbulletService = pbSettings;
PushbulletApi = pbApi;
CpApi = cpApi;

#if !DEBUG
this.RequiresAuthentication();
Expand All @@ -107,6 +110,7 @@ public class AdminModule : NancyModule
Post["/sonarr"] = _ => SaveSonarr();

Post["/sonarrprofiles"] = _ => GetSonarrQualityProfiles();
Post["/cpprofiles"] = _ => GetCpProfiles();

Get["/emailnotification"] = _ => EmailNotifications();
Post["/emailnotification"] = _ => SaveEmailNotifications();
Expand Down Expand Up @@ -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<CouchPotatoSettings>();
var profiles = CpApi.GetProfiles(settings.FullUri, settings.ApiKey);

return Response.AsJson(profiles);
}
}
}
4 changes: 2 additions & 2 deletions PlexRequests.UI/Modules/ApprovalModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion PlexRequests.UI/Modules/SearchModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
71 changes: 69 additions & 2 deletions PlexRequests.UI/Views/Admin/CouchPotato.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@
</div>
</div>


<div class="form-group">
<div>
<button type="submit" id="getProfiles" class="btn btn-primary-outline">Get Quality Profiles</button>
</div>
</div>
<div class="form-group">
<label for="select" class="control-label">Quality Profiles</label>
<div id="profiles">
<select class="form-control" id="select"></select>
</div>
</div>
<br/>

<div class="form-group">
<div>
<button id="testCp" type="submit" class="btn btn-primary-outline">Test Connectivity</button>
Expand All @@ -75,9 +89,58 @@
<script>
$(function() {
@if (!string.IsNullOrEmpty(Model.ProfileId))
{
<text>
var qualitySelected = '@Model.ProfileId';
var $form = $("#mainForm");
$.ajax({
type: $form.prop("method"),
data: $form.serialize(),
url: "cpprofiles",
dataType: "json",
success: function(response) {
response.list.forEach(function(result) {
if (result._id == qualitySelected) {
$("#select").append("<option selected='selected' value='" + result._id + "'>" + result.label + "</option>");
} else {
$("#select").append("<option value='" + result._id + "'>" + result.label + "</option>");
}
});
},
error: function(e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});
</text>
}
$('#getProfiles').click(function (e) {
e.preventDefault();
var $form = $("#mainForm");
$.ajax({
type: $form.prop("method"),
data: $form.serialize(),
url: "cpprofiles",
dataType: "json",
success: function (response) {
response.list.forEach(function (result) {
$("#select").append("<option value='" + result._id + "'>" + result.label + "</option>");
});
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});
});
$('#testCp').click(function (e) {
e.preventDefault();
var $form = $("#mainForm");
$.ajax({
type: $form.prop("method"),
url: "/test/cp",
Expand Down Expand Up @@ -107,9 +170,13 @@
return;
}
var $form = $("#mainForm");
var qualityProfile = $("#profiles option:selected").val();
var data = $form.serialize();
data = data + "&profileId=" + qualityProfile;
$.ajax({
type: $form.prop("method"),
data: $form.serialize(),
data: data,
url: $form.prop("action"),
dataType: "json",
success: function (response) {
Expand All @@ -125,6 +192,6 @@
}
});
});
});
</script>

0 comments on commit e34170f

Please sign in to comment.