From 430f76e6c7df810a53761a39ac741a7646cae76c Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Mon, 16 Sep 2013 23:14:47 -0700 Subject: [PATCH] Xem cleanup. --- .../DiskProviderTests/FreeSpaceFixture.cs | 33 ++++--- .../DiskProviderTests/IsParentFixture.cs | 33 +++---- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 2 +- .../GetSceneTvdbMappingsFixture.cs | 17 ++-- .../GetXemSeriesIdsFixture.cs | 55 ----------- .../Providers/XemProxyFixture.cs | 59 ++++++++++++ NzbDrone.Core/NzbDrone.Core.csproj | 4 +- .../Providers/UpdateXemMappingsCommand.cs | 7 +- .../Providers/XemCommunicationProvider.cs | 68 -------------- NzbDrone.Core/Providers/XemProxy.cs | 76 +++++++++++++++ .../{XemProvider.cs => XemService.cs} | 93 +++++++------------ 11 files changed, 214 insertions(+), 233 deletions(-) delete mode 100644 NzbDrone.Core.Test/ProviderTests/XemCommunicationProviderTests/GetXemSeriesIdsFixture.cs create mode 100644 NzbDrone.Core.Test/Providers/XemProxyFixture.cs delete mode 100644 NzbDrone.Core/Providers/XemCommunicationProvider.cs create mode 100644 NzbDrone.Core/Providers/XemProxy.cs rename NzbDrone.Core/Providers/{XemProvider.cs => XemService.cs} (69%) diff --git a/NzbDrone.Common.Test/DiskProviderTests/FreeSpaceFixture.cs b/NzbDrone.Common.Test/DiskProviderTests/FreeSpaceFixture.cs index f3ede03eb..b29f60180 100644 --- a/NzbDrone.Common.Test/DiskProviderTests/FreeSpaceFixture.cs +++ b/NzbDrone.Common.Test/DiskProviderTests/FreeSpaceFixture.cs @@ -1,36 +1,43 @@ -using FluentAssertions; +using System.IO; +using FluentAssertions; using NUnit.Framework; using NzbDrone.Test.Common; namespace NzbDrone.Common.Test.DiskProviderTests { [TestFixture] - public class IsParentFixture : TestBase + public class FreeSpaceFixture : TestBase { - private string _parent = @"C:\Test".AsOsAgnostic(); - [Test] - public void should_return_false_when_not_a_child() + public void should_get_free_space_for_folder() { - var path = @"C:\Another Folder".AsOsAgnostic(); + var path = @"C:\".AsOsAgnostic(); - Subject.IsParent(_parent, path).Should().BeFalse(); + Subject.GetAvailableSpace(path).Should().NotBe(0); } [Test] - public void should_return_true_when_folder_is_parent_of_another_folder() + public void should_get_free_space_for_folder_that_doesnt_exist() { - var path = @"C:\Test\TV".AsOsAgnostic(); + var path = @"C:\".AsOsAgnostic(); - Subject.IsParent(_parent, path).Should().BeTrue(); + Subject.GetAvailableSpace(Path.Combine(path, "invalidFolder")).Should().NotBe(0); } + [Test] - public void should_return_true_when_folder_is_parent_of_a_file() + public void should_get_free_space_for_drive_that_doesnt_exist() { - var path = @"C:\Test\30.Rock.S01E01.Pilot.avi".AsOsAgnostic(); + WindowsOnly(); + + Assert.Throws(() => Subject.GetAvailableSpace("J:\\").Should().NotBe(0)); + } - Subject.IsParent(_parent, path).Should().BeTrue(); + [Test] + public void should_be_able_to_check_space_on_ramdrive() + { + LinuxOnly(); + Subject.GetAvailableSpace("/run/").Should().NotBe(0); } } } diff --git a/NzbDrone.Common.Test/DiskProviderTests/IsParentFixture.cs b/NzbDrone.Common.Test/DiskProviderTests/IsParentFixture.cs index b29f60180..f3ede03eb 100644 --- a/NzbDrone.Common.Test/DiskProviderTests/IsParentFixture.cs +++ b/NzbDrone.Common.Test/DiskProviderTests/IsParentFixture.cs @@ -1,43 +1,36 @@ -using System.IO; -using FluentAssertions; +using FluentAssertions; using NUnit.Framework; using NzbDrone.Test.Common; namespace NzbDrone.Common.Test.DiskProviderTests { [TestFixture] - public class FreeSpaceFixture : TestBase + public class IsParentFixture : TestBase { - [Test] - public void should_get_free_space_for_folder() - { - var path = @"C:\".AsOsAgnostic(); - - Subject.GetAvailableSpace(path).Should().NotBe(0); - } + private string _parent = @"C:\Test".AsOsAgnostic(); [Test] - public void should_get_free_space_for_folder_that_doesnt_exist() + public void should_return_false_when_not_a_child() { - var path = @"C:\".AsOsAgnostic(); + var path = @"C:\Another Folder".AsOsAgnostic(); - Subject.GetAvailableSpace(Path.Combine(path, "invalidFolder")).Should().NotBe(0); + Subject.IsParent(_parent, path).Should().BeFalse(); } - [Test] - public void should_get_free_space_for_drive_that_doesnt_exist() + public void should_return_true_when_folder_is_parent_of_another_folder() { - WindowsOnly(); + var path = @"C:\Test\TV".AsOsAgnostic(); - Assert.Throws(() => Subject.GetAvailableSpace("J:\\").Should().NotBe(0)); + Subject.IsParent(_parent, path).Should().BeTrue(); } [Test] - public void should_be_able_to_check_space_on_ramdrive() + public void should_return_true_when_folder_is_parent_of_a_file() { - LinuxOnly(); - Subject.GetAvailableSpace("/run/").Should().NotBe(0); + var path = @"C:\Test\30.Rock.S01E01.Pilot.avi".AsOsAgnostic(); + + Subject.IsParent(_parent, path).Should().BeTrue(); } } } diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index c855d7ad9..44efa3221 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -165,6 +165,7 @@ + @@ -198,7 +199,6 @@ - diff --git a/NzbDrone.Core.Test/ProviderTests/XemCommunicationProviderTests/GetSceneTvdbMappingsFixture.cs b/NzbDrone.Core.Test/ProviderTests/XemCommunicationProviderTests/GetSceneTvdbMappingsFixture.cs index c8066ace5..65da5567d 100644 --- a/NzbDrone.Core.Test/ProviderTests/XemCommunicationProviderTests/GetSceneTvdbMappingsFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/XemCommunicationProviderTests/GetSceneTvdbMappingsFixture.cs @@ -7,57 +7,58 @@ using NzbDrone.Common; using NzbDrone.Core.Providers; using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common.Categories; namespace NzbDrone.Core.Test.ProviderTests.XemCommunicationProviderTests { [TestFixture] - + [IntegrationTest] public class GetSceneTvdbMappingsFixture : CoreTest { private void WithFailureJson() { Mocker.GetMock().Setup(s => s.DownloadString(It.IsAny())) - .Returns(ReadAllText("Files","Xem","Failure.txt")); + .Returns(ReadAllText("Files", "Xem", "Failure.txt")); } private void WithIdsJson() { Mocker.GetMock().Setup(s => s.DownloadString(It.IsAny())) - .Returns(ReadAllText("Files","Xem","Ids.txt")); + .Returns(ReadAllText("Files", "Xem", "Ids.txt")); } private void WithMappingsJson() { Mocker.GetMock().Setup(s => s.DownloadString(It.IsAny())) - .Returns(ReadAllText("Files","Xem","Mappings.txt")); + .Returns(ReadAllText("Files", "Xem", "Mappings.txt")); } [Test] public void should_throw_when_failure_is_found() { WithFailureJson(); - Assert.Throws(() => Mocker.Resolve().GetSceneTvdbMappings(12345)); + Assert.Throws(() => Mocker.Resolve().GetSceneTvdbMappings(12345)); } [Test] public void should_get_list_of_mappings() { WithMappingsJson(); - Mocker.Resolve().GetSceneTvdbMappings(12345).Should().NotBeEmpty(); + Mocker.Resolve().GetSceneTvdbMappings(12345).Should().NotBeEmpty(); } [Test] public void should_have_two_mappings() { WithMappingsJson(); - Mocker.Resolve().GetSceneTvdbMappings(12345).Should().HaveCount(2); + Mocker.Resolve().GetSceneTvdbMappings(12345).Should().HaveCount(2); } [Test] public void should_have_expected_results() { WithMappingsJson(); - var results = Mocker.Resolve().GetSceneTvdbMappings(12345); + var results = Mocker.Resolve().GetSceneTvdbMappings(12345); var first = results.First(); first.Scene.Absolute.Should().Be(1); first.Scene.Season.Should().Be(1); diff --git a/NzbDrone.Core.Test/ProviderTests/XemCommunicationProviderTests/GetXemSeriesIdsFixture.cs b/NzbDrone.Core.Test/ProviderTests/XemCommunicationProviderTests/GetXemSeriesIdsFixture.cs deleted file mode 100644 index 82035d5c8..000000000 --- a/NzbDrone.Core.Test/ProviderTests/XemCommunicationProviderTests/GetXemSeriesIdsFixture.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using FluentAssertions; -using Moq; -using NUnit.Framework; -using NzbDrone.Common; -using NzbDrone.Core.Providers; - -using NzbDrone.Core.Test.Framework; - -namespace NzbDrone.Core.Test.ProviderTests.XemCommunicationProviderTests -{ - [TestFixture] - - public class GetXemSeriesIdsFixture : CoreTest - { - private void WithFailureJson() - { - Mocker.GetMock().Setup(s => s.DownloadString(It.IsAny())) - .Returns(ReadAllText("Files", "Xem", "Failure.txt")); - } - - private void WithIdsJson() - { - Mocker.GetMock().Setup(s => s.DownloadString(It.IsAny())) - .Returns(ReadAllText("Files", "Xem", "Ids.txt")); - } - - private void WithMappingsJson() - { - Mocker.GetMock().Setup(s => s.DownloadString(It.IsAny())) - .Returns(ReadAllText("Files", "Xem", "Mappings.txt")); - } - - [Test] - public void should_throw_when_failure_is_found() - { - WithFailureJson(); - Assert.Throws(() => Mocker.Resolve().GetXemSeriesIds()); - } - - [Test] - public void should_get_list_of_int() - { - WithIdsJson(); - Mocker.Resolve().GetXemSeriesIds().Should().NotBeEmpty(); - } - - [Test] - public void should_have_two_ids() - { - WithIdsJson(); - Mocker.Resolve().GetXemSeriesIds().Should().HaveCount(2); - } - } -} \ No newline at end of file diff --git a/NzbDrone.Core.Test/Providers/XemProxyFixture.cs b/NzbDrone.Core.Test/Providers/XemProxyFixture.cs new file mode 100644 index 000000000..3bc1f73c2 --- /dev/null +++ b/NzbDrone.Core.Test/Providers/XemProxyFixture.cs @@ -0,0 +1,59 @@ +using System; +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common.Categories; + +namespace NzbDrone.Core.Test.Providers +{ + [TestFixture] + [IntegrationTest] + public class XemProxyFixture : CoreTest + { + [SetUp] + public void Setup() + { + UseRealHttp(); + } + + [Test] + public void get_series_ids() + { + Subject.GetXemSeriesIds().Should().NotBeEmpty(); + } + + + [Test] + [Ignore("XEM's data is not clean")] + public void get_mapping_for_all_series() + { + var ids = Subject.GetXemSeriesIds(); + + var randomIds = ids.OrderBy(x => Guid.NewGuid()).Take(5); + + foreach (var randomId in randomIds) + { + Subject.GetSceneTvdbMappings(randomId).Should().NotBeEmpty(); + } + } + + [Test] + public void should_throw_when_failure_is_found() + { + Assert.Throws(() => Subject.GetSceneTvdbMappings(12345)); + } + + + [Test] + public void should_get_mapping() + { + var result = Subject.GetSceneTvdbMappings(82807); + + result.Should().NotBeEmpty(); + result.Should().OnlyContain(c => c.Scene != null); + result.Should().OnlyContain(c => c.Tvdb != null); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 8d8c65364..26e301627 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -437,8 +437,8 @@ - - + + diff --git a/NzbDrone.Core/Providers/UpdateXemMappingsCommand.cs b/NzbDrone.Core/Providers/UpdateXemMappingsCommand.cs index a30c0c70d..5f35eaf1d 100644 --- a/NzbDrone.Core/Providers/UpdateXemMappingsCommand.cs +++ b/NzbDrone.Core/Providers/UpdateXemMappingsCommand.cs @@ -4,11 +4,6 @@ namespace NzbDrone.Core.Providers { public class UpdateXemMappingsCommand : Command { - public int? SeriesId { get; set; } - - public UpdateXemMappingsCommand(int? seriesId) - { - SeriesId = seriesId; - } + } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/XemCommunicationProvider.cs b/NzbDrone.Core/Providers/XemCommunicationProvider.cs deleted file mode 100644 index 5c0e07132..000000000 --- a/NzbDrone.Core/Providers/XemCommunicationProvider.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using NLog; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using NzbDrone.Common; -using NzbDrone.Common.Instrumentation; -using NzbDrone.Core.Model.Xem; - -namespace NzbDrone.Core.Providers -{ - public interface IXemCommunicationProvider - { - List GetXemSeriesIds(string origin = "tvdb"); - List GetSceneTvdbMappings(int id); - void CheckForFailureResult(string response); - } - - public class XemCommunicationProvider : IXemCommunicationProvider - { - private readonly IHttpProvider _httpProvider; - - private static readonly Logger _logger = NzbDroneLogger.GetLogger(); - - private const string XEM_BASE_URL = "http://thexem.de/map/"; - - public XemCommunicationProvider(IHttpProvider httpProvider) - { - _httpProvider = httpProvider; - } - - public List GetXemSeriesIds(string origin = "tvdb") - { - _logger.Trace("Fetching Series IDs from: {0}", origin); - - var url = String.Format("{0}havemap?origin={1}", XEM_BASE_URL, origin); - var response =_httpProvider.DownloadString(url); - - CheckForFailureResult(response); - - var result = JsonConvert.DeserializeObject>>(response); - - return result.Data.ToList(); - } - - public List GetSceneTvdbMappings(int id) - { - _logger.Trace("Fetching Mappings for: {0}", id); - var url = String.Format("{0}all?id={1}&origin=tvdb", XEM_BASE_URL, id); - var response = _httpProvider.DownloadString(url); - - CheckForFailureResult(response); - - var result = JsonConvert.DeserializeObject>(JObject.Parse(response).SelectToken("data").ToString()); - - return result; - } - - public void CheckForFailureResult(string response) - { - var result = JsonConvert.DeserializeObject>(response); - - if (result != null && result.Result.Equals("failure", StringComparison.InvariantCultureIgnoreCase)) - throw new Exception("Error response received from Xem: " + result.Message); - } - } -} diff --git a/NzbDrone.Core/Providers/XemProxy.cs b/NzbDrone.Core/Providers/XemProxy.cs new file mode 100644 index 000000000..0d500cdee --- /dev/null +++ b/NzbDrone.Core/Providers/XemProxy.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using NLog; +using NzbDrone.Core.Model.Xem; +using NzbDrone.Core.Rest; +using RestSharp; + +namespace NzbDrone.Core.Providers +{ + public interface IXemProxy + { + List GetXemSeriesIds(); + List GetSceneTvdbMappings(int id); + } + + public class XemProxy : IXemProxy + { + private readonly Logger _logger; + + private const string XEM_BASE_URL = "http://thexem.de/map/"; + + public XemProxy(Logger logger) + { + _logger = logger; + } + + + private static RestRequest BuildRequest(string resource) + { + var req = new RestRequest(resource, Method.GET); + req.AddParameter("origin", "tvdb"); + return req; + } + + public List GetXemSeriesIds() + { + _logger.Trace("Fetching Series IDs from"); + + var restClient = new RestClient(XEM_BASE_URL); + + var request = BuildRequest("havemap"); + + var response = restClient.ExecuteAndValidate>>(request); + CheckForFailureResult(response); + + return response.Data.ToList(); + } + + public List GetSceneTvdbMappings(int id) + { + _logger.Trace("Fetching Mappings for: {0}", id); + var url = String.Format("{0}all?id={1}&origin=tvdb", XEM_BASE_URL, id); + + + var restClient = new RestClient(XEM_BASE_URL); + + var request = BuildRequest("all"); + request.AddParameter("id", id); + + var response = restClient.ExecuteAndValidate>>(request); + CheckForFailureResult(response); + + return response.Data; + } + + private static void CheckForFailureResult(XemResult response) + { + if (response.Result.Equals("failure", StringComparison.InvariantCultureIgnoreCase) && + !response.Message.Contains("no show with the tvdb_id")) + { + throw new Exception("Error response received from Xem: " + response.Message); + } + } + } +} diff --git a/NzbDrone.Core/Providers/XemProvider.cs b/NzbDrone.Core/Providers/XemService.cs similarity index 69% rename from NzbDrone.Core/Providers/XemProvider.cs rename to NzbDrone.Core/Providers/XemService.cs index e66000afc..ac36a0bb7 100644 --- a/NzbDrone.Core/Providers/XemProvider.cs +++ b/NzbDrone.Core/Providers/XemService.cs @@ -3,9 +3,7 @@ using System.Collections.Generic; using System.Linq; using NLog; using NzbDrone.Common.Cache; -using NzbDrone.Common.Instrumentation; using NzbDrone.Core.Lifecycle; -using NzbDrone.Core.Messaging; using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Tv; @@ -13,35 +11,44 @@ using NzbDrone.Core.Tv.Events; namespace NzbDrone.Core.Providers { - public interface IXemProvider - { - void UpdateMappings(); - void UpdateMappings(int seriesId); - void UpdateMappings(Series series); - void PerformUpdate(Series series); - } - - public class XemProvider : IXemProvider, IExecute, IHandle, IHandleAsync + public class XemService : IExecute, IHandle, IHandleAsync { private readonly IEpisodeService _episodeService; - private readonly IXemCommunicationProvider _xemCommunicationProvider; + private readonly IXemProxy _xemProxy; private readonly ISeriesService _seriesService; + private readonly Logger _logger; private readonly ICached _cache; - private static readonly Logger _logger = NzbDroneLogger.GetLogger(); - - public XemProvider(IEpisodeService episodeService, - IXemCommunicationProvider xemCommunicationProvider, - ISeriesService seriesService, ICacheManger cacheManger) + public XemService(IEpisodeService episodeService, + IXemProxy xemProxy, + ISeriesService seriesService, ICacheManger cacheManger, Logger logger) { if (seriesService == null) throw new ArgumentNullException("seriesService"); _episodeService = episodeService; - _xemCommunicationProvider = xemCommunicationProvider; + _xemProxy = xemProxy; _seriesService = seriesService; + _logger = logger; + _logger = logger; _cache = cacheManger.GetCache(GetType()); } - public void UpdateMappings() + + public void Execute(UpdateXemMappingsCommand message) + { + UpdateMappings(); + } + + public void Handle(SeriesUpdatedEvent message) + { + UpdateMappings(message.Series); + } + + public void HandleAsync(ApplicationStartedEvent message) + { + GetXemSeriesIds(); + } + + private void UpdateMappings() { _logger.Trace("Starting scene numbering update"); @@ -66,20 +73,7 @@ namespace NzbDrone.Core.Providers } } - public void UpdateMappings(int seriesId) - { - var series = _seriesService.GetSeries(seriesId); - - if (series == null) - { - _logger.Trace("Series could not be found: {0}", seriesId); - return; - } - - UpdateMappings(series); - } - - public void UpdateMappings(Series series) + private void UpdateMappings(Series series) { if (!_cache.Find(series.TvdbId.ToString())) { @@ -90,17 +84,18 @@ namespace NzbDrone.Core.Providers PerformUpdate(series); } - public void PerformUpdate(Series series) + private void PerformUpdate(Series series) { _logger.Trace("Updating scene numbering mapping for: {0}", series); try { var episodesToUpdate = new List(); - var mappings = _xemCommunicationProvider.GetSceneTvdbMappings(series.TvdbId); + var mappings = _xemProxy.GetSceneTvdbMappings(series.TvdbId); - if (mappings == null) + if (!mappings.Any()) { - _logger.Trace("Mappings for: {0} are null, skipping", series); + _logger.Trace("Mappings for: {0} are empty, skipping", series); + _cache.Remove(series.TvdbId.ToString()); return; } @@ -142,7 +137,7 @@ namespace NzbDrone.Core.Providers { _cache.Clear(); - var ids = _xemCommunicationProvider.GetXemSeriesIds(); + var ids = _xemProxy.GetXemSeriesIds(); foreach (var id in ids) { @@ -151,27 +146,5 @@ namespace NzbDrone.Core.Providers return ids; } - - public void Execute(UpdateXemMappingsCommand message) - { - if (message.SeriesId.HasValue) - { - UpdateMappings(message.SeriesId.Value); - } - else - { - UpdateMappings(); - } - } - - public void Handle(SeriesUpdatedEvent message) - { - UpdateMappings(message.Series); - } - - public void HandleAsync(ApplicationStartedEvent message) - { - GetXemSeriesIds(); - } } }