|
|
|
@ -8,7 +8,7 @@ using RestSharp;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Download.Clients.Hadouken
|
|
|
|
|
{
|
|
|
|
|
public sealed class HadoukenProxy : IHadoukenProxy
|
|
|
|
|
public class HadoukenProxy : IHadoukenProxy
|
|
|
|
|
{
|
|
|
|
|
private static int _callId;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
@ -23,34 +23,39 @@ namespace NzbDrone.Core.Download.Clients.Hadouken
|
|
|
|
|
return ProcessRequest<HadoukenSystemInfo>(settings, "core.getSystemInfo").Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IDictionary<string, HadoukenTorrent> GetTorrents(HadoukenSettings settings)
|
|
|
|
|
public HadoukenTorrent[] GetTorrents(HadoukenSettings settings)
|
|
|
|
|
{
|
|
|
|
|
return ProcessRequest<Dictionary<string, HadoukenTorrent>>(settings, "session.getTorrents").Result;
|
|
|
|
|
var result = ProcessRequest<HadoukenResponseResult>(settings, "webui.list").Result;
|
|
|
|
|
|
|
|
|
|
return GetTorrents(result.Torrents);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IDictionary<string, object> GetConfig(HadoukenSettings settings)
|
|
|
|
|
{
|
|
|
|
|
return ProcessRequest<IDictionary<string, object>>(settings, "config.get").Result;
|
|
|
|
|
return ProcessRequest<IDictionary<string, object>>(settings, "webui.getSettings").Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string AddTorrentFile(HadoukenSettings settings, byte[] fileContent)
|
|
|
|
|
{
|
|
|
|
|
return ProcessRequest<string>(settings, "session.addTorrentFile", Convert.ToBase64String(fileContent)).Result;
|
|
|
|
|
return ProcessRequest<string>(settings, "webui.addTorrent", "file", Convert.ToBase64String(fileContent)).Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddTorrentUri(HadoukenSettings settings, string torrentUrl)
|
|
|
|
|
{
|
|
|
|
|
ProcessRequest<string>(settings, "session.addTorrentUri", torrentUrl);
|
|
|
|
|
ProcessRequest<string>(settings, "webui.addTorrent", "url", torrentUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveTorrent(HadoukenSettings settings, string downloadId, bool deleteData)
|
|
|
|
|
public void RemoveTorrent(HadoukenSettings settings, string downloadId)
|
|
|
|
|
{
|
|
|
|
|
ProcessRequest<bool>(settings, "session.removeTorrent", downloadId, deleteData);
|
|
|
|
|
ProcessRequest<bool>(settings, "webui.perform", "remove", new string[] { downloadId });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HadoukenResponse<TResult> ProcessRequest<TResult>(HadoukenSettings settings,
|
|
|
|
|
string method,
|
|
|
|
|
params object[] parameters)
|
|
|
|
|
public void RemoveTorrentAndData(HadoukenSettings settings, string downloadId)
|
|
|
|
|
{
|
|
|
|
|
ProcessRequest<bool>(settings, "webui.perform", "removedata", new string[] { downloadId });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HadoukenResponse<TResult> ProcessRequest<TResult>(HadoukenSettings settings, string method, params object[] parameters)
|
|
|
|
|
{
|
|
|
|
|
var client = BuildClient(settings);
|
|
|
|
|
return ProcessRequest<TResult>(client, method, parameters);
|
|
|
|
@ -86,24 +91,88 @@ namespace NzbDrone.Core.Download.Clients.Hadouken
|
|
|
|
|
var restClient = RestClientFactory.BuildClient(url);
|
|
|
|
|
restClient.Timeout = 4000;
|
|
|
|
|
|
|
|
|
|
if (settings.AuthenticationType == (int) AuthenticationType.Basic)
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (torrentsRaw == null)
|
|
|
|
|
{
|
|
|
|
|
var basicData = Encoding.UTF8.GetBytes(string.Format("{0}:{1}", settings.Username, settings.Password));
|
|
|
|
|
var basicHeader = Convert.ToBase64String(basicData);
|
|
|
|
|
return new HadoukenTorrent[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var torrents = new List<HadoukenTorrent>();
|
|
|
|
|
|
|
|
|
|
restClient.AddDefaultHeader("Authorization", string.Format("Basic {0}", basicHeader));
|
|
|
|
|
foreach (var item in torrentsRaw)
|
|
|
|
|
{
|
|
|
|
|
var torrent = MapTorrent(item);
|
|
|
|
|
if (torrent != null)
|
|
|
|
|
{
|
|
|
|
|
torrent.IsFinished = torrent.Progress >= 1000;
|
|
|
|
|
torrents.Add(torrent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (settings.AuthenticationType == (int) AuthenticationType.Token)
|
|
|
|
|
|
|
|
|
|
return torrents.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HadoukenTorrent MapTorrent(object[] item)
|
|
|
|
|
{
|
|
|
|
|
HadoukenTorrent torrent = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
restClient.AddDefaultHeader("Authorization", string.Format("Token {0}", settings.Token));
|
|
|
|
|
torrent = new HadoukenTorrent()
|
|
|
|
|
{
|
|
|
|
|
InfoHash = Convert.ToString(item[0]),
|
|
|
|
|
State = ParseState(Convert.ToInt32(item[1])),
|
|
|
|
|
Name = Convert.ToString(item[2]),
|
|
|
|
|
TotalSize = Convert.ToInt64(item[3]),
|
|
|
|
|
Progress = Convert.ToDouble(item[4]),
|
|
|
|
|
DownloadedBytes = Convert.ToInt64(item[5]),
|
|
|
|
|
DownloadRate = Convert.ToInt64(item[9]),
|
|
|
|
|
Error = Convert.ToString(item[21]),
|
|
|
|
|
SavePath = Convert.ToString(item[26])
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Failed to map Hadouken torrent data.", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return restClient;
|
|
|
|
|
return torrent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetCallId()
|
|
|
|
|
private HadoukenTorrentState ParseState(int state)
|
|
|
|
|
{
|
|
|
|
|
return System.Threading.Interlocked.Increment(ref _callId);
|
|
|
|
|
if ((state & 1) == 1)
|
|
|
|
|
{
|
|
|
|
|
return HadoukenTorrentState.Downloading;
|
|
|
|
|
}
|
|
|
|
|
else if ((state & 2) == 2)
|
|
|
|
|
{
|
|
|
|
|
return HadoukenTorrentState.CheckingFiles;
|
|
|
|
|
}
|
|
|
|
|
else if ((state & 32) == 32)
|
|
|
|
|
{
|
|
|
|
|
return HadoukenTorrentState.Paused;
|
|
|
|
|
}
|
|
|
|
|
else if ((state & 64) == 64)
|
|
|
|
|
{
|
|
|
|
|
return HadoukenTorrentState.QueuedForChecking;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return HadoukenTorrentState.Unknown;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|