New: Hadouken torrent client support

This commit is contained in:
Mark McDowall 2016-06-02 21:14:01 -07:00
parent d81e03fcc0
commit 902d6929c0
10 changed files with 76 additions and 114 deletions

View File

@ -54,6 +54,11 @@ namespace NzbDrone.Core.Download.Clients.Hadouken
foreach (var torrent in torrents) foreach (var torrent in torrents)
{ {
if (Settings.Category.IsNotNullOrWhiteSpace() && torrent.Label != Settings.Category)
{
continue;
}
var outputPath = _remotePathMappingService.RemapRemoteToLocal(Settings.Host, new OsPath(torrent.SavePath)); var outputPath = _remotePathMappingService.RemapRemoteToLocal(Settings.Host, new OsPath(torrent.SavePath));
var eta = TimeSpan.FromSeconds(0); var eta = TimeSpan.FromSeconds(0);

View File

@ -1,8 +0,0 @@
namespace NzbDrone.Core.Download.Clients.Hadouken
{
public class HadoukenError
{
public int Code { get; set; }
public string Message { get; set; }
}
}

View File

@ -1,48 +1,61 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Net;
using NLog; using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Download.Clients.Hadouken.Models; using NzbDrone.Core.Download.Clients.Hadouken.Models;
using NzbDrone.Core.Rest;
using RestSharp;
namespace NzbDrone.Core.Download.Clients.Hadouken namespace NzbDrone.Core.Download.Clients.Hadouken
{ {
public interface IHadoukenProxy
{
HadoukenSystemInfo GetSystemInfo(HadoukenSettings settings);
HadoukenTorrent[] GetTorrents(HadoukenSettings settings);
IDictionary<string, object> GetConfig(HadoukenSettings settings);
string AddTorrentFile(HadoukenSettings settings, byte[] fileContent);
void AddTorrentUri(HadoukenSettings settings, string torrentUrl);
void RemoveTorrent(HadoukenSettings settings, string downloadId);
void RemoveTorrentAndData(HadoukenSettings settings, string downloadId);
}
public class HadoukenProxy : IHadoukenProxy public class HadoukenProxy : IHadoukenProxy
{ {
private static int _callId; private static int _callId;
private readonly IHttpClient _httpClient;
private readonly Logger _logger; private readonly Logger _logger;
public HadoukenProxy(Logger logger) public HadoukenProxy(IHttpClient httpClient, Logger logger)
{ {
_httpClient = httpClient;
_logger = logger; _logger = logger;
} }
public HadoukenSystemInfo GetSystemInfo(HadoukenSettings settings) public HadoukenSystemInfo GetSystemInfo(HadoukenSettings settings)
{ {
return ProcessRequest<HadoukenSystemInfo>(settings, "core.getSystemInfo").Result; return ProcessRequest<HadoukenSystemInfo>(settings, "core.getSystemInfo");
} }
public HadoukenTorrent[] GetTorrents(HadoukenSettings settings) public HadoukenTorrent[] GetTorrents(HadoukenSettings settings)
{ {
var result = ProcessRequest<HadoukenResponseResult>(settings, "webui.list").Result; var result = ProcessRequest<HadoukenTorrentResponse>(settings, "webui.list");
return GetTorrents(result.Torrents); return GetTorrents(result.Torrents);
} }
public IDictionary<string, object> GetConfig(HadoukenSettings settings) public IDictionary<string, object> GetConfig(HadoukenSettings settings)
{ {
return ProcessRequest<IDictionary<string, object>>(settings, "webui.getSettings").Result; return ProcessRequest<IDictionary<string, object>>(settings, "webui.getSettings");
} }
public string AddTorrentFile(HadoukenSettings settings, byte[] fileContent) public string AddTorrentFile(HadoukenSettings settings, byte[] fileContent)
{ {
return ProcessRequest<string>(settings, "webui.addTorrent", "file", Convert.ToBase64String(fileContent)).Result; return ProcessRequest<string>(settings, "webui.addTorrent", "file", Convert.ToBase64String(fileContent), new { label = settings.Category });
} }
public void AddTorrentUri(HadoukenSettings settings, string torrentUrl) public void AddTorrentUri(HadoukenSettings settings, string torrentUrl)
{ {
ProcessRequest<string>(settings, "webui.addTorrent", "url", torrentUrl); ProcessRequest<string>(settings, "webui.addTorrent", "url", torrentUrl, new { label = settings.Category });
} }
public void RemoveTorrent(HadoukenSettings settings, string downloadId) public void RemoveTorrent(HadoukenSettings settings, string downloadId)
@ -55,53 +68,24 @@ namespace NzbDrone.Core.Download.Clients.Hadouken
ProcessRequest<bool>(settings, "webui.perform", "removedata", new string[] { downloadId }); ProcessRequest<bool>(settings, "webui.perform", "removedata", new string[] { downloadId });
} }
private HadoukenResponse<TResult> ProcessRequest<TResult>(HadoukenSettings settings, string method, params object[] parameters) private T ProcessRequest<T>(HadoukenSettings settings, string method, params object[] parameters)
{ {
var client = BuildClient(settings); var baseUrl = HttpRequestBuilder.BuildBaseUrl(settings.UseSsl, settings.Host, settings.Port, "api");
return ProcessRequest<TResult>(client, method, parameters); var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters);
} requestBuilder.LogResponseContent = true;
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password);
requestBuilder.Headers.Add("Accept-Encoding", "gzip,deflate");
private HadoukenResponse<TResult> ProcessRequest<TResult>(IRestClient client, string method, params object[] parameters) var httpRequest = requestBuilder.Build();
{ var response = _httpClient.Execute(httpRequest);
var request = new RestRequest(Method.POST); var result = Json.Deserialize<JsonRpcResponse<T>>(response.Content);
request.Resource = "api";
request.RequestFormat = DataFormat.Json;
request.AddHeader("Accept-Encoding", "gzip,deflate");
var data = new Dictionary<String, Object>(); if (result.Error != null)
data.Add("id", GetCallId());
data.Add("method", method);
if (parameters != null)
{ {
data.Add("params", parameters); throw new DownloadClientException("Error response received from Hadouken: {0}", result.Error.ToString());
} }
request.AddBody(data); return result.Result;
_logger.Debug("Url: {0} Method: {1}", client.BuildUri(request), method);
return client.ExecuteAndValidate<HadoukenResponse<TResult>>(request);
}
private IRestClient BuildClient(HadoukenSettings settings)
{
var protocol = settings.UseSsl ? "https" : "http";
var url = string.Format(@"{0}://{1}:{2}", protocol, settings.Host, settings.Port);
var restClient = RestClientFactory.BuildClient(url);
restClient.Timeout = 4000;
var basicData = Encoding.UTF8.GetBytes(string.Format("{0}:{1}", settings.Username, settings.Password));
var basicHeader = Convert.ToBase64String(basicData);
restClient.AddDefaultHeader("Authorization", string.Format("Basic {0}", basicHeader));
return restClient;
}
private int GetCallId()
{
return System.Threading.Interlocked.Increment(ref _callId);
} }
private HadoukenTorrent[] GetTorrents(object[][] torrentsRaw) private HadoukenTorrent[] GetTorrents(object[][] torrentsRaw)
@ -141,6 +125,7 @@ namespace NzbDrone.Core.Download.Clients.Hadouken
Progress = Convert.ToDouble(item[4]), Progress = Convert.ToDouble(item[4]),
DownloadedBytes = Convert.ToInt64(item[5]), DownloadedBytes = Convert.ToInt64(item[5]),
DownloadRate = Convert.ToInt64(item[9]), DownloadRate = Convert.ToInt64(item[9]),
Label = Convert.ToString(item[11]),
Error = Convert.ToString(item[21]), Error = Convert.ToString(item[21]),
SavePath = Convert.ToString(item[26]) SavePath = Convert.ToString(item[26])
}; };

View File

@ -1,14 +0,0 @@
namespace NzbDrone.Core.Download.Clients.Hadouken
{
public class HadoukenResponse<TResult>
{
public int Id { get; set; }
public TResult Result { get; set; }
public HadoukenError Error { get; set; }
}
public class HadoukenResponseResult
{
public object[][] Torrents { get; set; }
}
}

View File

@ -1,9 +1,25 @@
using NzbDrone.Core.Annotations; using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation; using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download.Clients.Hadouken namespace NzbDrone.Core.Download.Clients.Hadouken
{ {
public class HadoukenSettingsValidator : AbstractValidator<HadoukenSettings>
{
public HadoukenSettingsValidator()
{
RuleFor(c => c.Host).ValidHost();
RuleFor(c => c.Port).GreaterThan(0);
RuleFor(c => c.Username).NotEmpty()
.WithMessage("Username must not be empty.");
RuleFor(c => c.Password).NotEmpty()
.WithMessage("Password must not be empty.");
}
}
public class HadoukenSettings : IProviderConfig public class HadoukenSettings : IProviderConfig
{ {
private static readonly HadoukenSettingsValidator Validator = new HadoukenSettingsValidator(); private static readonly HadoukenSettingsValidator Validator = new HadoukenSettingsValidator();
@ -12,6 +28,7 @@ namespace NzbDrone.Core.Download.Clients.Hadouken
{ {
Host = "localhost"; Host = "localhost";
Port = 7070; Port = 7070;
Category = "sonarr-tv";
} }
[FieldDefinition(0, Label = "Host", Type = FieldType.Textbox)] [FieldDefinition(0, Label = "Host", Type = FieldType.Textbox)]
@ -26,7 +43,10 @@ namespace NzbDrone.Core.Download.Clients.Hadouken
[FieldDefinition(3, Label = "Password", Type = FieldType.Password)] [FieldDefinition(3, Label = "Password", Type = FieldType.Password)]
public string Password { get; set; } public string Password { get; set; }
[FieldDefinition(4, Label = "Use SSL", Type = FieldType.Checkbox, Advanced = true)] [FieldDefinition(4, Label = "Category", Type = FieldType.Textbox)]
public string Category { get; set; }
[FieldDefinition(5, Label = "Use SSL", Type = FieldType.Checkbox, Advanced = true)]
public bool UseSsl { get; set; } public bool UseSsl { get; set; }
public NzbDroneValidationResult Validate() public NzbDroneValidationResult Validate()

View File

@ -1,20 +0,0 @@
using FluentValidation;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download.Clients.Hadouken
{
public class HadoukenSettingsValidator : AbstractValidator<HadoukenSettings>
{
public HadoukenSettingsValidator()
{
RuleFor(c => c.Host).ValidHost();
RuleFor(c => c.Port).GreaterThan(0);
RuleFor(c => c.Username).NotEmpty()
.WithMessage("Username must not be empty.");
RuleFor(c => c.Password).NotEmpty()
.WithMessage("Password must not be empty.");
}
}
}

View File

@ -1,16 +0,0 @@
using System.Collections.Generic;
using NzbDrone.Core.Download.Clients.Hadouken.Models;
namespace NzbDrone.Core.Download.Clients.Hadouken
{
public interface IHadoukenProxy
{
HadoukenSystemInfo GetSystemInfo(HadoukenSettings settings);
HadoukenTorrent[] GetTorrents(HadoukenSettings settings);
IDictionary<string, object> GetConfig(HadoukenSettings settings);
string AddTorrentFile(HadoukenSettings settings, byte[] fileContent);
void AddTorrentUri(HadoukenSettings settings, string torrentUrl);
void RemoveTorrent(HadoukenSettings settings, string downloadId);
void RemoveTorrentAndData(HadoukenSettings settings, string downloadId);
}
}

View File

@ -5,6 +5,7 @@
public string InfoHash { get; set; } public string InfoHash { get; set; }
public double Progress { get; set; } public double Progress { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Label { get; set; }
public string SavePath { get; set; } public string SavePath { get; set; }
public HadoukenTorrentState State { get; set; } public HadoukenTorrentState State { get; set; }
public bool IsFinished { get; set; } public bool IsFinished { get; set; }

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Download.Clients.Hadouken.Models
{
public class HadoukenTorrentResponse
{
public object[][] Torrents { get; set; }
}
}

View File

@ -349,12 +349,9 @@
<Compile Include="Download\Clients\DownloadClientAuthenticationException.cs" /> <Compile Include="Download\Clients\DownloadClientAuthenticationException.cs" />
<Compile Include="Download\Clients\DownloadClientException.cs" /> <Compile Include="Download\Clients\DownloadClientException.cs" />
<Compile Include="Download\Clients\Hadouken\Hadouken.cs" /> <Compile Include="Download\Clients\Hadouken\Hadouken.cs" />
<Compile Include="Download\Clients\Hadouken\HadoukenError.cs" />
<Compile Include="Download\Clients\Hadouken\HadoukenProxy.cs" /> <Compile Include="Download\Clients\Hadouken\HadoukenProxy.cs" />
<Compile Include="Download\Clients\Hadouken\HadoukenResponse.cs" />
<Compile Include="Download\Clients\Hadouken\HadoukenSettings.cs" /> <Compile Include="Download\Clients\Hadouken\HadoukenSettings.cs" />
<Compile Include="Download\Clients\Hadouken\HadoukenSettingsValidator.cs" /> <Compile Include="Download\Clients\Hadouken\Models\HadoukenTorrentResponse.cs" />
<Compile Include="Download\Clients\Hadouken\IHadoukenProxy.cs" />
<Compile Include="Download\Clients\Hadouken\Models\HadoukenTorrentState.cs" /> <Compile Include="Download\Clients\Hadouken\Models\HadoukenTorrentState.cs" />
<Compile Include="Download\Clients\Hadouken\Models\HadoukenSystemInfo.cs" /> <Compile Include="Download\Clients\Hadouken\Models\HadoukenSystemInfo.cs" />
<Compile Include="Download\Clients\Hadouken\Models\HadoukenTorrent.cs" /> <Compile Include="Download\Clients\Hadouken\Models\HadoukenTorrent.cs" />