Fixed: Attempt to refresh anime episodes by absolute numering when refreshing

This commit is contained in:
Mark McDowall 2014-07-10 19:23:57 -07:00
parent 8b2ef1a228
commit 55beec4908
3 changed files with 164 additions and 32 deletions

View File

@ -6,6 +6,7 @@ using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.MetadataSource; using NzbDrone.Core.MetadataSource;
using NzbDrone.Core.MetadataSource.Tvdb;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
@ -42,6 +43,15 @@ namespace NzbDrone.Core.Test.TvTests
return series; return series;
} }
private Series GetAnimeSeries()
{
var series = Builder<Series>.CreateNew().Build();
series.SeriesType = SeriesTypes.Anime;
series.Seasons = new List<Season>();
return series;
}
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
@ -61,6 +71,13 @@ namespace NzbDrone.Core.Test.TvTests
.Callback<List<Episode>>(e => _deletedEpisodes = e); .Callback<List<Episode>>(e => _deletedEpisodes = e);
} }
private void GivenAnimeEpisodes(List<Episode> episodes)
{
Mocker.GetMock<ITvdbProxy>()
.Setup(s => s.GetEpisodeInfo(It.IsAny<Int32>()))
.Returns(episodes);
}
[Test] [Test]
public void should_create_all_when_no_existing_episodes() public void should_create_all_when_no_existing_episodes()
{ {
@ -168,24 +185,102 @@ namespace NzbDrone.Core.Test.TvTests
} }
[Test] [Test]
[Ignore] public void should_set_absolute_episode_number_for_anime()
public void should_set_absolute_episode_number()
{ {
//TODO: Only run this against an anime series var episodes = Builder<Episode>.CreateListOfSize(3).Build().ToList();
GivenAnimeEpisodes(episodes);
Mocker.GetMock<IEpisodeService>().Setup(c => c.GetEpisodeBySeries(It.IsAny<int>())) Mocker.GetMock<IEpisodeService>().Setup(c => c.GetEpisodeBySeries(It.IsAny<int>()))
.Returns(new List<Episode>()); .Returns(new List<Episode>());
Subject.RefreshEpisodeInfo(GetSeries(), GetEpisodes()); Subject.RefreshEpisodeInfo(GetAnimeSeries(), episodes);
var season1 = _insertedEpisodes.Where(e => e.SeasonNumber == 1 && e.EpisodeNumber > 0); _insertedEpisodes.All(e => e.AbsoluteEpisodeNumber > 0).Should().BeTrue();
var season2episode1 = _insertedEpisodes.Single(e => e.SeasonNumber == 2 && e.EpisodeNumber == 1);
season2episode1.AbsoluteEpisodeNumber.Should().Be(season1.Count() + 1);
_insertedEpisodes.Where(e => e.SeasonNumber > 0 && e.EpisodeNumber > 0).All(e => e.AbsoluteEpisodeNumber > 0).Should().BeTrue();
_updatedEpisodes.Should().BeEmpty(); _updatedEpisodes.Should().BeEmpty();
_deletedEpisodes.Should().BeEmpty(); _deletedEpisodes.Should().BeEmpty();
} }
[Test]
public void should_set_absolute_episode_number_even_if_not_previously_set_for_anime()
{
var episodes = Builder<Episode>.CreateListOfSize(3).Build().ToList();
GivenAnimeEpisodes(episodes);
var existingEpisodes = episodes.JsonClone();
existingEpisodes.ForEach(e => e.AbsoluteEpisodeNumber = 0);
Mocker.GetMock<IEpisodeService>().Setup(c => c.GetEpisodeBySeries(It.IsAny<int>()))
.Returns(existingEpisodes);
Subject.RefreshEpisodeInfo(GetAnimeSeries(), episodes);
_insertedEpisodes.Should().BeEmpty();
_updatedEpisodes.All(e => e.AbsoluteEpisodeNumber > 0).Should().BeTrue();
_deletedEpisodes.Should().BeEmpty();
}
[Test]
public void should_get_new_season_and_episode_numbers_when_absolute_episode_number_match_found()
{
const Int32 expectedSeasonNumber = 10;
const Int32 expectedEpisodeNumber = 5;
const Int32 expectedAbsoluteNumber = 3;
var episode = Builder<Episode>.CreateNew()
.With(e => e.SeasonNumber = expectedSeasonNumber)
.With(e => e.EpisodeNumber = expectedEpisodeNumber)
.With(e => e.AbsoluteEpisodeNumber = expectedAbsoluteNumber)
.Build();
GivenAnimeEpisodes(new List<Episode> { episode });
var existingEpisode = episode.JsonClone();
existingEpisode.SeasonNumber = 1;
existingEpisode.EpisodeNumber = 1;
existingEpisode.AbsoluteEpisodeNumber = 1;
Mocker.GetMock<IEpisodeService>().Setup(c => c.GetEpisodeBySeries(It.IsAny<int>()))
.Returns(new List<Episode>{ existingEpisode });
Subject.RefreshEpisodeInfo(GetAnimeSeries(), new List<Episode> { episode });
_insertedEpisodes.Should().BeEmpty();
_deletedEpisodes.Should().BeEmpty();
_updatedEpisodes.First().SeasonNumber.Should().Be(expectedSeasonNumber);
_updatedEpisodes.First().EpisodeNumber.Should().Be(expectedEpisodeNumber);
_updatedEpisodes.First().AbsoluteEpisodeNumber.Should().Be(expectedAbsoluteNumber);
}
[Test]
public void should_prefer_absolute_match_over_season_and_epsiode_match()
{
var episodes = Builder<Episode>.CreateListOfSize(2)
.Build()
.ToList();
episodes[0].AbsoluteEpisodeNumber = 0;
episodes[0].SeasonNumber.Should().NotBe(episodes[1].SeasonNumber);
episodes[0].EpisodeNumber.Should().NotBe(episodes[1].EpisodeNumber);
episodes[0].AbsoluteEpisodeNumber.Should().NotBe(episodes[1].AbsoluteEpisodeNumber);
GivenAnimeEpisodes(episodes);
var existingEpisode = new Episode
{
SeasonNumber = episodes[0].SeasonNumber,
EpisodeNumber = episodes[0].EpisodeNumber,
AbsoluteEpisodeNumber = episodes[1].AbsoluteEpisodeNumber
};
Mocker.GetMock<IEpisodeService>().Setup(c => c.GetEpisodeBySeries(It.IsAny<int>()))
.Returns(new List<Episode> { existingEpisode });
Subject.RefreshEpisodeInfo(GetAnimeSeries(), episodes);
_updatedEpisodes.First().SeasonNumber.Should().Be(episodes[1].SeasonNumber);
_updatedEpisodes.First().EpisodeNumber.Should().Be(episodes[1].EpisodeNumber);
_updatedEpisodes.First().AbsoluteEpisodeNumber.Should().Be(episodes[1].AbsoluteEpisodeNumber);
}
} }
} }

View File

@ -49,12 +49,12 @@ namespace NzbDrone.Core.Test.TvTests
[Test] [Test]
public void should_monitor_new_seasons_automatically() public void should_monitor_new_seasons_automatically()
{ {
var series = _series.JsonClone(); var newSeriesInfo = _series.JsonClone();
series.Seasons.Add(Builder<Season>.CreateNew() newSeriesInfo.Seasons.Add(Builder<Season>.CreateNew()
.With(s => s.SeasonNumber = 2) .With(s => s.SeasonNumber = 2)
.Build()); .Build());
GivenNewSeriesInfo(series); GivenNewSeriesInfo(newSeriesInfo);
Subject.Execute(new RefreshSeriesCommand(_series.Id)); Subject.Execute(new RefreshSeriesCommand(_series.Id));
@ -77,5 +77,19 @@ namespace NzbDrone.Core.Test.TvTests
Mocker.GetMock<ISeriesService>() Mocker.GetMock<ISeriesService>()
.Verify(v => v.UpdateSeries(It.Is<Series>(s => s.Seasons.Count == 2 && s.Seasons.Single(season => season.SeasonNumber == 0).Monitored == false))); .Verify(v => v.UpdateSeries(It.Is<Series>(s => s.Seasons.Count == 2 && s.Seasons.Single(season => season.SeasonNumber == 0).Monitored == false)));
} }
[Test]
public void should_update_tvrage_id_if_changed()
{
var newSeriesInfo = _series.JsonClone();
newSeriesInfo.TvRageId = _series.TvRageId + 1;
GivenNewSeriesInfo(newSeriesInfo);
Subject.Execute(new RefreshSeriesCommand(_series.Id));
Mocker.GetMock<ISeriesService>()
.Verify(v => v.UpdateSeries(It.Is<Series>(s => s.TvRageId == newSeriesInfo.TvRageId)));
}
} }
} }

View File

@ -42,11 +42,16 @@ namespace NzbDrone.Core.Tv
var newList = new List<Episode>(); var newList = new List<Episode>();
var dupeFreeRemoteEpisodes = remoteEpisodes.DistinctBy(m => new { m.SeasonNumber, m.EpisodeNumber }).ToList(); var dupeFreeRemoteEpisodes = remoteEpisodes.DistinctBy(m => new { m.SeasonNumber, m.EpisodeNumber }).ToList();
foreach (var episode in dupeFreeRemoteEpisodes.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber)) if (series.SeriesType == SeriesTypes.Anime)
{
dupeFreeRemoteEpisodes = MapAbsoluteEpisodeNumbers(series, dupeFreeRemoteEpisodes);
}
foreach (var episode in OrderEpsiodes(series, dupeFreeRemoteEpisodes))
{ {
try try
{ {
var episodeToUpdate = existingEpisodes.FirstOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber); var episodeToUpdate = GetEpisodeToUpdate(series, episode, existingEpisodes);
if (episodeToUpdate != null) if (episodeToUpdate != null)
{ {
@ -63,6 +68,7 @@ namespace NzbDrone.Core.Tv
episodeToUpdate.SeriesId = series.Id; episodeToUpdate.SeriesId = series.Id;
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber; episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
episodeToUpdate.SeasonNumber = episode.SeasonNumber; episodeToUpdate.SeasonNumber = episode.SeasonNumber;
episodeToUpdate.AbsoluteEpisodeNumber = episode.AbsoluteEpisodeNumber;
episodeToUpdate.Title = episode.Title; episodeToUpdate.Title = episode.Title;
episodeToUpdate.Overview = episode.Overview; episodeToUpdate.Overview = episode.Overview;
episodeToUpdate.AirDate = episode.AirDate; episodeToUpdate.AirDate = episode.AirDate;
@ -70,13 +76,6 @@ namespace NzbDrone.Core.Tv
episodeToUpdate.Ratings = episode.Ratings; episodeToUpdate.Ratings = episode.Ratings;
episodeToUpdate.Images = episode.Images; episodeToUpdate.Images = episode.Images;
//Reset the absolute episode number to zero if the series is not anime
if (series.SeriesType != SeriesTypes.Anime)
{
episodeToUpdate.AbsoluteEpisodeNumber = 0;
}
successCount++; successCount++;
} }
catch (Exception e) catch (Exception e)
@ -91,7 +90,6 @@ namespace NzbDrone.Core.Tv
allEpisodes.AddRange(updateList); allEpisodes.AddRange(updateList);
AdjustMultiEpisodeAirTime(series, allEpisodes); AdjustMultiEpisodeAirTime(series, allEpisodes);
SetAbsoluteEpisodeNumber(series, allEpisodes);
_episodeService.DeleteMany(existingEpisodes); _episodeService.DeleteMany(existingEpisodes);
_episodeService.UpdateMany(updateList); _episodeService.UpdateMany(updateList);
@ -153,18 +151,11 @@ namespace NzbDrone.Core.Tv
} }
} }
private void SetAbsoluteEpisodeNumber(Series series, IEnumerable<Episode> allEpisodes) private List<Episode> MapAbsoluteEpisodeNumbers(Series series, List<Episode> traktEpisodes)
{ {
if (series.SeriesType != SeriesTypes.Anime)
{
_logger.Debug("Skipping absolute number lookup for non-anime");
return;
}
var tvdbEpisodes = _tvdbProxy.GetEpisodeInfo(series.TvdbId); var tvdbEpisodes = _tvdbProxy.GetEpisodeInfo(series.TvdbId);
foreach (var episode in allEpisodes) foreach (var episode in traktEpisodes)
{ {
//I'd use single, but then I'd have to trust the tvdb data... and I don't //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 && var tvdbEpisode = tvdbEpisodes.FirstOrDefault(e => e.SeasonNumber == episode.SeasonNumber &&
@ -178,6 +169,38 @@ namespace NzbDrone.Core.Tv
episode.AbsoluteEpisodeNumber = tvdbEpisode.AbsoluteEpisodeNumber; episode.AbsoluteEpisodeNumber = tvdbEpisode.AbsoluteEpisodeNumber;
} }
return traktEpisodes.DistinctBy(e => e.AbsoluteEpisodeNumber).ToList();
}
private Episode GetEpisodeToUpdate(Series series, Episode episode, IEnumerable<Episode> existingEpisodes)
{
if (series.SeriesType == SeriesTypes.Anime)
{
if (episode.AbsoluteEpisodeNumber > 0)
{
return existingEpisodes.FirstOrDefault(e => e.AbsoluteEpisodeNumber == episode.AbsoluteEpisodeNumber);
}
}
return existingEpisodes.FirstOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
}
private IEnumerable<Episode> OrderEpsiodes(Series series, List<Episode> episodes)
{
if (series.SeriesType == SeriesTypes.Anime)
{
var withAbs = episodes.Where(e => e.AbsoluteEpisodeNumber > 0)
.OrderBy(e => e.AbsoluteEpisodeNumber);
var withoutAbs = episodes.Where(e => e.AbsoluteEpisodeNumber == 0)
.OrderBy(e => e.SeasonNumber)
.ThenBy(e => e.EpisodeNumber);
return withAbs.Concat(withoutAbs);
}
return episodes.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber);
} }
} }
} }