From 4f0ca20808c0d82a3efab7a5117de57a2d9afa0d Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 3 Jan 2015 17:02:45 -0800 Subject: [PATCH] Fixed: Refreshing anime series won't cause an error --- .../MetadataSourceTests/TvdbProxyFixture.cs | 41 --------------- .../NzbDrone.Core.Test.csproj | 1 - .../TvTests/RefreshEpisodeServiceFixture.cs | 27 ---------- src/NzbDrone.Core/MetadataSource/TvDbProxy.cs | 5 +- .../MetadataSource/Tvdb/TvdbProxy.cs | 52 ------------------- src/NzbDrone.Core/NzbDrone.Core.csproj | 3 +- src/NzbDrone.Core/Tv/RefreshEpisodeService.cs | 22 +------- src/TVDBSharp/Models/Builder.cs | 4 ++ src/TVDBSharp/Models/Episode.cs | 5 ++ 9 files changed, 14 insertions(+), 146 deletions(-) delete mode 100644 src/NzbDrone.Core.Test/MetadataSourceTests/TvdbProxyFixture.cs delete mode 100644 src/NzbDrone.Core/MetadataSource/Tvdb/TvdbProxy.cs diff --git a/src/NzbDrone.Core.Test/MetadataSourceTests/TvdbProxyFixture.cs b/src/NzbDrone.Core.Test/MetadataSourceTests/TvdbProxyFixture.cs deleted file mode 100644 index 60ec11516..000000000 --- a/src/NzbDrone.Core.Test/MetadataSourceTests/TvdbProxyFixture.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using FluentAssertions; -using NUnit.Framework; -using NzbDrone.Core.MetadataSource.Tvdb; -using NzbDrone.Core.Test.Framework; -using NzbDrone.Core.Tv; -using NzbDrone.Test.Common.Categories; - -namespace NzbDrone.Core.Test.MetadataSourceTests -{ - [TestFixture] - [IntegrationTest] - public class TvdbProxyFixture : CoreTest - { - [TestCase(88031)] - [TestCase(179321)] - public void should_be_able_to_get_series_detail(int tvdbId) - { - UseRealHttp(); - - var episodes = Subject.GetEpisodeInfo(tvdbId); - - ValidateEpisodes(episodes); - } - - private void ValidateEpisodes(List episodes) - { - episodes.Should().NotBeEmpty(); - - episodes.GroupBy(e => e.SeasonNumber.ToString("000") + e.EpisodeNumber.ToString("000")) - .Max(e => e.Count()).Should().Be(1); - - episodes.Should().Contain(c => c.SeasonNumber > 0); - - episodes.Should().OnlyContain(c => c.SeasonNumber > 0 || c.EpisodeNumber > 0); - } - - - } -} diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 39e5ac214..7fb102c75 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -249,7 +249,6 @@ - diff --git a/src/NzbDrone.Core.Test/TvTests/RefreshEpisodeServiceFixture.cs b/src/NzbDrone.Core.Test/TvTests/RefreshEpisodeServiceFixture.cs index 6659777c0..8a482ee3f 100644 --- a/src/NzbDrone.Core.Test/TvTests/RefreshEpisodeServiceFixture.cs +++ b/src/NzbDrone.Core.Test/TvTests/RefreshEpisodeServiceFixture.cs @@ -6,7 +6,6 @@ using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Core.MetadataSource; -using NzbDrone.Core.MetadataSource.Tvdb; using NzbDrone.Core.Tv; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; @@ -73,13 +72,6 @@ namespace NzbDrone.Core.Test.TvTests .Callback>(e => _deletedEpisodes = e); } - private void GivenAnimeEpisodes(List episodes) - { - Mocker.GetMock() - .Setup(s => s.GetEpisodeInfo(It.IsAny())) - .Returns(episodes); - } - [Test] public void should_create_all_when_no_existing_episodes() { @@ -175,22 +167,10 @@ namespace NzbDrone.Core.Test.TvTests _deletedEpisodes.Should().BeEmpty(); } - [Test] - public void should_not_set_absolute_episode_number_for_non_anime() - { - Mocker.GetMock().Setup(c => c.GetEpisodeBySeries(It.IsAny())) - .Returns(new List()); - - Subject.RefreshEpisodeInfo(GetSeries(), GetEpisodes()); - - _insertedEpisodes.All(e => !e.AbsoluteEpisodeNumber.HasValue).Should().BeTrue(); - } - [Test] public void should_set_absolute_episode_number_for_anime() { var episodes = Builder.CreateListOfSize(3).Build().ToList(); - GivenAnimeEpisodes(episodes); Mocker.GetMock().Setup(c => c.GetEpisodeBySeries(It.IsAny())) .Returns(new List()); @@ -206,7 +186,6 @@ namespace NzbDrone.Core.Test.TvTests public void should_set_absolute_episode_number_even_if_not_previously_set_for_anime() { var episodes = Builder.CreateListOfSize(3).Build().ToList(); - GivenAnimeEpisodes(episodes); var existingEpisodes = episodes.JsonClone(); existingEpisodes.ForEach(e => e.AbsoluteEpisodeNumber = null); @@ -234,8 +213,6 @@ namespace NzbDrone.Core.Test.TvTests .With(e => e.AbsoluteEpisodeNumber = expectedAbsoluteNumber) .Build(); - GivenAnimeEpisodes(new List { episode }); - var existingEpisode = episode.JsonClone(); existingEpisode.SeasonNumber = 1; existingEpisode.EpisodeNumber = 1; @@ -265,8 +242,6 @@ namespace NzbDrone.Core.Test.TvTests episodes[0].SeasonNumber.Should().NotBe(episodes[1].SeasonNumber); episodes[0].EpisodeNumber.Should().NotBe(episodes[1].EpisodeNumber); - GivenAnimeEpisodes(episodes); - var existingEpisode = new Episode { SeasonNumber = episodes[0].SeasonNumber, @@ -297,8 +272,6 @@ namespace NzbDrone.Core.Test.TvTests episodes[3].AbsoluteEpisodeNumber = null; episodes[4].AbsoluteEpisodeNumber = null; - GivenAnimeEpisodes(episodes); - Mocker.GetMock().Setup(c => c.GetEpisodeBySeries(It.IsAny())) .Returns(new List()); diff --git a/src/NzbDrone.Core/MetadataSource/TvDbProxy.cs b/src/NzbDrone.Core/MetadataSource/TvDbProxy.cs index 8c0498120..2c3113040 100644 --- a/src/NzbDrone.Core/MetadataSource/TvDbProxy.cs +++ b/src/NzbDrone.Core/MetadataSource/TvDbProxy.cs @@ -160,12 +160,13 @@ namespace NzbDrone.Core.MetadataSource return series; } - private static Tv.Episode MapEpisode(TVDBSharp.Models.Episode traktEpisode) + private static Episode MapEpisode(TVDBSharp.Models.Episode traktEpisode) { - var episode = new Tv.Episode(); + var episode = new Episode(); episode.Overview = traktEpisode.Description; episode.SeasonNumber = traktEpisode.SeasonNumber; episode.EpisodeNumber = traktEpisode.EpisodeNumber; + episode.AbsoluteEpisodeNumber = traktEpisode.AbsoluteEpisodeNumber; episode.Title = traktEpisode.Title; if (traktEpisode.FirstAired != null) diff --git a/src/NzbDrone.Core/MetadataSource/Tvdb/TvdbProxy.cs b/src/NzbDrone.Core/MetadataSource/Tvdb/TvdbProxy.cs deleted file mode 100644 index 1694aa707..000000000 --- a/src/NzbDrone.Core/MetadataSource/Tvdb/TvdbProxy.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Xml.Linq; -using NzbDrone.Common.Extensions; -using NzbDrone.Common.Http; -using NzbDrone.Core.Indexers; -using NzbDrone.Core.Tv; - -namespace NzbDrone.Core.MetadataSource.Tvdb -{ - public interface ITvdbProxy - { - List GetEpisodeInfo(int tvdbSeriesId); - } - - public class TvdbProxy : ITvdbProxy - { - private readonly IHttpClient _httpClient; - - public TvdbProxy(IHttpClient httpClient) - { - _httpClient = httpClient; - } - - public List GetEpisodeInfo(int tvdbSeriesId) - { - var httpRequest = new HttpRequest("http://thetvdb.com/data/series/{tvdbId}/all/"); - httpRequest.AddSegment("tvdbId", tvdbSeriesId.ToString()); - var response = _httpClient.Get(httpRequest); - - var xml = XDocument.Load(new StringReader(response.Content)); - var episodes = xml.Descendants("Episode").Select(MapEpisode).ToList(); - return episodes; - } - - private static Episode MapEpisode(XElement item) - { - //TODO: We should map all the data incase we want to actually use it - var episode = new Episode(); - episode.SeasonNumber = item.TryGetValue("SeasonNumber", 0); - episode.EpisodeNumber = item.TryGetValue("EpisodeNumber", 0); - - if (item.TryGetValue("absolute_number").IsNotNullOrWhiteSpace()) - { - episode.AbsoluteEpisodeNumber = item.TryGetValue("absolute_number", 0); - } - - return episode; - } - } -} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 5b41ce309..5d77d8444 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -598,6 +598,7 @@ + @@ -623,8 +624,6 @@ - - diff --git a/src/NzbDrone.Core/Tv/RefreshEpisodeService.cs b/src/NzbDrone.Core/Tv/RefreshEpisodeService.cs index fefbd82c6..d5ad8018c 100644 --- a/src/NzbDrone.Core/Tv/RefreshEpisodeService.cs +++ b/src/NzbDrone.Core/Tv/RefreshEpisodeService.cs @@ -4,7 +4,6 @@ using System.Linq; using NLog; using NzbDrone.Common.Extensions; using NzbDrone.Core.Messaging.Events; -using NzbDrone.Core.MetadataSource.Tvdb; using NzbDrone.Core.Tv.Events; namespace NzbDrone.Core.Tv @@ -17,14 +16,12 @@ namespace NzbDrone.Core.Tv public class RefreshEpisodeService : IRefreshEpisodeService { private readonly IEpisodeService _episodeService; - private readonly ITvdbProxy _tvdbProxy; private readonly IEventAggregator _eventAggregator; private readonly Logger _logger; - public RefreshEpisodeService(IEpisodeService episodeService, ITvdbProxy tvdbProxy, IEventAggregator eventAggregator, Logger logger) + public RefreshEpisodeService(IEpisodeService episodeService, IEventAggregator eventAggregator, Logger logger) { _episodeService = episodeService; - _tvdbProxy = tvdbProxy; _eventAggregator = eventAggregator; _logger = logger; } @@ -153,23 +150,6 @@ namespace NzbDrone.Core.Tv private List MapAbsoluteEpisodeNumbers(Series series, List traktEpisodes) { - var tvdbEpisodes = _tvdbProxy.GetEpisodeInfo(series.TvdbId); - - foreach (var episode in traktEpisodes) - { - //I'd use single, but then I'd have to trust the tvdb data... and I don't - var tvdbEpisode = tvdbEpisodes.FirstOrDefault(e => e.SeasonNumber == episode.SeasonNumber && - e.EpisodeNumber == episode.EpisodeNumber); - - if (tvdbEpisode == null) - { - _logger.Debug("Cannot find matching episode from the tvdb: {0}x{1:00}", episode.SeasonNumber, episode.EpisodeNumber); - continue; - } - - episode.AbsoluteEpisodeNumber = tvdbEpisode.AbsoluteEpisodeNumber; - } - //Return all episodes with no abs number, but distinct for those with abs number return traktEpisodes.Where(e => e.AbsoluteEpisodeNumber.HasValue) .DistinctBy(e => e.AbsoluteEpisodeNumber.Value) diff --git a/src/TVDBSharp/Models/Builder.cs b/src/TVDBSharp/Models/Builder.cs index 74f633174..dd79c6f85 100644 --- a/src/TVDBSharp/Models/Builder.cs +++ b/src/TVDBSharp/Models/Builder.cs @@ -160,6 +160,10 @@ namespace TVDBSharp.Models Id = int.Parse(episodeNode.GetXmlData("id")), Title = episodeNode.GetXmlData("EpisodeName"), Description = episodeNode.GetXmlData("Overview"), + AbsoluteEpisodeNumber = + string.IsNullOrWhiteSpace(episodeNode.GetXmlData("absolute_number")) + ? (int?)null + : int.Parse(episodeNode.GetXmlData("absolute_number")), EpisodeNumber = int.Parse(episodeNode.GetXmlData("EpisodeNumber")), Director = episodeNode.GetXmlData("Director"), EpisodeImage = GetBannerUri(episodeNode.GetXmlData("filename")), diff --git a/src/TVDBSharp/Models/Episode.cs b/src/TVDBSharp/Models/Episode.cs index 8ec6c9501..e53d321ae 100644 --- a/src/TVDBSharp/Models/Episode.cs +++ b/src/TVDBSharp/Models/Episode.cs @@ -23,6 +23,11 @@ namespace TVDBSharp.Models /// public string Title { get; set; } + /// + /// This episode's absolute number + /// + public int? AbsoluteEpisodeNumber { get; set; } + /// /// This episode's number in the appropriate season. ///