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