1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

cleaned up nzb download clients.

This commit is contained in:
kay.one 2013-08-17 16:27:18 -07:00
parent ed883fd014
commit b6ca43f734
11 changed files with 77 additions and 171 deletions

View File

@ -1,6 +1,5 @@
using System.IO;
using System.Net;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
@ -49,31 +48,11 @@ private void WithFailedDownload()
[Test]
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());
}
[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]
public void should_replace_illegal_characters_in_title()
{
@ -81,7 +60,7 @@ public void should_replace_illegal_characters_in_title()
var expectedFilename = Path.Combine(_blackHoleFolder, "Saturday Night Live - S38E08 - Jeremy Renner+Maroon 5 [SDTV].nzb");
_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());
}

View File

@ -57,10 +57,7 @@ public void should_add_item_to_queue()
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}");
Mocker.Resolve<NzbgetClient>()
.DownloadNzb(_remoteEpisode)
.Should()
.BeTrue();
Mocker.Resolve<NzbgetClient>().DownloadNzb(_remoteEpisode);
}
[Test]

View File

@ -1,6 +1,6 @@
using System;
using System.IO;
using System.Net;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
@ -55,38 +55,27 @@ private void WithFailedDownload()
[Test]
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());
}
[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]
public void should_return_false_on_failed_download()
public void should_throw_on_failed_download()
{
WithFailedDownload();
Subject.DownloadNzb(_remoteEpisode).Should().BeFalse();
ExceptionVerification.ExpectedWarns(1);
Assert.Throws<WebException>(() => Subject.DownloadNzb(_remoteEpisode));
}
[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.ParsedEpisodeInfo.FullSeason = true;
Mocker.Resolve<PneumaticClient>().DownloadNzb(_remoteEpisode).Should().BeFalse();
Assert.Throws<NotImplementedException>(() => Subject.DownloadNzb(_remoteEpisode));
}
[Test]
@ -96,7 +85,7 @@ public void should_replace_illegal_characters_in_title()
var expectedFilename = Path.Combine(_pneumaticFolder, "Saturday Night Live - S38E08 - Jeremy Renner+Maroon 5 [SDTV].nzb");
_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());
}

View File

@ -61,14 +61,14 @@ public void add_url_should_format_request_properly()
.Returns("{ \"status\": true }");
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue();
Subject.DownloadNzb(_remoteEpisode);
}
[Test]
public void add_by_url_should_detect_and_handle_sab_errors()
{
WithFailResponse();
Assert.Throws<ApplicationException>(() => Subject.DownloadNzb(_remoteEpisode).Should().BeFalse());
Assert.Throws<ApplicationException>(() => Subject.DownloadNzb(_remoteEpisode));
}
[Test]
@ -197,13 +197,12 @@ public void Test_should_return_version_as_a_string()
}
[Test]
public void should_return_false_when_WebException_is_thrown()
public void should_throw_when_WebException_is_thrown()
{
Mocker.GetMock<IHttpProvider>()
.Setup(s => s.DownloadString(It.IsAny<String>())).Throws(new WebException());
Subject.DownloadNzb(_remoteEpisode).Should().BeFalse();
ExceptionVerification.ExpectedErrors(1);
Assert.Throws<WebException>(() => Subject.DownloadNzb(_remoteEpisode));
}
[Test]
@ -219,7 +218,7 @@ public void downloadNzb_should_use_sabRecentTvPriority_when_recentEpisode_is_tru
.Returns("{ \"status\": true }");
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue();
Subject.DownloadNzb(_remoteEpisode);
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());

View File

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

View File

@ -32,35 +32,20 @@ public bool IsInQueue(RemoteEpisode newEpisode)
throw new NotImplementedException();
}
public bool DownloadNzb(RemoteEpisode remoteEpisode)
public void DownloadNzb(RemoteEpisode remoteEpisode)
{
var url = remoteEpisode.Report.NzbUrl;
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);
_httpProvider.DownloadFile(url, filename);
_logger.Trace("Downloading NZB from: {0} to: {1}", url, filename);
_httpProvider.DownloadFile(url, 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;
}
_logger.Trace("NZB Download succeeded, saved to: {0}", filename);
}
public bool IsConfigured

View File

@ -22,39 +22,28 @@ public NzbgetClient(IConfigService configService, IHttpProvider httpProvider, Lo
_logger = logger;
}
public virtual bool DownloadNzb(RemoteEpisode remoteEpisode)
public void DownloadNzb(RemoteEpisode remoteEpisode)
{
var url = remoteEpisode.Report.NzbUrl;
var title = remoteEpisode.Report.Title;
try
string cat = _configService.NzbgetTvCategory;
int priority = remoteEpisode.IsRecentEpisode() ? (int)_configService.NzbgetRecentTvPriority : (int)_configService.NzbgetOlderTvPriority;
var command = new JsonRequest
{
string cat = _configService.NzbgetTvCategory;
int priority = remoteEpisode.IsRecentEpisode() ? (int)_configService.NzbgetRecentTvPriority : (int)_configService.NzbgetOlderTvPriority;
Method = "appendurl",
Params = new object[] { title, cat, priority, false, url }
};
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));
_logger.Info("Adding report [{0}] to the queue.", title);
var response = PostCommand(JsonConvert.SerializeObject(command));
CheckForError(response);
CheckForError(response);
var success = JsonConvert.DeserializeObject<EnqueueResponse>(response).Result;
_logger.Debug("Queue Response: [{0}]", success);
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 bool IsConfigured

View File

@ -25,47 +25,30 @@ public PneumaticClient(IConfigService configService, IHttpProvider httpProvider,
_diskProvider = diskProvider;
}
public virtual bool DownloadNzb(RemoteEpisode remoteEpisode)
public void DownloadNzb(RemoteEpisode remoteEpisode)
{
var url = remoteEpisode.Report.NzbUrl;
var title = remoteEpisode.Report.Title;
try
if (remoteEpisode.ParsedEpisodeInfo.FullSeason)
{
//Todo: Allow full season releases
if (remoteEpisode.ParsedEpisodeInfo.FullSeason)
{
logger.Info("Skipping Full Season Release: {0}", title);
return false;
}
title = FileNameBuilder.CleanFilename(title);
//Save to the Pneumatic directory (The user will need to ensure its accessible by XBMC)
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);
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);
return true;
}
catch (Exception ex)
{
logger.WarnException("Failed to download NZB: " + url, ex);
return false;
throw new NotImplementedException("Full season Pneumatic releases are not supported.");
}
title = FileNameBuilder.CleanFilename(title);
//Save to the Pneumatic directory (The user will need to ensure its accessible by XBMC)
var filename = Path.Combine(_configService.PneumaticFolder, title + ".nzb");
logger.Trace("Downloading NZB from: {0} to: {1}", url, filename);
_httpProvider.DownloadFile(url, filename);
logger.Trace("NZB Download succeeded, saved to: {0}", filename);
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);
}
public bool IsConfigured

View File

@ -62,39 +62,28 @@ public SabnzbdClient(IConfigService configService, IHttpProvider httpProvider, L
_logger = logger;
}
public virtual bool DownloadNzb(RemoteEpisode remoteEpisode)
public void DownloadNzb(RemoteEpisode remoteEpisode)
{
var url = remoteEpisode.Report.NzbUrl;
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 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 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);
_logger.Info("Adding report [{0}] to the queue.", title);
string request = GetSabRequest(action);
_logger.Info("Adding report [{0}] to the queue.", title);
var response = _httpProvider.DownloadString(request);
var response = _httpProvider.DownloadString(request);
_logger.Debug("Queue Response: [{0}]", response);
_logger.Debug("Queue Response: [{0}]", response);
CheckForError(response);
return true;
}
catch (WebException ex)
{
_logger.Error("Error communicating with SAB: " + ex.Message);
}
return false;
CheckForError(response);
}
public bool IsConfigured

View File

@ -6,7 +6,7 @@ namespace NzbDrone.Core.Download
{
public interface IDownloadService
{
bool DownloadReport(RemoteEpisode remoteEpisode);
void DownloadReport(RemoteEpisode remoteEpisode);
}
public class DownloadService : IDownloadService
@ -24,7 +24,7 @@ public DownloadService(IProvideDownloadClient downloadClientProvider,
_logger = logger;
}
public bool DownloadReport(RemoteEpisode remoteEpisode)
public void DownloadReport(RemoteEpisode remoteEpisode)
{
var downloadTitle = remoteEpisode.Report.Title;
var downloadClient = _downloadClientProvider.GetDownloadClient();
@ -32,18 +32,13 @@ public bool DownloadReport(RemoteEpisode remoteEpisode)
if (!downloadClient.IsConfigured)
{
_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));
}
return success;
_logger.Info("Report sent to download client. {0}", downloadTitle);
_messageAggregator.PublishEvent(new EpisodeGrabbedEvent(remoteEpisode));
}
}
}

View File

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