cleaned up nzb download clients.

pull/2/head
kay.one 11 years ago
parent ed883fd014
commit b6ca43f734

@ -1,6 +1,5 @@
using System.IO; using System.IO;
using System.Net; using System.Net;
using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common; using NzbDrone.Common;
@ -49,31 +48,11 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests
[Test] [Test]
public void DownloadNzb_should_download_file_if_it_doesnt_exist() public void DownloadNzb_should_download_file_if_it_doesnt_exist()
{ {
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue(); Subject.DownloadNzb(_remoteEpisode);
Mocker.GetMock<IHttpProvider>().Verify(c => c.DownloadFile(_nzbUrl, _nzbPath), Times.Once()); Mocker.GetMock<IHttpProvider>().Verify(c => c.DownloadFile(_nzbUrl, _nzbPath), Times.Once());
} }
[Test]
public void DownloadNzb_not_download_file_if_it_doesn_exist()
{
WithExistingFile();
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue();
Mocker.GetMock<IHttpProvider>().Verify(c => c.DownloadFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
}
[Test]
public void should_return_false_on_failed_download()
{
WithFailedDownload();
Subject.DownloadNzb(_remoteEpisode).Should().BeFalse();
ExceptionVerification.ExpectedWarns(1);
}
[Test] [Test]
public void should_replace_illegal_characters_in_title() public void should_replace_illegal_characters_in_title()
{ {
@ -81,7 +60,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests
var expectedFilename = Path.Combine(_blackHoleFolder, "Saturday Night Live - S38E08 - Jeremy Renner+Maroon 5 [SDTV].nzb"); var expectedFilename = Path.Combine(_blackHoleFolder, "Saturday Night Live - S38E08 - Jeremy Renner+Maroon 5 [SDTV].nzb");
_remoteEpisode.Report.Title = illegalTitle; _remoteEpisode.Report.Title = illegalTitle;
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue(); Subject.DownloadNzb(_remoteEpisode);
Mocker.GetMock<IHttpProvider>().Verify(c => c.DownloadFile(It.IsAny<string>(), expectedFilename), Times.Once()); Mocker.GetMock<IHttpProvider>().Verify(c => c.DownloadFile(It.IsAny<string>(), expectedFilename), Times.Once());
} }

@ -57,10 +57,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetProviderTests
It.Is<String>(c => c.Equals("{\"method\":\"appendurl\",\"params\":[\"30 Rock - S01E01 - Pilot [HDTV-720p]\",\"TV\",50,false,\"http://www.nzbdrone.com\"]}")))) It.Is<String>(c => c.Equals("{\"method\":\"appendurl\",\"params\":[\"30 Rock - S01E01 - Pilot [HDTV-720p]\",\"TV\",50,false,\"http://www.nzbdrone.com\"]}"))))
.Returns("{\"version\": \"1.1\",\"result\": true}"); .Returns("{\"version\": \"1.1\",\"result\": true}");
Mocker.Resolve<NzbgetClient>() Mocker.Resolve<NzbgetClient>().DownloadNzb(_remoteEpisode);
.DownloadNzb(_remoteEpisode)
.Should()
.BeTrue();
} }
[Test] [Test]

@ -1,6 +1,6 @@
using System;
using System.IO; using System.IO;
using System.Net; using System.Net;
using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common; using NzbDrone.Common;
@ -55,38 +55,27 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests
[Test] [Test]
public void should_download_file_if_it_doesnt_exist() public void should_download_file_if_it_doesnt_exist()
{ {
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue(); Subject.DownloadNzb(_remoteEpisode);
Mocker.GetMock<IHttpProvider>().Verify(c => c.DownloadFile(_nzbUrl, _nzbPath), Times.Once()); Mocker.GetMock<IHttpProvider>().Verify(c => c.DownloadFile(_nzbUrl, _nzbPath), Times.Once());
} }
[Test]
public void should_not_download_file_if_it_doesn_exist()
{
WithExistingFile();
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue();
Mocker.GetMock<IHttpProvider>().Verify(c => c.DownloadFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
}
[Test] [Test]
public void should_return_false_on_failed_download() public void should_throw_on_failed_download()
{ {
WithFailedDownload(); WithFailedDownload();
Subject.DownloadNzb(_remoteEpisode).Should().BeFalse(); Assert.Throws<WebException>(() => Subject.DownloadNzb(_remoteEpisode));
ExceptionVerification.ExpectedWarns(1);
} }
[Test] [Test]
public void should_skip_if_full_season_download() public void should_throw_if_full_season_download()
{ {
_remoteEpisode.Report.Title = "30 Rock - Season 1"; _remoteEpisode.Report.Title = "30 Rock - Season 1";
_remoteEpisode.ParsedEpisodeInfo.FullSeason = true; _remoteEpisode.ParsedEpisodeInfo.FullSeason = true;
Mocker.Resolve<PneumaticClient>().DownloadNzb(_remoteEpisode).Should().BeFalse(); Assert.Throws<NotImplementedException>(() => Subject.DownloadNzb(_remoteEpisode));
} }
[Test] [Test]
@ -96,7 +85,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests
var expectedFilename = Path.Combine(_pneumaticFolder, "Saturday Night Live - S38E08 - Jeremy Renner+Maroon 5 [SDTV].nzb"); var expectedFilename = Path.Combine(_pneumaticFolder, "Saturday Night Live - S38E08 - Jeremy Renner+Maroon 5 [SDTV].nzb");
_remoteEpisode.Report.Title = illegalTitle; _remoteEpisode.Report.Title = illegalTitle;
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue(); Subject.DownloadNzb(_remoteEpisode);
Mocker.GetMock<IHttpProvider>().Verify(c => c.DownloadFile(It.IsAny<string>(), expectedFilename), Times.Once()); Mocker.GetMock<IHttpProvider>().Verify(c => c.DownloadFile(It.IsAny<string>(), expectedFilename), Times.Once());
} }

@ -61,14 +61,14 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabProviderTests
.Returns("{ \"status\": true }"); .Returns("{ \"status\": true }");
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue(); Subject.DownloadNzb(_remoteEpisode);
} }
[Test] [Test]
public void add_by_url_should_detect_and_handle_sab_errors() public void add_by_url_should_detect_and_handle_sab_errors()
{ {
WithFailResponse(); WithFailResponse();
Assert.Throws<ApplicationException>(() => Subject.DownloadNzb(_remoteEpisode).Should().BeFalse()); Assert.Throws<ApplicationException>(() => Subject.DownloadNzb(_remoteEpisode));
} }
[Test] [Test]
@ -197,13 +197,12 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabProviderTests
} }
[Test] [Test]
public void should_return_false_when_WebException_is_thrown() public void should_throw_when_WebException_is_thrown()
{ {
Mocker.GetMock<IHttpProvider>() Mocker.GetMock<IHttpProvider>()
.Setup(s => s.DownloadString(It.IsAny<String>())).Throws(new WebException()); .Setup(s => s.DownloadString(It.IsAny<String>())).Throws(new WebException());
Subject.DownloadNzb(_remoteEpisode).Should().BeFalse(); Assert.Throws<WebException>(() => Subject.DownloadNzb(_remoteEpisode));
ExceptionVerification.ExpectedErrors(1);
} }
[Test] [Test]
@ -219,7 +218,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabProviderTests
.Returns("{ \"status\": true }"); .Returns("{ \"status\": true }");
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue(); Subject.DownloadNzb(_remoteEpisode);
Mocker.GetMock<IHttpProvider>() Mocker.GetMock<IHttpProvider>()
.Verify(v => v.DownloadString("http://192.168.5.55:2222/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=1&pp=3&cat=tv&nzbname=My+Series+Name+-+5x2-5x3+-+My+title+%5bBluray720p%5d+%5bProper%5d&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"), Times.Once()); .Verify(v => v.DownloadString("http://192.168.5.55:2222/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=1&pp=3&cat=tv&nzbname=My+Series+Name+-+5x2-5x3+-+My+title+%5bBluray720p%5d+%5bProper%5d&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"), Times.Once());

@ -1,4 +1,5 @@
using System.Linq; using System.Linq;
using System.Net;
using FizzWare.NBuilder; using FizzWare.NBuilder;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -40,15 +41,14 @@ namespace NzbDrone.Core.Test.Download
private void WithSuccessfulAdd() private void WithSuccessfulAdd()
{ {
Mocker.GetMock<IDownloadClient>() Mocker.GetMock<IDownloadClient>()
.Setup(s => s.DownloadNzb(It.IsAny<RemoteEpisode>())) .Setup(s => s.DownloadNzb(It.IsAny<RemoteEpisode>()));
.Returns(true);
} }
private void WithFailedAdd() private void WithFailedAdd()
{ {
Mocker.GetMock<IDownloadClient>() Mocker.GetMock<IDownloadClient>()
.Setup(s => s.DownloadNzb(It.IsAny<RemoteEpisode>())) .Setup(s => s.DownloadNzb(It.IsAny<RemoteEpisode>()))
.Returns(false); .Throws(new WebException());
} }
[Test] [Test]
@ -77,7 +77,8 @@ namespace NzbDrone.Core.Test.Download
{ {
WithFailedAdd(); WithFailedAdd();
Subject.DownloadReport(_parseResult); Assert.Throws<WebException>(() => Subject.DownloadReport(_parseResult));
VerifyEventNotPublished<EpisodeGrabbedEvent>(); VerifyEventNotPublished<EpisodeGrabbedEvent>();
} }
@ -90,7 +91,7 @@ namespace NzbDrone.Core.Test.Download
Subject.DownloadReport(_parseResult); Subject.DownloadReport(_parseResult);
Mocker.GetMock<IDownloadClient>().Verify(c => c.DownloadNzb(It.IsAny<RemoteEpisode>()),Times.Never()); Mocker.GetMock<IDownloadClient>().Verify(c => c.DownloadNzb(It.IsAny<RemoteEpisode>()), Times.Never());
VerifyEventNotPublished<EpisodeGrabbedEvent>(); VerifyEventNotPublished<EpisodeGrabbedEvent>();
ExceptionVerification.ExpectedWarns(1); ExceptionVerification.ExpectedWarns(1);

@ -32,35 +32,20 @@ namespace NzbDrone.Core.Download.Clients
throw new NotImplementedException(); throw new NotImplementedException();
} }
public bool DownloadNzb(RemoteEpisode remoteEpisode) public void DownloadNzb(RemoteEpisode remoteEpisode)
{ {
var url = remoteEpisode.Report.NzbUrl; var url = remoteEpisode.Report.NzbUrl;
var title = remoteEpisode.Report.Title; var title = remoteEpisode.Report.Title;
try title = FileNameBuilder.CleanFilename(title);
{
title = FileNameBuilder.CleanFilename(title);
var filename = Path.Combine(_configService.BlackholeFolder, title + ".nzb"); var filename = Path.Combine(_configService.BlackholeFolder, title + ".nzb");
if (_diskProvider.FileExists(filename))
{
//Return true so a lesser quality is not returned.
_logger.Info("NZB already exists on disk: {0}", filename);
return true;
}
_logger.Trace("Downloading NZB from: {0} to: {1}", url, filename); _logger.Trace("Downloading NZB from: {0} to: {1}", url, filename);
_httpProvider.DownloadFile(url, filename); _httpProvider.DownloadFile(url, filename);
_logger.Trace("NZB Download succeeded, saved to: {0}", filename); _logger.Trace("NZB Download succeeded, saved to: {0}", filename);
return true;
}
catch (Exception ex)
{
_logger.WarnException("Failed to download NZB: " + url, ex);
return false;
}
} }
public bool IsConfigured public bool IsConfigured

@ -22,39 +22,28 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
_logger = logger; _logger = logger;
} }
public virtual bool DownloadNzb(RemoteEpisode remoteEpisode) public void DownloadNzb(RemoteEpisode remoteEpisode)
{ {
var url = remoteEpisode.Report.NzbUrl; var url = remoteEpisode.Report.NzbUrl;
var title = remoteEpisode.Report.Title; var title = remoteEpisode.Report.Title;
try string cat = _configService.NzbgetTvCategory;
{ int priority = remoteEpisode.IsRecentEpisode() ? (int)_configService.NzbgetRecentTvPriority : (int)_configService.NzbgetOlderTvPriority;
string cat = _configService.NzbgetTvCategory;
int priority = remoteEpisode.IsRecentEpisode() ? (int)_configService.NzbgetRecentTvPriority : (int)_configService.NzbgetOlderTvPriority;
var command = new JsonRequest var command = new JsonRequest
{ {
Method = "appendurl", Method = "appendurl",
Params = new object[] { title, cat, priority, false, url } 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.Info("Adding report [{0}] to the queue.", title);
_logger.Debug("Queue Response: [{0}]", success); var response = PostCommand(JsonConvert.SerializeObject(command));
return true; CheckForError(response);
}
catch (WebException ex) var success = JsonConvert.DeserializeObject<EnqueueResponse>(response).Result;
{ _logger.Debug("Queue Response: [{0}]", success);
_logger.Error("Error communicating with Nzbget: " + ex.Message);
}
return false;
} }
public bool IsConfigured public bool IsConfigured

@ -25,47 +25,30 @@ namespace NzbDrone.Core.Download.Clients
_diskProvider = diskProvider; _diskProvider = diskProvider;
} }
public virtual bool DownloadNzb(RemoteEpisode remoteEpisode) public void DownloadNzb(RemoteEpisode remoteEpisode)
{ {
var url = remoteEpisode.Report.NzbUrl; var url = remoteEpisode.Report.NzbUrl;
var title = remoteEpisode.Report.Title; var title = remoteEpisode.Report.Title;
try if (remoteEpisode.ParsedEpisodeInfo.FullSeason)
{ {
//Todo: Allow full season releases throw new NotImplementedException("Full season Pneumatic releases are not supported.");
if (remoteEpisode.ParsedEpisodeInfo.FullSeason) }
{
logger.Info("Skipping Full Season Release: {0}", title);
return false;
}
title = FileNameBuilder.CleanFilename(title); title = FileNameBuilder.CleanFilename(title);
//Save to the Pneumatic directory (The user will need to ensure its accessible by XBMC) //Save to the Pneumatic directory (The user will need to ensure its accessible by XBMC)
var filename = Path.Combine(_configService.PneumaticFolder, title + ".nzb"); var filename = Path.Combine(_configService.PneumaticFolder, title + ".nzb");
if (_diskProvider.FileExists(filename))
{
//Return true so a lesser quality is not returned.
logger.Info("NZB already exists on disk: {0}", filename);
return true;
}
logger.Trace("Downloading NZB from: {0} to: {1}", url, filename);
_httpProvider.DownloadFile(url, filename);
logger.Trace("NZB Download succeeded, saved to: {0}", filename); logger.Trace("Downloading NZB from: {0} to: {1}", url, filename);
_httpProvider.DownloadFile(url, filename);
var contents = String.Format("plugin://plugin.program.pneumatic/?mode=strm&type=add_file&nzb={0}&nzbname={1}", filename, title); logger.Trace("NZB Download succeeded, saved to: {0}", filename);
_diskProvider.WriteAllText(Path.Combine(_configService.DownloadedEpisodesFolder, title + ".strm"), contents);
return true; var contents = String.Format("plugin://plugin.program.pneumatic/?mode=strm&type=add_file&nzb={0}&nzbname={1}", filename, title);
} _diskProvider.WriteAllText(Path.Combine(_configService.DownloadedEpisodesFolder, title + ".strm"), contents);
catch (Exception ex)
{
logger.WarnException("Failed to download NZB: " + url, ex);
return false;
}
} }
public bool IsConfigured public bool IsConfigured

@ -62,39 +62,28 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
_logger = logger; _logger = logger;
} }
public virtual bool DownloadNzb(RemoteEpisode remoteEpisode) public void DownloadNzb(RemoteEpisode remoteEpisode)
{ {
var url = remoteEpisode.Report.NzbUrl; var url = remoteEpisode.Report.NzbUrl;
var title = remoteEpisode.Report.Title; var title = remoteEpisode.Report.Title;
try string cat = _configService.SabTvCategory;
{ int priority = remoteEpisode.IsRecentEpisode() ? (int)_configService.SabRecentTvPriority : (int)_configService.SabOlderTvPriority;
string cat = _configService.SabTvCategory;
int priority = remoteEpisode.IsRecentEpisode() ? (int)_configService.SabRecentTvPriority : (int)_configService.SabOlderTvPriority;
string name = url.Replace("&", "%26");
string nzbName = HttpUtility.UrlEncode(title);
string action = string.Format("mode=addurl&name={0}&priority={1}&pp=3&cat={2}&nzbname={3}&output=json",
name, priority, cat, nzbName);
string request = GetSabRequest(action); string name = url.Replace("&", "%26");
_logger.Info("Adding report [{0}] to the queue.", title); string nzbName = HttpUtility.UrlEncode(title);
var response = _httpProvider.DownloadString(request); string action = string.Format("mode=addurl&name={0}&priority={1}&pp=3&cat={2}&nzbname={3}&output=json",
name, priority, cat, nzbName);
_logger.Debug("Queue Response: [{0}]", response); string request = GetSabRequest(action);
_logger.Info("Adding report [{0}] to the queue.", title);
CheckForError(response); var response = _httpProvider.DownloadString(request);
return true;
}
catch (WebException ex) _logger.Debug("Queue Response: [{0}]", response);
{
_logger.Error("Error communicating with SAB: " + ex.Message);
}
return false; CheckForError(response);
} }
public bool IsConfigured public bool IsConfigured

@ -6,7 +6,7 @@ namespace NzbDrone.Core.Download
{ {
public interface IDownloadService public interface IDownloadService
{ {
bool DownloadReport(RemoteEpisode remoteEpisode); void DownloadReport(RemoteEpisode remoteEpisode);
} }
public class DownloadService : IDownloadService public class DownloadService : IDownloadService
@ -24,7 +24,7 @@ namespace NzbDrone.Core.Download
_logger = logger; _logger = logger;
} }
public bool DownloadReport(RemoteEpisode remoteEpisode) public void DownloadReport(RemoteEpisode remoteEpisode)
{ {
var downloadTitle = remoteEpisode.Report.Title; var downloadTitle = remoteEpisode.Report.Title;
var downloadClient = _downloadClientProvider.GetDownloadClient(); var downloadClient = _downloadClientProvider.GetDownloadClient();
@ -32,18 +32,13 @@ namespace NzbDrone.Core.Download
if (!downloadClient.IsConfigured) if (!downloadClient.IsConfigured)
{ {
_logger.Warn("Download client {0} isn't configured yet.", downloadClient.GetType().Name); _logger.Warn("Download client {0} isn't configured yet.", downloadClient.GetType().Name);
return false; return;
} }
bool success = downloadClient.DownloadNzb(remoteEpisode); downloadClient.DownloadNzb(remoteEpisode);
if (success) _logger.Info("Report sent to download client. {0}", downloadTitle);
{ _messageAggregator.PublishEvent(new EpisodeGrabbedEvent(remoteEpisode));
_logger.Info("Report sent to download client. {0}", downloadTitle);
_messageAggregator.PublishEvent(new EpisodeGrabbedEvent(remoteEpisode));
}
return success;
} }
} }
} }

@ -5,7 +5,7 @@ namespace NzbDrone.Core.Download
{ {
public interface IDownloadClient public interface IDownloadClient
{ {
bool DownloadNzb(RemoteEpisode remoteEpisode); void DownloadNzb(RemoteEpisode remoteEpisode);
bool IsConfigured { get; } bool IsConfigured { get; }
IEnumerable<QueueItem> GetQueue(); IEnumerable<QueueItem> GetQueue();
} }

Loading…
Cancel
Save