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

Xem cleanup.

This commit is contained in:
kay.one 2013-09-16 23:14:47 -07:00
parent b8827096b9
commit 430f76e6c7
11 changed files with 218 additions and 237 deletions

View File

@ -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<DiskProvider>
public class FreeSpaceFixture : TestBase<DiskProvider>
{
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_get_free_space_for_drive_that_doesnt_exist()
{
WindowsOnly();
Assert.Throws<DirectoryNotFoundException>(() => Subject.GetAvailableSpace("J:\\").Should().NotBe(0));
}
[Test]
public void should_return_true_when_folder_is_parent_of_a_file()
public void should_be_able_to_check_space_on_ramdrive()
{
var path = @"C:\Test\30.Rock.S01E01.Pilot.avi".AsOsAgnostic();
Subject.IsParent(_parent, path).Should().BeTrue();
LinuxOnly();
Subject.GetAvailableSpace("/run/").Should().NotBe(0);
}
}
}

View File

@ -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<DiskProvider>
public class IsParentFixture : TestBase<DiskProvider>
{
[Test]
public void should_get_free_space_for_folder()
{
var path = @"C:\".AsOsAgnostic();
private string _parent = @"C:\Test".AsOsAgnostic();
Subject.GetAvailableSpace(path).Should().NotBe(0);
[Test]
public void should_return_false_when_not_a_child()
{
var path = @"C:\Another Folder".AsOsAgnostic();
Subject.IsParent(_parent, path).Should().BeFalse();
}
[Test]
public void should_get_free_space_for_folder_that_doesnt_exist()
public void should_return_true_when_folder_is_parent_of_another_folder()
{
var path = @"C:\".AsOsAgnostic();
var path = @"C:\Test\TV".AsOsAgnostic();
Subject.GetAvailableSpace(Path.Combine(path, "invalidFolder")).Should().NotBe(0);
}
[Test]
public void should_get_free_space_for_drive_that_doesnt_exist()
{
WindowsOnly();
Assert.Throws<DirectoryNotFoundException>(() => 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();
}
}
}

View File

@ -165,6 +165,7 @@
<Compile Include="OrganizerTests\BuildFilePathFixture.cs" />
<Compile Include="ParserTests\ParsingServiceTests\GetEpisodesFixture.cs" />
<Compile Include="ParserTests\ParsingServiceTests\MapFixture.cs" />
<Compile Include="Providers\XemProxyFixture.cs" />
<Compile Include="Qualities\QualitySizeRepositoryFixture.cs" />
<Compile Include="Qualities\QualityProfileRepositoryFixture.cs" />
<Compile Include="RootFolderTests\FreeSpaceOnDrivesFixture.cs" />
@ -198,7 +199,6 @@
<Compile Include="TvTests\SeriesServiceTests\UpdateSeriesFixture.cs" />
<Compile Include="UpdateTests\UpdateServiceFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetSceneTvdbMappingsFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetXemSeriesIdsFixture.cs" />
<Compile Include="HelperTests\SortHelperTest.cs" />
<Compile Include="DecisionEngineTests\AcceptableSizeSpecificationFixture.cs" />
<Compile Include="Qualities\QualitySizeServiceFixture.cs" />

View File

@ -7,57 +7,58 @@
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<IHttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(ReadAllText("Files","Xem","Failure.txt"));
.Returns(ReadAllText("Files", "Xem", "Failure.txt"));
}
private void WithIdsJson()
{
Mocker.GetMock<IHttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(ReadAllText("Files","Xem","Ids.txt"));
.Returns(ReadAllText("Files", "Xem", "Ids.txt"));
}
private void WithMappingsJson()
{
Mocker.GetMock<IHttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(ReadAllText("Files","Xem","Mappings.txt"));
.Returns(ReadAllText("Files", "Xem", "Mappings.txt"));
}
[Test]
public void should_throw_when_failure_is_found()
{
WithFailureJson();
Assert.Throws<Exception>(() => Mocker.Resolve<XemCommunicationProvider>().GetSceneTvdbMappings(12345));
Assert.Throws<Exception>(() => Mocker.Resolve<XemProxy>().GetSceneTvdbMappings(12345));
}
[Test]
public void should_get_list_of_mappings()
{
WithMappingsJson();
Mocker.Resolve<XemCommunicationProvider>().GetSceneTvdbMappings(12345).Should().NotBeEmpty();
Mocker.Resolve<XemProxy>().GetSceneTvdbMappings(12345).Should().NotBeEmpty();
}
[Test]
public void should_have_two_mappings()
{
WithMappingsJson();
Mocker.Resolve<XemCommunicationProvider>().GetSceneTvdbMappings(12345).Should().HaveCount(2);
Mocker.Resolve<XemProxy>().GetSceneTvdbMappings(12345).Should().HaveCount(2);
}
[Test]
public void should_have_expected_results()
{
WithMappingsJson();
var results = Mocker.Resolve<XemCommunicationProvider>().GetSceneTvdbMappings(12345);
var results = Mocker.Resolve<XemProxy>().GetSceneTvdbMappings(12345);
var first = results.First();
first.Scene.Absolute.Should().Be(1);
first.Scene.Season.Should().Be(1);

View File

@ -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<IHttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(ReadAllText("Files", "Xem", "Failure.txt"));
}
private void WithIdsJson()
{
Mocker.GetMock<IHttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(ReadAllText("Files", "Xem", "Ids.txt"));
}
private void WithMappingsJson()
{
Mocker.GetMock<IHttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(ReadAllText("Files", "Xem", "Mappings.txt"));
}
[Test]
public void should_throw_when_failure_is_found()
{
WithFailureJson();
Assert.Throws<Exception>(() => Mocker.Resolve<XemCommunicationProvider>().GetXemSeriesIds());
}
[Test]
public void should_get_list_of_int()
{
WithIdsJson();
Mocker.Resolve<XemCommunicationProvider>().GetXemSeriesIds().Should().NotBeEmpty();
}
[Test]
public void should_have_two_ids()
{
WithIdsJson();
Mocker.Resolve<XemCommunicationProvider>().GetXemSeriesIds().Should().HaveCount(2);
}
}
}

View File

@ -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<XemProxy>
{
[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<Exception>(() => 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);
}
}
}

View File

@ -437,8 +437,8 @@
<Compile Include="Model\Xbmc\TvShowResponse.cs" />
<Compile Include="Model\Xbmc\TvShow.cs" />
<Compile Include="Model\Xbmc\VersionResult.cs" />
<Compile Include="Providers\XemCommunicationProvider.cs" />
<Compile Include="Providers\XemProvider.cs" />
<Compile Include="Providers\XemProxy.cs" />
<Compile Include="Providers\XemService.cs" />
<Compile Include="Qualities\Quality.cs" />
<Compile Include="Tv\Season.cs" />
<Compile Include="Configuration\ConfigService.cs">

View File

@ -4,11 +4,6 @@ namespace NzbDrone.Core.Providers
{
public class UpdateXemMappingsCommand : Command
{
public int? SeriesId { get; set; }
public UpdateXemMappingsCommand(int? seriesId)
{
SeriesId = seriesId;
}
}
}

View File

@ -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<Int32> GetXemSeriesIds(string origin = "tvdb");
List<XemSceneTvdbMapping> 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<Int32> 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<XemResult<List<Int32>>>(response);
return result.Data.ToList();
}
public List<XemSceneTvdbMapping> 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<List<XemSceneTvdbMapping>>(JObject.Parse(response).SelectToken("data").ToString());
return result;
}
public void CheckForFailureResult(string response)
{
var result = JsonConvert.DeserializeObject<XemResult<dynamic>>(response);
if (result != null && result.Result.Equals("failure", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Error response received from Xem: " + result.Message);
}
}
}

View File

@ -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<Int32> GetXemSeriesIds();
List<XemSceneTvdbMapping> 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<Int32> GetXemSeriesIds()
{
_logger.Trace("Fetching Series IDs from");
var restClient = new RestClient(XEM_BASE_URL);
var request = BuildRequest("havemap");
var response = restClient.ExecuteAndValidate<XemResult<List<Int32>>>(request);
CheckForFailureResult(response);
return response.Data.ToList();
}
public List<XemSceneTvdbMapping> 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<XemResult<List<XemSceneTvdbMapping>>>(request);
CheckForFailureResult(response);
return response.Data;
}
private static void CheckForFailureResult<T>(XemResult<T> 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);
}
}
}
}

View File

@ -3,9 +3,7 @@
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 @@
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<UpdateXemMappingsCommand>, IHandle<SeriesUpdatedEvent>, IHandleAsync<ApplicationStartedEvent>
public class XemService : IExecute<UpdateXemMappingsCommand>, IHandle<SeriesUpdatedEvent>, IHandleAsync<ApplicationStartedEvent>
{
private readonly IEpisodeService _episodeService;
private readonly IXemCommunicationProvider _xemCommunicationProvider;
private readonly IXemProxy _xemProxy;
private readonly ISeriesService _seriesService;
private readonly Logger _logger;
private readonly ICached<bool> _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<bool>(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 @@ public void UpdateMappings()
}
}
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 @@ public void UpdateMappings(Series series)
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<Episode>();
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 @@ private List<int> GetXemSeriesIds()
{
_cache.Clear();
var ids = _xemCommunicationProvider.GetXemSeriesIds();
var ids = _xemProxy.GetXemSeriesIds();
foreach (var id in ids)
{
@ -151,27 +146,5 @@ private List<int> GetXemSeriesIds()
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();
}
}
}