1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-09 04:22:30 +01:00

EpisodeSearch properly uses PerformSearch.

PerformSearch will handle shows that air daily.
This commit is contained in:
Mark McDowall 2011-11-25 22:13:47 -08:00
parent e16f83c433
commit cadccb4360
6 changed files with 294 additions and 27 deletions

View File

@ -99,6 +99,7 @@
<Compile Include="JobTests\RecentBacklogSearchJobTest.cs" />
<Compile Include="ProviderTests\ReferenceDataProviderTest.cs" />
<Compile Include="ProviderTests\NotificationProviderTests\NotificationProviderFixture.cs" />
<Compile Include="ProviderTests\SearchProviderTests\SearchFixture.cs" />
<Compile Include="ProviderTests\SearchProviderTests\PerformSearchFixture.cs" />
<Compile Include="ProviderTests\SearchProviderTests\ProcessSearchResultsFixture.cs" />
<Compile Include="ProviderTests\NewznabProviderTest.cs" />

View File

@ -56,6 +56,8 @@ private void BuildIndexers(IList<EpisodeParseResult> parseResults)
_episodeIndexer1 = new Mock<IndexerBase>();
_episodeIndexer1.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer1.Setup(c => c.FetchDailyEpisode(It.IsAny<string>(), It.IsAny<DateTime>()))
.Returns(parseResults);
_episodeIndexer1.Setup(c => c.FetchSeason(It.IsAny<string>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer1.Setup(c => c.FetchPartialSeason(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
@ -65,6 +67,8 @@ private void BuildIndexers(IList<EpisodeParseResult> parseResults)
_episodeIndexer2 = new Mock<IndexerBase>();
_episodeIndexer2.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer2.Setup(c => c.FetchDailyEpisode(It.IsAny<string>(), It.IsAny<DateTime>()))
.Returns(parseResults);
_episodeIndexer2.Setup(c => c.FetchSeason(It.IsAny<string>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer2.Setup(c => c.FetchPartialSeason(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
@ -123,6 +127,15 @@ private void VerifyFetchEpisode(Times times)
, times);
}
private void VerifyFetchDailyEpisode(Times times)
{
_episodeIndexer1.Verify(v => v.FetchDailyEpisode(_series.Title, It.IsAny<DateTime>())
, times);
_episodeIndexer2.Verify(v => v.FetchDailyEpisode(_series.Title, It.IsAny<DateTime>())
, times);
}
private void VerifyFetchEpisodeWithSceneName(Times times)
{
_episodeIndexer1.Verify(v => v.FetchEpisode(SCENE_NAME, SEASON_NUMBER, It.IsAny<int>())
@ -210,6 +223,21 @@ public void PerformSearch_for_episode_should_call_FetchEpisode()
VerifyFetchEpisode(Times.Once());
}
[Test]
public void PerformSearch_for_daily_episode_should_call_FetchEpisode()
{
//Setup
_series.IsDaily = true;
//Act
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes);
//Assert
result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
VerifyFetchDailyEpisode(Times.Once());
}
[Test]
public void PerformSearch_for_partial_season_should_call_FetchPartialSeason()
{

View File

@ -0,0 +1,206 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Indexer;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
{
public class SearchFixture : CoreTest
{
private const string SCENE_NAME = "Scene Name";
private const int SEASON_NUMBER = 5;
private const int PARSE_RESULT_COUNT = 10;
private Mock<IndexerBase> _episodeIndexer1;
private Mock<IndexerBase> _episodeIndexer2;
private Mock<IndexerBase> _brokenIndexer;
private Mock<IndexerBase> _nullIndexer;
private List<IndexerBase> _indexers;
private Series _series;
private IList<Episode> _episodes;
[SetUp]
public void Setup()
{
var parseResults = Builder<EpisodeParseResult>.CreateListOfSize(PARSE_RESULT_COUNT)
.Build();
_series = Builder<Series>.CreateNew()
.Build();
_episodes = Builder<Episode>.CreateListOfSize(1)
.Build();
BuildIndexers(parseResults);
_indexers = new List<IndexerBase> { _episodeIndexer1.Object, _episodeIndexer2.Object };
Mocker.GetMock<IndexerProvider>()
.Setup(c => c.GetEnabledIndexers())
.Returns(_indexers);
}
private void BuildIndexers(IList<EpisodeParseResult> parseResults)
{
_episodeIndexer1 = new Mock<IndexerBase>();
_episodeIndexer1.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer1.Setup(c => c.FetchSeason(It.IsAny<string>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer1.Setup(c => c.FetchPartialSeason(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer2 = new Mock<IndexerBase>();
_episodeIndexer2.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer2.Setup(c => c.FetchSeason(It.IsAny<string>(), It.IsAny<int>()))
.Returns(parseResults);
_episodeIndexer2.Setup(c => c.FetchPartialSeason(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns(parseResults);
_brokenIndexer = new Mock<IndexerBase>();
_brokenIndexer.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.Throws(new Exception());
_nullIndexer = new Mock<IndexerBase>();
_nullIndexer.Setup(c => c.FetchEpisode(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns<List<EpisodeParseResult>>(null);
}
private void WithTwoGoodOneBrokenIndexer()
{
_indexers = new List<IndexerBase> { _episodeIndexer1.Object, _brokenIndexer.Object, _episodeIndexer2.Object };
Mocker.GetMock<IndexerProvider>()
.Setup(c => c.GetEnabledIndexers())
.Returns(_indexers);
}
private void WithNullIndexers()
{
_indexers = new List<IndexerBase> { _nullIndexer.Object, _nullIndexer.Object };
Mocker.GetMock<IndexerProvider>()
.Setup(c => c.GetEnabledIndexers())
.Returns(_indexers);
}
private void WithSceneName()
{
Mocker.GetMock<SceneMappingProvider>()
.Setup(s => s.GetSceneName(_series.SeriesId)).Returns(SCENE_NAME);
}
private void With30Episodes()
{
_episodes = Builder<Episode>.CreateListOfSize(30)
.Build();
}
private void WithNullEpisodes()
{
_episodes = null;
}
private void VerifyFetchEpisode(Times times)
{
_episodeIndexer1.Verify(v => v.FetchEpisode(_series.Title, SEASON_NUMBER, It.IsAny<int>())
, times);
_episodeIndexer2.Verify(v => v.FetchEpisode(_series.Title, SEASON_NUMBER, It.IsAny<int>())
, times);
}
private void VerifyFetchEpisodeWithSceneName(Times times)
{
_episodeIndexer1.Verify(v => v.FetchEpisode(SCENE_NAME, SEASON_NUMBER, It.IsAny<int>())
, times);
_episodeIndexer2.Verify(v => v.FetchEpisode(SCENE_NAME, SEASON_NUMBER, It.IsAny<int>())
, times);
}
private void VerifyFetchEpisodeBrokenIndexer(Times times)
{
_brokenIndexer.Verify(v => v.FetchEpisode(It.IsAny<string>(), SEASON_NUMBER, It.IsAny<int>())
, times);
}
private void VerifyFetchPartialSeason(Times times)
{
_episodeIndexer1.Verify(v => v.FetchPartialSeason(_series.Title, SEASON_NUMBER, It.IsAny<int>())
, times);
_episodeIndexer2.Verify(v => v.FetchPartialSeason(_series.Title, SEASON_NUMBER, It.IsAny<int>())
, times);
}
private void VerifyFetchSeason(Times times)
{
_episodeIndexer1.Verify(v => v.FetchSeason(_series.Title, SEASON_NUMBER), times);
_episodeIndexer1.Verify(v => v.FetchSeason(_series.Title, SEASON_NUMBER), times);
}
[Test]
public void SeasonSearch_should_skip_daily_series()
{
//Setup
_series.IsDaily = true;
Mocker.GetMock<SeriesProvider>().Setup(s => s.GetSeries(1)).Returns(_series);
//Act
var result = Mocker.Resolve<SearchProvider>().SeasonSearch(MockNotification, _series.SeriesId, 1);
//Assert
result.Should().BeFalse();
}
[Test]
public void PartialSeasonSearch_should_skip_daily_series()
{
//Setup
_series.IsDaily = true;
Mocker.GetMock<SeriesProvider>().Setup(s => s.GetSeries(1)).Returns(_series);
//Act
var result = Mocker.Resolve<SearchProvider>().PartialSeasonSearch(MockNotification, _series.SeriesId, 1);
//Assert
result.Should().BeEmpty();
}
[Test]
public void EpisodeSearch_should_skip_if_air_date_is_null()
{
//Setup
_series.IsDaily = true;
var episode = _episodes.First();
episode.AirDate = null;
episode.Series = _series;
Mocker.GetMock<EpisodeProvider>().Setup(s => s.GetEpisode(episode.EpisodeId))
.Returns(episode);
//Act
var result = Mocker.Resolve<SearchProvider>().EpisodeSearch(MockNotification, episode.EpisodeId);
//Assert
result.Should().BeFalse();
ExceptionVerification.ExcpectedWarns(1);
}
}
}

View File

@ -179,6 +179,33 @@ public virtual IList<EpisodeParseResult> FetchEpisode(string seriesTitle, int se
}
public virtual IList<EpisodeParseResult> FetchDailyEpisode(string seriesTitle, DateTime airDate)
{
_logger.Debug("Searching {0} for {1}-{2}", Name, seriesTitle, airDate.ToShortDateString());
var result = new List<EpisodeParseResult>();
var searchModel = new SearchModel
{
SeriesTitle = GetQueryTitle(seriesTitle),
AirDate = airDate,
SearchType = SearchType.DailySearch
};
var searchUrls = GetSearchUrls(searchModel);
foreach (var url in searchUrls)
{
result.AddRange(Fetch(url));
}
result = result.Where(e => e.CleanTitle == Parser.NormalizeTitle(seriesTitle)).ToList();
_logger.Info("Finished searching {0} for {1}-{2}, Found {3}", Name, seriesTitle, airDate.ToShortDateString(), result.Count);
return result;
}
private IEnumerable<EpisodeParseResult> Fetch(string url)
{
var result = new List<EpisodeParseResult>();

View File

@ -11,12 +11,15 @@ public class UpdateInfoJob : IJob
{
private readonly SeriesProvider _seriesProvider;
private readonly EpisodeProvider _episodeProvider;
private readonly ReferenceDataProvider _referenceDataProvider;
[Inject]
public UpdateInfoJob(SeriesProvider seriesProvider, EpisodeProvider episodeProvider)
public UpdateInfoJob(SeriesProvider seriesProvider, EpisodeProvider episodeProvider,
ReferenceDataProvider referenceDataProvider)
{
_seriesProvider = seriesProvider;
_episodeProvider = episodeProvider;
_referenceDataProvider = referenceDataProvider;
}
public UpdateInfoJob()
@ -46,6 +49,9 @@ public virtual void Start(ProgressNotification notification, int targetId, int s
seriesToUpdate = new List<Series>() { _seriesProvider.GetSeries(targetId) };
}
//Update any Daily Series in the DB with the IsDaily flag
_referenceDataProvider.UpdateDailySeries();
foreach (var series in seriesToUpdate)
{
notification.CurrentMessage = "Updating " + series.Title;

View File

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -50,6 +51,10 @@ public virtual bool SeasonSearch(ProgressNotification notification, int seriesId
return false;
}
//Return false if the series is a daily series (we only support individual episode searching
if (series.IsDaily)
return false;
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
var reports = PerformSearch(notification, series, seasonNumber);
@ -96,6 +101,10 @@ public virtual List<int> PartialSeasonSearch(ProgressNotification notification,
return new List<int>();
}
//Return empty list if the series is a daily series (we only support individual episode searching
if (series.IsDaily)
return new List<int>();
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
var episodes = _episodeProvider.GetEpisodesBySeason(seriesId, seasonNumber);
@ -121,36 +130,18 @@ public virtual bool EpisodeSearch(ProgressNotification notification, int episode
Logger.Error("Unable to find an episode {0} in database", episodeId);
return false;
}
notification.CurrentMessage = "Searching for " + episode;
var series = _seriesProvider.GetSeries(episode.SeriesId);
var indexers = _indexerProvider.GetEnabledIndexers();
var reports = new List<EpisodeParseResult>();
var title = _sceneMappingProvider.GetSceneName(series.SeriesId);
if (string.IsNullOrWhiteSpace(title))
if (episode.Series.IsDaily && !episode.AirDate.HasValue)
{
title = series.Title;
Logger.Warn("AirDate is not Valid for: {0}", episode);
return false;
}
foreach (var indexer in indexers)
{
try
{
//notification.CurrentMessage = String.Format("Searching for {0} in {1}", episode, indexer.Name);
//TODO:Add support for daily episodes, maybe search using both date and season/episode?
var indexerResults = indexer.FetchEpisode(title, episode.SeasonNumber, episode.EpisodeNumber);
reports.AddRange(indexerResults);
}
catch (Exception e)
{
Logger.ErrorException("An error has occurred while fetching items from " + indexer.Name, e);
}
}
var reports = PerformSearch(notification, series, episode.SeasonNumber, new List<Episode> { episode });
Logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
notification.CurrentMessage = "Processing search results";
@ -184,15 +175,23 @@ public List<EpisodeParseResult> PerformSearch(ProgressNotification notification,
if (episodes == null)
reports.AddRange(indexer.FetchSeason(title, seasonNumber));
else if(episodes.Count == 1)
reports.AddRange(indexer.FetchEpisode(title, seasonNumber, episodes.First().EpisodeNumber));
//Treat as single episode
else if (episodes.Count == 1)
{
if (!series.IsDaily)
reports.AddRange(indexer.FetchEpisode(title, seasonNumber, episodes.First().EpisodeNumber));
//Daily Episode
else
reports.AddRange(indexer.FetchDailyEpisode(title, episodes.First().AirDate.Value));
}
//Treat as Partial Season
else
{
var prefixes = GetEpisodeNumberPrefixes(episodes.Select(s => s.EpisodeNumber));
foreach(var episodePrefix in prefixes)
foreach (var episodePrefix in prefixes)
{
reports.AddRange(indexer.FetchPartialSeason(title, seasonNumber, episodePrefix));
}