Refactored Search, less work in the searching

pull/4/head
Mark McDowall 12 years ago
parent 6f308b54f2
commit d065876991

@ -18,6 +18,7 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
{ {
private const string SCENE_NAME = "Scene Name"; private const string SCENE_NAME = "Scene Name";
private const int SEASON_NUMBER = 5; private const int SEASON_NUMBER = 5;
private const int EPISODE_NUMBER = 1;
private const int PARSE_RESULT_COUNT = 10; private const int PARSE_RESULT_COUNT = 10;
private Mock<IndexerBase> _episodeIndexer1; private Mock<IndexerBase> _episodeIndexer1;
@ -170,26 +171,13 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
public void PerformSearch_should_search_all_enabled_providers() public void PerformSearch_should_search_all_enabled_providers()
{ {
//Act //Act
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes); var result = Mocker.Resolve<SearchProvider>().PerformEpisodeSearch(_series, SEASON_NUMBER, _episodes.First().EpisodeNumber);
//Assert //Assert
VerifyFetchEpisode(Times.Once()); VerifyFetchEpisode(Times.Once());
result.Should().HaveCount(PARSE_RESULT_COUNT * 2); result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
} }
[Test]
public void PerformSearch_should_look_for_scene_name_to_search()
{
WithSceneName();
//Act
Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, 1, _episodes);
//Assert
Mocker.GetMock<SceneMappingProvider>().Verify(c => c.GetSceneName(_series.SeriesId),
Times.Once());
}
[Test] [Test]
public void broken_indexer_should_not_block_other_indexers() public void broken_indexer_should_not_block_other_indexers()
{ {
@ -197,7 +185,7 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
WithTwoGoodOneBrokenIndexer(); WithTwoGoodOneBrokenIndexer();
//Act //Act
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes); var result = Mocker.Resolve<SearchProvider>().PerformEpisodeSearch(_series, SEASON_NUMBER, EPISODE_NUMBER);
//Assert //Assert
result.Should().HaveCount(PARSE_RESULT_COUNT * 2); result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
@ -215,7 +203,7 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
public void PerformSearch_for_episode_should_call_FetchEpisode() public void PerformSearch_for_episode_should_call_FetchEpisode()
{ {
//Act //Act
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes); var result = Mocker.Resolve<SearchProvider>().PerformEpisodeSearch(_series, SEASON_NUMBER, EPISODE_NUMBER);
//Assert //Assert
result.Should().HaveCount(PARSE_RESULT_COUNT * 2); result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
@ -230,7 +218,7 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
_series.IsDaily = true; _series.IsDaily = true;
//Act //Act
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes); var result = Mocker.Resolve<SearchProvider>().PerformDailyEpisodeSearch(_series, _episodes.First());
//Assert //Assert
result.Should().HaveCount(PARSE_RESULT_COUNT * 2); result.Should().HaveCount(PARSE_RESULT_COUNT * 2);
@ -244,7 +232,7 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
With30Episodes(); With30Episodes();
//Act //Act
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes); var result = Mocker.Resolve<SearchProvider>().PerformPartialSeasonSearch(_series, SEASON_NUMBER, new List<int>{0, 1, 2, 3});
//Assert //Assert
result.Should().HaveCount(80); result.Should().HaveCount(80);
@ -257,7 +245,7 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
WithNullEpisodes(); WithNullEpisodes();
//Act //Act
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes); var result = Mocker.Resolve<SearchProvider>().PerformSeasonSearch(_series, SEASON_NUMBER);
//Assert //Assert
result.Should().HaveCount(20); result.Should().HaveCount(20);
@ -271,7 +259,7 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
WithNullIndexers(); WithNullIndexers();
//Act //Act
var result = Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes); var result = Mocker.Resolve<SearchProvider>().PerformEpisodeSearch(_series, SEASON_NUMBER, EPISODE_NUMBER);
//Assert //Assert
result.Should().HaveCount(0); result.Should().HaveCount(0);
@ -283,10 +271,9 @@ namespace NzbDrone.Core.Test.ProviderTests.SearchProviderTests
{ {
WithSceneName(); WithSceneName();
Mocker.Resolve<SearchProvider>().PerformSearch(MockNotification, _series, SEASON_NUMBER, _episodes); Mocker.Resolve<SearchProvider>().PerformEpisodeSearch(_series, SEASON_NUMBER, EPISODE_NUMBER);
VerifyFetchEpisodeWithSceneName(Times.Once()); VerifyFetchEpisodeWithSceneName(Times.Once());
} }
} }
} }

@ -64,33 +64,58 @@ namespace NzbDrone.Core.Providers
return new List<int>(); return new List<int>();
} }
//Return false if the series is a daily series (we only support individual episode searching
if (series.IsDaily) if (series.IsDaily)
{
_logger.Trace("Daily series detected, skipping season search: {0}", series.Title);
return new List<int>(); return new List<int>();
}
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber); _logger.Debug("Getting episodes from database for series: {0} and season: {1}", seriesId, seasonNumber);
var episodes = _episodeProvider.GetEpisodesBySeason(seriesId, seasonNumber);
var reports = PerformSearch(notification, series, seasonNumber); if (episodes == null || episodes.Count == 0)
{
_logger.Warn("No episodes in database found for series: {0} and season: {1}.", seriesId, seasonNumber);
return new List<int>();
}
_logger.Debug("Finished searching all indexers. Total {0}", reports.Count); notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
if (reports.Count == 0) List<EpisodeParseResult> reports;
return new List<int>();
_logger.Debug("Getting episodes from database for series: {0} and season: {1}", seriesId, seasonNumber); if (series.UseSceneNumbering)
var episodeNumbers = _episodeProvider.GetEpisodeNumbersBySeason(seriesId, seasonNumber); {
var sceneSeasonNumbers = episodes.Select(e => e.SceneSeasonNumber).ToList();
var sceneEpisodeNumbers = episodes.Select(e => e.SceneEpisodeNumber).ToList();
if (episodeNumbers == null || episodeNumbers.Count == 0) if (sceneSeasonNumbers.Distinct().Count() > 1)
{ {
_logger.Warn("No episodes in database found for series: {0} and season: {1}.", seriesId, seasonNumber); _logger.Trace("Uses scene numbering, but multiple seasons found, skipping.");
return new List<int>(); return new List<int>();
} }
notification.CurrentMessage = "Processing search results"; reports = PerformSeasonSearch(series, sceneSeasonNumbers.First());
reports.Where(p => p.FullSeason && p.SeasonNumber == sceneSeasonNumbers.First()).ToList().ForEach(
e => e.EpisodeNumbers = sceneEpisodeNumbers.ToList()
);
}
else
{
reports = PerformSeasonSearch(series, seasonNumber);
reports.Where(p => p.FullSeason && p.SeasonNumber == seasonNumber).ToList().ForEach( reports.Where(p => p.FullSeason && p.SeasonNumber == seasonNumber).ToList().ForEach(
e => e.EpisodeNumbers = episodeNumbers.ToList() e => e.EpisodeNumbers = episodes.Select(ep => ep.EpisodeNumber).ToList()
); );
}
_logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
if (reports.Count == 0)
return new List<int>();
notification.CurrentMessage = "Processing search results";
searchResult.SearchHistoryItems = ProcessSearchResults(notification, reports, searchResult, series, seasonNumber); searchResult.SearchHistoryItems = ProcessSearchResults(notification, reports, searchResult, series, seasonNumber);
_searchHistoryProvider.Add(searchResult); _searchHistoryProvider.Add(searchResult);
@ -115,13 +140,36 @@ namespace NzbDrone.Core.Providers
return new List<int>(); return new List<int>();
} }
//Return empty list if the series is a daily series (we only support individual episode searching
if (series.IsDaily) if (series.IsDaily)
{
_logger.Trace("Daily series detected, skipping season search: {0}", series.Title);
return new List<int>(); return new List<int>();
}
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber); notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
var episodes = _episodeProvider.GetEpisodesBySeason(seriesId, seasonNumber); var episodes = _episodeProvider.GetEpisodesBySeason(seriesId, seasonNumber);
var reports = PerformSearch(notification, series, seasonNumber, episodes);
List<EpisodeParseResult> reports;
if (series.UseSceneNumbering)
{
var sceneSeasonNumbers = episodes.Select(e => e.SceneSeasonNumber).ToList();
var sceneEpisodeNumbers = episodes.Select(e => e.SceneEpisodeNumber).ToList();
if (sceneSeasonNumbers.Distinct().Count() > 1)
{
_logger.Trace("Uses scene numbering, but multiple seasons found, skipping.");
return new List<int>();
}
reports = PerformPartialSeasonSearch(series, sceneSeasonNumbers.First(), GetEpisodeNumberPrefixes(sceneEpisodeNumbers));
}
else
{
reports = PerformPartialSeasonSearch(series, seasonNumber, GetEpisodeNumberPrefixes(episodes.Select(e => e.EpisodeNumber)));
}
_logger.Debug("Finished searching all indexers. Total {0}", reports.Count); _logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
if (reports.Count == 0) if (reports.Count == 0)
@ -153,27 +201,29 @@ namespace NzbDrone.Core.Providers
} }
notification.CurrentMessage = "Looking for " + episode; notification.CurrentMessage = "Looking for " + episode;
List<EpisodeParseResult> reports;
if (episode.Series.IsDaily && !episode.AirDate.HasValue) var searchResult = new SearchHistory
{
SearchTime = DateTime.Now,
SeriesId = episode.Series.SeriesId,
EpisodeId = episodeId
};
if (episode.Series.IsDaily)
{
if (!episode.AirDate.HasValue)
{ {
_logger.Warn("AirDate is not Valid for: {0}", episode); _logger.Warn("AirDate is not Valid for: {0}", episode);
notification.CurrentMessage = String.Format("Search for {0} Failed, AirDate is invalid", episode); notification.CurrentMessage = String.Format("Search for {0} Failed, AirDate is invalid", episode);
return false; return false;
} }
var searchResult = new SearchHistory reports = PerformDailyEpisodeSearch(episode.Series, episode);
{
SearchTime = DateTime.Now,
SeriesId = episode.Series.SeriesId
};
var reports = PerformSearch(notification, episode.Series, episode.SeasonNumber, new List<Episode> { episode });
_logger.Debug("Finished searching all indexers. Total {0}", reports.Count); _logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
notification.CurrentMessage = "Processing search results"; notification.CurrentMessage = "Processing search results";
if (episode.Series.IsDaily)
{
searchResult.SearchHistoryItems = ProcessSearchResults(notification, reports, episode.Series, episode.AirDate.Value); searchResult.SearchHistoryItems = ProcessSearchResults(notification, reports, episode.Series, episode.AirDate.Value);
_searchHistoryProvider.Add(searchResult); _searchHistoryProvider.Add(searchResult);
@ -183,17 +233,17 @@ namespace NzbDrone.Core.Providers
else if (episode.Series.UseSceneNumbering) else if (episode.Series.UseSceneNumbering)
{ {
searchResult.EpisodeId = episodeId;
var seasonNumber = episode.SceneSeasonNumber; var seasonNumber = episode.SceneSeasonNumber;
var episodeNumber = episode.SceneEpisodeNumber; var episodeNumber = episode.SceneEpisodeNumber;
if (seasonNumber == 0 || episodeNumber == 0) if (seasonNumber == 0 && episodeNumber == 0)
{ {
seasonNumber = episode.SeasonNumber; seasonNumber = episode.SeasonNumber;
episodeNumber = episode.EpisodeNumber; episodeNumber = episode.EpisodeNumber;
} }
reports = PerformEpisodeSearch(episode.Series, seasonNumber, episodeNumber);
searchResult.SearchHistoryItems = ProcessSearchResults( searchResult.SearchHistoryItems = ProcessSearchResults(
notification, notification,
reports, reports,
@ -211,7 +261,8 @@ namespace NzbDrone.Core.Providers
else else
{ {
searchResult.EpisodeId = episodeId; reports = PerformEpisodeSearch(episode.Series, episode.SeasonNumber, episode.EpisodeNumber);
searchResult.SearchHistoryItems = ProcessSearchResults(notification, reports, searchResult, episode.Series, episode.SeasonNumber, episode.EpisodeNumber); searchResult.SearchHistoryItems = ProcessSearchResults(notification, reports, searchResult, episode.Series, episode.SeasonNumber, episode.EpisodeNumber);
_searchHistoryProvider.Add(searchResult); _searchHistoryProvider.Add(searchResult);
@ -221,75 +272,12 @@ namespace NzbDrone.Core.Providers
_logger.Warn("Unable to find {0} in any of indexers.", episode); _logger.Warn("Unable to find {0} in any of indexers.", episode);
if (reports.Any()) notification.CurrentMessage = reports.Any() ? String.Format("Sorry, couldn't find {0}, that matches your preferences.", episode)
{ : String.Format("Sorry, couldn't find {0} in any of indexers.", episode);
notification.CurrentMessage = String.Format("Sorry, couldn't find {0}, that matches your preferences.", episode);
}
else
{
notification.CurrentMessage = String.Format("Sorry, couldn't find {0} in any of indexers.", episode);
}
return false; return false;
} }
public List<EpisodeParseResult> PerformSearch(ProgressNotification notification, Series series, int seasonNumber, IList<Episode> episodes = null)
{
//If single episode, do a single episode search, if full season then do a full season search, otherwise, do a partial search
var reports = new List<EpisodeParseResult>();
var title = _sceneMappingProvider.GetSceneName(series.SeriesId);
if (string.IsNullOrWhiteSpace(title))
{
title = series.Title;
}
Parallel.ForEach(_indexerProvider.GetEnabledIndexers(), indexer =>
{
try
{
if (episodes == null)
reports.AddRange(indexer.FetchSeason(title, seasonNumber));
//Treat as single episode
else if (episodes.Count == 1)
{
//Use SceneNumbering - Only if SceneSN and SceneEN are greater than zero
if (series.UseSceneNumbering && episodes.First().SceneSeasonNumber > 0 && episodes.First().SceneEpisodeNumber > 0)
reports.AddRange(indexer.FetchEpisode(title, episodes.First().SceneSeasonNumber, episodes.First().SceneEpisodeNumber));
//Standard
else 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)
{
reports.AddRange(indexer.FetchPartialSeason(title, seasonNumber, episodePrefix));
}
}
}
catch (Exception e)
{
_logger.ErrorException("An error has occurred while fetching items from " + indexer.Name, e);
}
});
return reports;
}
public List<SearchHistoryItem> ProcessSearchResults(ProgressNotification notification, IEnumerable<EpisodeParseResult> reports, SearchHistory searchResult, Series series, int seasonNumber, int? episodeNumber = null) public List<SearchHistoryItem> ProcessSearchResults(ProgressNotification notification, IEnumerable<EpisodeParseResult> reports, SearchHistory searchResult, Series series, int seasonNumber, int? episodeNumber = null)
{ {
var items = new List<SearchHistoryItem>(); var items = new List<SearchHistoryItem>();
@ -488,5 +476,106 @@ namespace NzbDrone.Core.Providers
return results.Distinct().ToList(); return results.Distinct().ToList();
} }
public List<EpisodeParseResult> PerformEpisodeSearch(Series series, int seasonNumber, int episodeNumber)
{
var reports = new List<EpisodeParseResult>();
var title = GetSeriesTitle(series);
Parallel.ForEach(_indexerProvider.GetEnabledIndexers(), indexer =>
{
try
{
reports.AddRange(indexer.FetchEpisode(title, seasonNumber, episodeNumber));
}
catch (Exception e)
{
_logger.ErrorException(String.Format("An error has occurred while searching for {0}-S{1:00}E{2:00} from: {3}",
series.Title, seasonNumber, episodeNumber, indexer.Name), e);
}
});
return reports;
}
public List<EpisodeParseResult> PerformDailyEpisodeSearch(Series series, Episode episode)
{
var reports = new List<EpisodeParseResult>();
var title = GetSeriesTitle(series);
Parallel.ForEach(_indexerProvider.GetEnabledIndexers(), indexer =>
{
try
{
_logger.Trace("Episode {0} is a daily episode, searching as daily", episode);
reports.AddRange(indexer.FetchDailyEpisode(title, episode.AirDate.Value));
}
catch (Exception e)
{
_logger.ErrorException(String.Format("An error has occurred while searching for {0}-{1} from: {2}",
series.Title, episode.AirDate, indexer.Name), e);
}
});
return reports;
}
public List<EpisodeParseResult> PerformPartialSeasonSearch(Series series, int seasonNumber, List<int> prefixes)
{
var reports = new List<EpisodeParseResult>();
var title = GetSeriesTitle(series);
Parallel.ForEach(_indexerProvider.GetEnabledIndexers(), indexer =>
{
try
{
foreach (var episodePrefix in prefixes)
{
reports.AddRange(indexer.FetchPartialSeason(title, seasonNumber, episodePrefix));
}
}
catch (Exception e)
{
_logger.ErrorException(String.Format("An error has occurred while searching for {0}-S{1:00} from: {2}",
series.Title, seasonNumber, indexer.Name), e);
}
});
return reports;
}
public List<EpisodeParseResult> PerformSeasonSearch(Series series, int seasonNumber)
{
var reports = new List<EpisodeParseResult>();
var title = GetSeriesTitle(series);
Parallel.ForEach(_indexerProvider.GetEnabledIndexers(), indexer =>
{
try
{
reports.AddRange(indexer.FetchSeason(title, seasonNumber));
}
catch (Exception e)
{
_logger.ErrorException("An error has occurred while searching for items from: " + indexer.Name, e);
}
});
return reports;
}
public string GetSeriesTitle(Series series)
{
var title = _sceneMappingProvider.GetSceneName(series.SeriesId);
if (String.IsNullOrWhiteSpace(title))
title = series.Title;
return title;
}
} }
} }

Loading…
Cancel
Save