2013-01-24 06:36:37 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-02-24 06:48:52 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2013-01-24 06:36:37 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Model.Nzbget;
|
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2013-01-24 06:36:37 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers.DownloadClients
|
|
|
|
|
{
|
|
|
|
|
public class NzbgetProvider : IDownloadClient
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2013-02-24 06:48:52 +00:00
|
|
|
|
private readonly IConfigService _configService;
|
2013-01-24 06:36:37 +00:00
|
|
|
|
private readonly HttpProvider _httpProvider;
|
|
|
|
|
|
2013-02-24 06:48:52 +00:00
|
|
|
|
public NzbgetProvider(IConfigService configService, HttpProvider httpProvider)
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NzbgetProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool IsInQueue(EpisodeParseResult newParseResult)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var queue = GetQueue().Where(c => c.ParseResult != null);
|
|
|
|
|
|
|
|
|
|
var matchigTitle = queue.Where(q => String.Equals(q.ParseResult.CleanTitle, newParseResult.Series.CleanTitle, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
|
|
|
|
var matchingTitleWithQuality = matchigTitle.Where(q => q.ParseResult.Quality >= newParseResult.Quality);
|
|
|
|
|
|
|
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
|
if (newParseResult.Series.SeriesType == SeriesType.Daily)
|
2013-01-24 06:36:37 +00:00
|
|
|
|
{
|
|
|
|
|
return matchingTitleWithQuality.Any(q => q.ParseResult.AirDate.Value.Date == newParseResult.AirDate.Value.Date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var matchingSeason = matchingTitleWithQuality.Where(q => q.ParseResult.SeasonNumber == newParseResult.SeasonNumber);
|
|
|
|
|
|
|
|
|
|
if (newParseResult.FullSeason)
|
|
|
|
|
{
|
|
|
|
|
return matchingSeason.Any();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return matchingSeason.Any(q => q.ParseResult.EpisodeNumbers != null && q.ParseResult.EpisodeNumbers.Any(e => newParseResult.EpisodeNumbers.Contains(e)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.WarnException("Unable to connect to Nzbget to check queue.", ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool DownloadNzb(string url, string title, bool recentlyAired)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-02-24 06:48:52 +00:00
|
|
|
|
string cat = _configService.NzbgetTvCategory;
|
|
|
|
|
int priority = recentlyAired ? (int)_configService.NzbgetRecentTvPriority : (int)_configService.NzbgetBacklogTvPriority;
|
2013-01-24 06:36:37 +00:00
|
|
|
|
|
|
|
|
|
var command = new JsonRequest
|
|
|
|
|
{
|
|
|
|
|
Method = "appendurl",
|
|
|
|
|
Params = new object[]{ title, cat, priority, false, url }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
logger.Info("Adding report [{0}] to the queue.", title);
|
|
|
|
|
var response = PostCommand(JsonConvert.SerializeObject(command));
|
|
|
|
|
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
|
|
|
|
|
var success = JsonConvert.DeserializeObject<EnqueueResponse>(response).Result;
|
|
|
|
|
logger.Debug("Queue Response: [{0}]", success);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (WebException ex)
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Error communicating with Nzbget: " + ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual List<QueueItem> GetQueue()
|
|
|
|
|
{
|
|
|
|
|
var command = new JsonRequest
|
|
|
|
|
{
|
|
|
|
|
Method = "listgroups",
|
|
|
|
|
Params = null
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = PostCommand(JsonConvert.SerializeObject(command));
|
|
|
|
|
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
|
|
|
|
|
return JsonConvert.DeserializeObject<Queue>(response).QueueItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.DebugException("Failed to Test Nzbget", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|