1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-24 16:53:54 +00:00

Nzbget support added to core

#ND-145 In Progress
This commit is contained in:
Mark McDowall 2013-01-23 22:36:37 -08:00
parent 0b9a4e1a46
commit bf2c811a09
22 changed files with 618 additions and 11 deletions

View file

@ -1,4 +1,8 @@
{
"status": false,
"error": "API Key Incorrect"
"version": "1.1",
"error": {
"name": "JSONRPCError",
"code": 1,
"message": "Invalid procedure"
}
}

View file

@ -0,0 +1,8 @@
{
"version": "1.1",
"error": {
"name": "JSONRPCError",
"code": 1,
"message": "Invalid procedure"
}
}

View file

@ -0,0 +1,33 @@
{
"version": "1.1",
"result": [
{
"FirstID": 1,
"LastID": 26,
"FileSizeLo": 350152055,
"FileSizeHi": 0,
"FileSizeMB": 333,
"RemainingSizeLo": 350152055,
"RemainingSizeHi": 0,
"RemainingSizeMB": 333,
"PausedSizeLo": 33317835,
"PausedSizeHi": 0,
"PausedSizeMB": 31,
"FileCount": 26,
"RemainingFileCount": 26,
"RemainingParCount": 7,
"MinPostTime": 1358913959,
"MaxPostTime": 1358913965,
"NZBID": 1,
"NZBName": "White.Collar.S04E11.HDTV.x264-2HD",
"NZBNicename": "White.Collar.S04E11.HDTV.x264-2HD",
"NZBFilename": "C:\\Test\\Blackhole\\White.Collar.S04E11.HDTV.x264-2HD.nzb",
"DestDir": "C:\\test\\NzbGet\\\\dst\\TV\\White.Collar.S04E11.HDTV.x264-2HD",
"Category": "TV",
"MinPriority": 0,
"MaxPriority": 0,
"ActiveDownloads": 4,
"Parameters": []
}
]
}

View file

@ -0,0 +1,4 @@
{
"version": "1.1",
"result": []
}

View file

@ -142,6 +142,8 @@
<Compile Include="IndexerTests\NzbxFixture.cs" />
<Compile Include="JobTests\RenameSeasonJobFixture.cs" />
<Compile Include="ProviderTests\DecisionEngineTests\LanguageSpecificationFixture.cs" />
<Compile Include="ProviderTests\DownloadClientTests\NzbgetProviderTests\DownloadNzbFixture.cs" />
<Compile Include="ProviderTests\DownloadClientTests\NzbgetProviderTests\QueueFixture.cs" />
<Compile Include="ProviderTests\RootDirProviderTests\FreeSpaceOnDrivesFixture.cs" />
<Compile Include="ProviderTests\SearchTests\ProcessResultsFixture.cs" />
<Compile Include="ProviderTests\SearchTests\DailyEpisodeSearchTests\CheckReportFixture.cs" />
@ -277,6 +279,15 @@
<Content Include="App_Data\Config.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Files\Nzbget\JsonError.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Files\Nzbget\Queue_empty.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Files\Nzbget\Queue.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Files\QueueUnknownPriority.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

View file

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Model.Nzbget;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.DownloadClients;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ProviderTests.DownloadClientTests.NzbgetProviderTests
{
public class DownloadNzbFixture : TestBase
{
[SetUp]
public void Setup()
{
var fakeConfig = Mocker.GetMock<ConfigProvider>();
fakeConfig.SetupGet(c => c.NzbgetHost).Returns("192.168.5.55");
fakeConfig.SetupGet(c => c.NzbgetPort).Returns(6789);
fakeConfig.SetupGet(c => c.NzbgetUsername).Returns("nzbget");
fakeConfig.SetupGet(c => c.NzbgetPassword).Returns("pass");
fakeConfig.SetupGet(c => c.NzbgetTvCategory).Returns("TV");
fakeConfig.SetupGet(c => c.NzbgetBacklogTvPriority).Returns(PriorityType.Normal);
fakeConfig.SetupGet(c => c.NzbgetRecentTvPriority).Returns(PriorityType.High);
}
private void WithFailResponse()
{
Mocker.GetMock<HttpProvider>()
.Setup(s => s.PostCommand("http://192.168.5.55:6789/jsonrpc", "nzbget", "pass", It.IsAny<String>()))
.Returns(File.ReadAllText(@".\Files\Nzbget\JsonError.txt"));
}
[Test]
public void should_add_item_to_queue()
{
const string url = "http://www.nzbdrone.com";
const string title = "30 Rock - S01E01 - Pilot [HDTV-720p]";
Mocker.GetMock<HttpProvider>()
.Setup(s => s.PostCommand("http://192.168.5.55:6789/jsonrpc", "nzbget", "pass",
It.Is<String>(c => c.Equals("{\"method\":\"appendurl\",\"params\":[\"30 Rock - S01E01 - Pilot [HDTV-720p]\",\"TV\",0,false,\"http://www.nzbdrone.com\"]}"))))
.Returns("{\"version\": \"1.1\",\"result\": true}");
Mocker.Resolve<NzbgetProvider>()
.DownloadNzb(url, title, false)
.Should()
.BeTrue();
}
[Test]
public void should_throw_when_error_is_returned()
{
WithFailResponse();
Assert.Throws<ApplicationException>(() => Mocker.Resolve<NzbgetProvider>().DownloadNzb("http://www.nzbdrone.com", "30 Rock - S01E01 - Pilot [HDTV-720p]", false));
}
}
}

View file

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Model.Nzbget;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.DownloadClients;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ProviderTests.DownloadClientTests.NzbgetProviderTests
{
public class QueueFixture : TestBase
{
[SetUp]
public void Setup()
{
var fakeConfig = Mocker.GetMock<ConfigProvider>();
fakeConfig.SetupGet(c => c.NzbgetHost).Returns("192.168.5.55");
fakeConfig.SetupGet(c => c.NzbgetPort).Returns(6789);
fakeConfig.SetupGet(c => c.NzbgetUsername).Returns("nzbget");
fakeConfig.SetupGet(c => c.NzbgetPassword).Returns("pass");
fakeConfig.SetupGet(c => c.NzbgetTvCategory).Returns("TV");
fakeConfig.SetupGet(c => c.NzbgetBacklogTvPriority).Returns(PriorityType.Normal);
fakeConfig.SetupGet(c => c.NzbgetRecentTvPriority).Returns(PriorityType.High);
}
private void WithFullQueue()
{
Mocker.GetMock<HttpProvider>()
.Setup(s => s.PostCommand("http://192.168.5.55:6789/jsonrpc", "nzbget", "pass", It.IsAny<String>()))
.Returns(File.ReadAllText(@".\Files\Nzbget\Queue.txt"));
}
private void WithEmptyQueue()
{
Mocker.GetMock<HttpProvider>()
.Setup(s => s.PostCommand("http://192.168.5.55:6789/jsonrpc", "nzbget", "pass", It.IsAny<String>()))
.Returns(File.ReadAllText(@".\Files\Nzbget\Queue_empty.txt"));
}
private void WithFailResponse()
{
Mocker.GetMock<HttpProvider>()
.Setup(s => s.PostCommand("http://192.168.5.55:6789/jsonrpc", "nzbget", "pass", It.IsAny<String>()))
.Returns(File.ReadAllText(@".\Files\Nzbget\JsonError.txt"));
}
[Test]
public void should_return_no_items_when_queue_is_empty()
{
WithEmptyQueue();
Mocker.Resolve<NzbgetProvider>()
.GetQueue()
.Should()
.BeEmpty();
}
[Test]
public void should_return_item_when_queue_has_item()
{
WithFullQueue();
Mocker.Resolve<NzbgetProvider>()
.GetQueue()
.Should()
.HaveCount(1);
}
[Test]
public void should_throw_when_error_is_returned()
{
WithFailResponse();
Assert.Throws<ApplicationException>(() => Mocker.Resolve<NzbgetProvider>().GetQueue());
}
}
}

View file

@ -28,7 +28,7 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchTests
private void WithSceneMapping()
{
Mocker.GetMock<SceneMappingProvider>()
.Setup(s => s.GetSceneName(_series.SeriesId))
.Setup(s => s.GetSceneName(_series.SeriesId, -1))
.Returns("Hawaii Five 0 2010");
}
@ -48,6 +48,26 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchTests
.Should().Be("Hawaii Five 0 2010");
}
[Test]
public void should_return_season_scene_name_when_one_exists()
{
Mocker.GetMock<SceneMappingProvider>()
.Setup(s => s.GetSceneName(_series.SeriesId, 5))
.Returns("Hawaii Five 0 2010 - Season 5");
Mocker.Resolve<TestSearch>().GetSearchTitle(_series, 5)
.Should().Be("Hawaii Five 0 2010 - Season 5");
}
[Test]
public void should_return_series_scene_name_when_one_for_season_does_not_exist()
{
WithSceneMapping();
Mocker.Resolve<TestSearch>().GetSearchTitle(_series, 5)
.Should().Be("Hawaii Five 0 2010");
}
[Test]
public void should_replace_ampersand_with_and()
{
@ -65,7 +85,7 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchTests
_series.Title = input;
Mocker.GetMock<SceneMappingProvider>()
.Setup(s => s.GetSceneName(_series.SeriesId))
.Setup(s => s.GetSceneName(_series.SeriesId, -1))
.Returns("");
Mocker.Resolve<TestSearch>().GetSearchTitle(_series, 5)

View file

@ -4,6 +4,7 @@
{
Sabnzbd = 0,
Blackhole = 1,
Pneumatic = 2
Pneumatic = 2,
Nzbget = 3
}
}

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Model.Nzbget
{
public class EnqueueResponse
{
public String Version { get; set; }
public Boolean Result { get; set; }
}
}

View file

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Model.Nzbget
{
public class ErrorModel
{
public String Name { get; set; }
public Int32 Code { get; set; }
public String Message { get; set; }
public override string ToString()
{
return String.Format("Name: {0}, Code: {1}, Message: {2}", Name, Code, Message);
}
}
}

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Model.Nzbget
{
public class JsonError
{
public String Version { get; set; }
public ErrorModel Error { get; set; }
}
}

View file

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace NzbDrone.Core.Model.Nzbget
{
public class JsonRequest
{
[JsonProperty(PropertyName = "method")]
public String Method { get; set; }
[JsonProperty(PropertyName = "params")]
public object[] Params { get; set; }
}
}

View file

@ -0,0 +1,11 @@
namespace NzbDrone.Core.Model.Nzbget
{
public enum PriorityType
{
VeryLow = -100,
Low = -50,
Normal = 0,
High = 50,
VeryHigh = 100
}
}

View file

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace NzbDrone.Core.Model.Nzbget
{
public class Queue
{
public String Version { get; set; }
[JsonProperty(PropertyName = "result")]
public List<QueueItem> QueueItems { get; set; }
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace NzbDrone.Core.Model.Nzbget
{
public class QueueItem
{
private string _title;
public Int32 NzbId { get; set; }
[JsonProperty(PropertyName = "NzbName")]
public string Title
{
get { return _title; }
set
{
_title = value;
ParseResult = Parser.ParseTitle(value.Replace("DUPLICATE / ", String.Empty));
}
}
public String Category { get; set; }
public Int32 FileSizeMb { get; set; }
public Int32 RemainingSizeMb { get; set; }
public EpisodeParseResult ParseResult { private set; get; }
}
}

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Model.Nzbget
{
public class VersionModel
{
public String Version { get; set; }
public String Result { get; set; }
}
}

View file

@ -281,6 +281,14 @@
<Compile Include="Model\ConnectionInfoModel.cs" />
<Compile Include="Model\BacklogSettingType.cs" />
<Compile Include="Model\MediaInfoModel.cs" />
<Compile Include="Model\Nzbget\EnqueueResponse.cs" />
<Compile Include="Model\Nzbget\ErrorModel.cs" />
<Compile Include="Model\Nzbget\JsonError.cs" />
<Compile Include="Model\Nzbget\JsonRequest.cs" />
<Compile Include="Model\Nzbget\Queue.cs" />
<Compile Include="Model\Nzbget\QueueItem.cs" />
<Compile Include="Model\Nzbget\PriorityType.cs" />
<Compile Include="Model\Nzbget\VersionModel.cs" />
<Compile Include="Model\Nzbx\NzbxSearchItem.cs" />
<Compile Include="Model\Nzbx\NzbxRecentItem.cs" />
<Compile Include="Model\Nzbx\NzbxVotesModel.cs" />
@ -318,6 +326,7 @@
<Compile Include="AutofacSignalrDependencyResolver.cs" />
<Compile Include="Providers\BannerProvider.cs" />
<Compile Include="Providers\DecisionEngine\LanguageSpecification.cs" />
<Compile Include="Providers\DownloadClients\NzbgetProvider.cs" />
<Compile Include="Providers\MediaInfoProvider.cs" />
<Compile Include="Providers\DecisionEngine\AllowedReleaseGroupSpecification.cs" />
<Compile Include="Providers\DecisionEngine\CustomStartDateSpecification.cs" />

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Model.Nzbget;
using NzbDrone.Core.Model.Sabnzbd;
using NzbDrone.Core.Repository;
using PetaPoco;
@ -541,6 +542,62 @@ namespace NzbDrone.Core.Providers.Core
set { SetValue("DownloadClientUseSceneName", value); }
}
public virtual String NzbgetUsername
{
get { return GetValue("NzbgetUsername", "nzbget"); }
set { SetValue("NzbgetUsername", value); }
}
public virtual String NzbgetPassword
{
get { return GetValue("NzbgetPassword", ""); }
set { SetValue("NzbgetPassword", value); }
}
public virtual String NzbgetHost
{
get { return GetValue("NzbgetHost", "nzbget"); }
set { SetValue("NzbgetHost", value); }
}
public virtual Int32 NzbgetPort
{
get { return GetValueInt("NzbgetPort", 6789); }
set { SetValue("NzbgetPort", value); }
}
public virtual String NzbgetTvCategory
{
get { return GetValue("NzbgetTvCategory", "nzbget"); }
set { SetValue("NzbgetTvCategory", value); }
}
public virtual Int32 NzbgetPriority
{
get { return GetValueInt("NzbgetPriority", 0); }
set { SetValue("NzbgetPriority", value); }
}
public virtual PriorityType NzbgetBacklogTvPriority
{
get { return (PriorityType)GetValueInt("NzbgetBacklogTvPriority"); }
set { SetValue("NzbgetBacklogTvPriority", (int)value); }
}
public virtual PriorityType NzbgetRecentTvPriority
{
get { return (PriorityType)GetValueInt("NzbgetRecentTvPriority"); }
set { SetValue("NzbgetRecentTvPriority", (int)value); }
}
private string GetValue(string key)
{
return GetValue(key, String.Empty);

View file

@ -0,0 +1,174 @@
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;
using NzbDrone.Core.Model;
using NzbDrone.Core.Model.Nzbget;
using NzbDrone.Core.Providers.Core;
namespace NzbDrone.Core.Providers.DownloadClients
{
public class NzbgetProvider : IDownloadClient
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private readonly ConfigProvider _configProvider;
private readonly HttpProvider _httpProvider;
public NzbgetProvider(ConfigProvider configProvider, HttpProvider httpProvider)
{
_configProvider = configProvider;
_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);
if (newParseResult.Series.IsDaily)
{
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
{
string cat = _configProvider.NzbgetTvCategory;
int priority = recentlyAired ? (int)_configProvider.NzbgetRecentTvPriority : (int)_configProvider.NzbgetBacklogTvPriority;
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)
host = _configProvider.NzbgetHost;
if (port == 0)
port = _configProvider.NzbgetPort;
if (username == null)
username = _configProvider.NzbgetUsername;
if (password == null)
password = _configProvider.NzbgetPassword;
var command = new JsonRequest
{
Method = "version",
Params = null
};
var url = String.Format(@"http://{0}:{1}/jsonrpc", host, port);
var response = _httpProvider.PostCommand(url, username, password, JsonConvert.SerializeObject(command));
CheckForError(response);
return JsonConvert.DeserializeObject<VersionModel>(response);
}
public virtual string Test(string host, int port, string apiKey, string username, string password)
{
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)
{
var url = String.Format(@"http://{0}:{1}/jsonrpc",
_configProvider.NzbgetHost,
_configProvider.NzbgetPort);
return _httpProvider.PostCommand(url, _configProvider.NzbgetUsername, _configProvider.NzbgetPassword, command);
}
private void CheckForError(string response)
{
var result = JsonConvert.DeserializeObject<JsonError>(response);
if (result.Error != null)
throw new ApplicationException(result.Error.ToString());
}
}
}

View file

@ -51,11 +51,11 @@ namespace NzbDrone.Core.Providers
return true;
}
public virtual string GetSceneName(int seriesId)
public virtual string GetSceneName(int seriesId, int seasonNumber = -1)
{
UpdateIfEmpty();
var item = _database.FirstOrDefault<SceneMapping>("WHERE SeriesId = @0", seriesId);
var item = _database.FirstOrDefault<SceneMapping>("WHERE SeriesId = @0 AND SeasonNumber = @1", seriesId, seasonNumber);
if (item == null)
return null;

View file

@ -57,7 +57,8 @@ namespace NzbDrone.Core.Providers.Search
{
SearchTime = DateTime.Now,
SeriesId = series.SeriesId,
EpisodeId = options.GetType().GetProperty("Episode") != null ? options.Episode.EpisodeId : null
EpisodeId = options.GetType().GetProperty("Episode") != null ? options.Episode.EpisodeId : null,
SeasonNumber = options.GetType().GetProperty("SeasonNumber") != null ? options.SeasonNumber : null
};
List<EpisodeParseResult> reports = PerformSearch(series, options, notification);
@ -166,9 +167,12 @@ namespace NzbDrone.Core.Providers.Search
}
public virtual string GetSearchTitle(Series series, int seasonNumber = -1)
{
//Todo: Add support for per season lookup (used for anime)
//Todo: Add support for multiple names
{
var seasonTitle = _sceneMappingProvider.GetSceneName(series.SeriesId, seasonNumber);
if(!String.IsNullOrWhiteSpace(seasonTitle))
return seasonTitle;
var title = _sceneMappingProvider.GetSceneName(series.SeriesId);
if (String.IsNullOrWhiteSpace(title))