2014-02-14 05:31:49 +00:00
|
|
|
|
using System;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using NLog;
|
2014-03-20 07:08:15 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
2014-02-14 05:31:49 +00:00
|
|
|
|
using NzbDrone.Common.Serializer;
|
2014-07-20 11:36:31 +00:00
|
|
|
|
using NzbDrone.Core.Rest;
|
2014-02-14 05:31:49 +00:00
|
|
|
|
using NzbDrone.Core.Download.Clients.Sabnzbd.Responses;
|
|
|
|
|
using RestSharp;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Download.Clients.Sabnzbd
|
|
|
|
|
{
|
|
|
|
|
public interface ISabnzbdProxy
|
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
|
SabnzbdAddResponse DownloadNzb(byte[] nzbData, string filename, string category, int priority, SabnzbdSettings settings);
|
2014-12-23 01:17:48 +00:00
|
|
|
|
void RemoveFrom(string source, string id,bool deleteData, SabnzbdSettings settings);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
string ProcessRequest(IRestRequest restRequest, string action, SabnzbdSettings settings);
|
|
|
|
|
SabnzbdVersionResponse GetVersion(SabnzbdSettings settings);
|
2014-07-04 20:27:21 +00:00
|
|
|
|
SabnzbdConfig GetConfig(SabnzbdSettings settings);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
SabnzbdQueue GetQueue(int start, int limit, SabnzbdSettings settings);
|
2016-01-20 20:34:07 +00:00
|
|
|
|
SabnzbdHistory GetHistory(int start, int limit, string category, SabnzbdSettings settings);
|
2014-12-02 06:39:27 +00:00
|
|
|
|
string RetryDownload(string id, SabnzbdSettings settings);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SabnzbdProxy : ISabnzbdProxy
|
|
|
|
|
{
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
public SabnzbdProxy(Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
public SabnzbdAddResponse DownloadNzb(byte[] nzbData, string filename, string category, int priority, SabnzbdSettings settings)
|
2014-02-14 05:31:49 +00:00
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest(Method.POST);
|
2015-10-03 17:45:26 +00:00
|
|
|
|
var action = string.Format("mode=addfile&cat={0}&priority={1}", Uri.EscapeDataString(category), priority);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
|
2015-03-20 23:55:15 +00:00
|
|
|
|
request.AddFile("name", nzbData, filename, "application/x-nzb");
|
2014-02-14 05:31:49 +00:00
|
|
|
|
|
|
|
|
|
SabnzbdAddResponse response;
|
|
|
|
|
|
|
|
|
|
if (!Json.TryDeserialize<SabnzbdAddResponse>(ProcessRequest(request, action, settings), out response))
|
|
|
|
|
{
|
|
|
|
|
response = new SabnzbdAddResponse();
|
|
|
|
|
response.Status = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-23 01:17:48 +00:00
|
|
|
|
public void RemoveFrom(string source, string id, bool deleteData, SabnzbdSettings settings)
|
2014-02-14 05:31:49 +00:00
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest();
|
2014-12-23 01:17:48 +00:00
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
var action = string.Format("mode={0}&name=delete&del_files={1}&value={2}", source, deleteData ? 1 : 0, id);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
|
|
|
|
|
ProcessRequest(request, action, settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ProcessRequest(IRestRequest restRequest, string action, SabnzbdSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var client = BuildClient(action, settings);
|
|
|
|
|
var response = client.Execute(restRequest);
|
|
|
|
|
_logger.Trace("Response: {0}", response.Content);
|
|
|
|
|
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
|
|
|
|
|
return response.Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SabnzbdVersionResponse GetVersion(SabnzbdSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest();
|
|
|
|
|
var action = "mode=version";
|
|
|
|
|
|
|
|
|
|
SabnzbdVersionResponse response;
|
|
|
|
|
|
|
|
|
|
if (!Json.TryDeserialize<SabnzbdVersionResponse>(ProcessRequest(request, action, settings), out response))
|
|
|
|
|
{
|
|
|
|
|
response = new SabnzbdVersionResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 20:27:21 +00:00
|
|
|
|
public SabnzbdConfig GetConfig(SabnzbdSettings settings)
|
2014-02-14 05:31:49 +00:00
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest();
|
2014-07-04 20:27:21 +00:00
|
|
|
|
var action = "mode=get_config";
|
2014-02-14 05:31:49 +00:00
|
|
|
|
|
2014-07-04 20:27:21 +00:00
|
|
|
|
var response = Json.Deserialize<SabnzbdConfigResponse>(ProcessRequest(request, action, settings));
|
2014-02-14 05:31:49 +00:00
|
|
|
|
|
2014-07-04 20:27:21 +00:00
|
|
|
|
return response.Config;
|
2014-02-14 05:31:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SabnzbdQueue GetQueue(int start, int limit, SabnzbdSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest();
|
2015-10-03 17:45:26 +00:00
|
|
|
|
var action = string.Format("mode=queue&start={0}&limit={1}", start, limit);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
|
|
|
|
|
var response = ProcessRequest(request, action, settings);
|
|
|
|
|
return Json.Deserialize<SabnzbdQueue>(JObject.Parse(response).SelectToken("queue").ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 20:34:07 +00:00
|
|
|
|
public SabnzbdHistory GetHistory(int start, int limit, string category, SabnzbdSettings settings)
|
2014-02-14 05:31:49 +00:00
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest();
|
2015-10-03 17:45:26 +00:00
|
|
|
|
var action = string.Format("mode=history&start={0}&limit={1}", start, limit);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
|
2016-01-20 20:34:07 +00:00
|
|
|
|
if (category.IsNotNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
action += "&category=" + category;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-14 05:31:49 +00:00
|
|
|
|
var response = ProcessRequest(request, action, settings);
|
|
|
|
|
return Json.Deserialize<SabnzbdHistory>(JObject.Parse(response).SelectToken("history").ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-02 06:39:27 +00:00
|
|
|
|
public string RetryDownload(string id, SabnzbdSettings settings)
|
2014-04-01 20:07:41 +00:00
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest();
|
2015-10-03 17:45:26 +00:00
|
|
|
|
var action = string.Format("mode=retry&value={0}", id);
|
2014-04-01 20:07:41 +00:00
|
|
|
|
|
2014-12-02 06:39:27 +00:00
|
|
|
|
SabnzbdRetryResponse response;
|
|
|
|
|
|
|
|
|
|
if (!Json.TryDeserialize<SabnzbdRetryResponse>(ProcessRequest(request, action, settings), out response))
|
|
|
|
|
{
|
|
|
|
|
response = new SabnzbdRetryResponse();
|
|
|
|
|
response.Status = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.Id;
|
2014-04-01 20:07:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-14 05:31:49 +00:00
|
|
|
|
private IRestClient BuildClient(string action, SabnzbdSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var protocol = settings.UseSsl ? "https" : "http";
|
|
|
|
|
|
2014-03-07 06:28:06 +00:00
|
|
|
|
var authentication = settings.ApiKey.IsNullOrWhiteSpace() ?
|
2015-10-03 17:45:26 +00:00
|
|
|
|
string.Format("ma_username={0}&ma_password={1}", settings.Username, Uri.EscapeDataString(settings.Password)) :
|
|
|
|
|
string.Format("apikey={0}", settings.ApiKey);
|
2014-03-07 06:28:06 +00:00
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
var url = string.Format(@"{0}://{1}:{2}/api?{3}&{4}&output=json",
|
2014-02-14 05:31:49 +00:00
|
|
|
|
protocol,
|
|
|
|
|
settings.Host,
|
|
|
|
|
settings.Port,
|
|
|
|
|
action,
|
2014-03-07 06:28:06 +00:00
|
|
|
|
authentication);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
|
2014-06-12 21:46:08 +00:00
|
|
|
|
_logger.Debug("Url: " + url);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
|
2014-07-20 11:36:31 +00:00
|
|
|
|
return RestClientFactory.BuildClient(url);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckForError(IRestResponse response)
|
|
|
|
|
{
|
|
|
|
|
if (response.ResponseStatus != ResponseStatus.Completed)
|
|
|
|
|
{
|
2014-07-09 22:11:57 +00:00
|
|
|
|
throw new DownloadClientException("Unable to connect to SABnzbd, please check your settings", response.ErrorException);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SabnzbdJsonError result;
|
|
|
|
|
|
|
|
|
|
if (!Json.TryDeserialize<SabnzbdJsonError>(response.Content, out result))
|
|
|
|
|
{
|
|
|
|
|
//Handle plain text responses from SAB
|
|
|
|
|
result = new SabnzbdJsonError();
|
|
|
|
|
|
|
|
|
|
if (response.Content.StartsWith("error", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
result.Status = "false";
|
|
|
|
|
result.Error = response.Content.Replace("error: ", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.Status = "true";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.Error = response.Content.Replace("error: ", "");
|
|
|
|
|
}
|
2014-04-19 15:09:22 +00:00
|
|
|
|
|
2014-02-14 05:31:49 +00:00
|
|
|
|
if (result.Failed)
|
2014-02-27 06:06:24 +00:00
|
|
|
|
throw new DownloadClientException("Error response received from SABnzbd: {0}", result.Error);
|
2014-02-14 05:31:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|