1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-17 00:13:15 +01:00
Radarr/NzbDrone.Core/Download/Clients/Nzbget/NzbgetClient.cs

155 lines
4.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
using NLog;
using NzbDrone.Common;
2013-02-24 07:48:52 +01:00
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Parser.Model;
2013-03-05 06:33:34 +01:00
namespace NzbDrone.Core.Download.Clients.Nzbget
{
public class NzbgetClient : IDownloadClient
{
2013-02-24 07:48:52 +01:00
private readonly IConfigService _configService;
2013-04-11 01:41:45 +02:00
private readonly IHttpProvider _httpProvider;
private readonly Logger _logger;
public NzbgetClient(IConfigService configService, IHttpProvider httpProvider, Logger logger)
{
2013-02-24 07:48:52 +01:00
_configService = configService;
_httpProvider = httpProvider;
2013-04-11 01:41:45 +02:00
_logger = logger;
}
public virtual bool DownloadNzb(RemoteEpisode remoteEpisode)
{
var url = remoteEpisode.Report.NzbUrl;
var title = remoteEpisode.Report.Title;
try
{
2013-02-24 07:48:52 +01:00
string cat = _configService.NzbgetTvCategory;
int priority = remoteEpisode.IsRecentEpisode() ? (int)_configService.NzbgetRecentTvPriority : (int)_configService.NzbgetOlderTvPriority;
var command = new JsonRequest
{
Method = "appendurl",
Params = new object[] { title, cat, priority, false, url }
};
2013-04-11 01:41:45 +02:00
_logger.Info("Adding report [{0}] to the queue.", title);
var response = PostCommand(JsonConvert.SerializeObject(command));
CheckForError(response);
var success = JsonConvert.DeserializeObject<EnqueueResponse>(response).Result;
2013-04-11 01:41:45 +02:00
_logger.Debug("Queue Response: [{0}]", success);
return true;
}
catch (WebException ex)
{
2013-04-11 01:41:45 +02:00
_logger.Error("Error communicating with Nzbget: " + ex.Message);
}
return false;
}
public bool IsConfigured
{
get
{
return !string.IsNullOrWhiteSpace(_configService.NzbgetHost) && _configService.NzbgetPort != 0;
}
}
public virtual IEnumerable<QueueItem> GetQueue()
{
var command = new JsonRequest
{
Method = "listgroups",
Params = null
};
var response = PostCommand(JsonConvert.SerializeObject(command));
CheckForError(response);
var itmes = JsonConvert.DeserializeObject<NzbGetQueue>(response).QueueItems;
foreach (var nzbGetQueueItem in itmes)
{
var queueItem = new QueueItem();
queueItem.Id = nzbGetQueueItem.NzbId.ToString();
queueItem.Title = nzbGetQueueItem.NzbName;
queueItem.Size = nzbGetQueueItem.FileSizeMb;
queueItem.SizeLeft = nzbGetQueueItem.RemainingSizeMb;
yield return queueItem;
}
}
public virtual VersionModel GetVersion(string host = null, int port = 0, string username = null, string password = null)
{
//Get saved values if any of these are defaults
if (host == null)
2013-02-24 07:48:52 +01:00
host = _configService.NzbgetHost;
if (port == 0)
2013-02-24 07:48:52 +01:00
port = _configService.NzbgetPort;
if (username == null)
2013-02-24 07:48:52 +01:00
username = _configService.NzbgetUsername;
if (password == null)
2013-02-24 07:48:52 +01:00
password = _configService.NzbgetPassword;
var command = new JsonRequest
{
Method = "version",
Params = null
};
2013-01-24 08:31:41 +01:00
var address = String.Format(@"{0}:{1}", host, port);
var response = _httpProvider.PostCommand(address, username, password, JsonConvert.SerializeObject(command));
CheckForError(response);
return JsonConvert.DeserializeObject<VersionModel>(response);
}
2013-01-24 08:31:41 +01:00
public virtual string Test(string host, int port, string username, string password)
{
try
{
var version = GetVersion(host, port, username, password);
return version.Result;
}
catch (Exception ex)
{
2013-04-11 01:41:45 +02:00
_logger.DebugException("Failed to Test Nzbget", ex);
}
return String.Empty;
}
private string PostCommand(string command)
{
2013-01-24 08:31:41 +01:00
var url = String.Format(@"{0}:{1}",
2013-02-24 07:48:52 +01:00
_configService.NzbgetHost,
_configService.NzbgetPort);
2013-02-24 07:48:52 +01:00
return _httpProvider.PostCommand(url, _configService.NzbgetUsername, _configService.NzbgetPassword, command);
}
private void CheckForError(string response)
{
var result = JsonConvert.DeserializeObject<JsonError>(response);
if (result.Error != null)
throw new ApplicationException(result.Error.ToString());
}
}
}