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