1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Core/Providers/Search/EpisodeSearch.cs

128 lines
5.1 KiB
C#
Raw Normal View History

2013-01-11 01:35:33 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;
2013-02-21 08:07:34 +01:00
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Tv;
2013-01-11 01:35:33 +01:00
using NzbDrone.Core.Model;
2013-01-13 09:24:48 +01:00
using NzbDrone.Core.Model.Notification;
2013-02-19 03:19:38 +01:00
using NzbDrone.Core.DecisionEngine;
2013-01-11 01:35:33 +01:00
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Search;
namespace NzbDrone.Core.Providers.Search
{
public class EpisodeSearch : SearchBase
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2013-02-21 08:26:32 +01:00
public EpisodeSearch(ISeriesService seriesService, EpisodeService episodeService, DownloadProvider downloadProvider, IIndexerService indexerService,
2013-01-13 09:24:48 +01:00
SceneMappingProvider sceneMappingProvider, AllowedDownloadSpecification allowedDownloadSpecification,
2013-02-19 07:56:02 +01:00
SearchHistoryProvider searchHistoryProvider, ISeriesRepository seriesRepository)
2013-02-21 08:07:34 +01:00
: base(seriesService,seriesRepository, episodeService, downloadProvider, indexerService, sceneMappingProvider,
2013-01-13 09:24:48 +01:00
allowedDownloadSpecification, searchHistoryProvider)
2013-01-11 01:35:33 +01:00
{
}
2013-01-13 19:29:53 +01:00
public EpisodeSearch()
{
}
2013-01-13 09:24:48 +01:00
public override List<EpisodeParseResult> PerformSearch(Series series, dynamic options, ProgressNotification notification)
2013-01-11 01:35:33 +01:00
{
2013-01-13 09:24:48 +01:00
//Todo: Daily and Anime or separate them out?
//Todo: Epsiodes that use scene numbering
2013-01-11 01:35:33 +01:00
2013-01-13 09:24:48 +01:00
if (options.Episode == null)
throw new ArgumentException("Episode is invalid");
2013-01-11 01:35:33 +01:00
2013-01-13 09:24:48 +01:00
notification.CurrentMessage = "Looking for " + options.Episode;
2013-01-11 01:35:33 +01:00
var reports = new List<EpisodeParseResult>();
2013-01-13 09:24:48 +01:00
var title = GetSearchTitle(series);
var seasonNumber = options.Episode.SeasonNumber;
var episodeNumber = options.Episode.EpisodeNumber;
if (series.UseSceneNumbering)
{
if(options.Episode.SceneSeasonNumber > 0 && options.Episode.SceneEpisodeNumber > 0)
{
logger.Trace("Using Scene Numbering for: {0}", options.Episode);
seasonNumber = options.Episode.SceneSeasonNumber;
episodeNumber = options.Episode.SceneEpisodeNumber;
}
}
2013-01-11 01:35:33 +01:00
2013-02-21 08:07:34 +01:00
Parallel.ForEach(_indexerService.GetEnabledIndexers(), indexer =>
2013-01-11 01:35:33 +01:00
{
try
{
2013-01-13 09:24:48 +01:00
reports.AddRange(indexer.FetchEpisode(title, seasonNumber, episodeNumber));
2013-01-11 01:35:33 +01:00
}
catch (Exception e)
{
logger.ErrorException(String.Format("An error has occurred while searching for {0}-S{1:00}E{2:00} from: {3}",
2013-01-13 09:24:48 +01:00
series.Title, options.Episode.SeasonNumber, options.Episode.EpisodeNumber, indexer.Name), e);
2013-01-11 01:35:33 +01:00
}
});
return reports;
}
2013-01-13 09:24:48 +01:00
public override SearchHistoryItem CheckReport(Series series, dynamic options, EpisodeParseResult episodeParseResult,
SearchHistoryItem item)
2013-01-11 01:35:33 +01:00
{
2013-01-13 09:24:48 +01:00
if(series.UseSceneNumbering && options.Episode.SeasonNumber > 0 && options.Episode.EpisodeNumber > 0)
{
if (options.Episode.SceneSeasonNumber != episodeParseResult.SeasonNumber)
{
logger.Trace("Season number does not match searched season number, skipping.");
item.SearchError = ReportRejectionType.WrongSeason;
return item;
}
if (!episodeParseResult.EpisodeNumbers.Contains(options.Episode.SceneEpisodeNumber))
{
logger.Trace("Episode number does not match searched episode number, skipping.");
item.SearchError = ReportRejectionType.WrongEpisode;
return item;
}
return item;
}
if(options.Episode.SeasonNumber != episodeParseResult.SeasonNumber)
{
logger.Trace("Season number does not match searched season number, skipping.");
item.SearchError = ReportRejectionType.WrongSeason;
return item;
}
if (!episodeParseResult.EpisodeNumbers.Contains(options.Episode.EpisodeNumber))
{
logger.Trace("Episode number does not match searched episode number, skipping.");
item.SearchError = ReportRejectionType.WrongEpisode;
return item;
}
return item;
}
protected override void FinalizeSearch(Series series, dynamic options, Boolean reportsFound, ProgressNotification notification)
{
logger.Warn("Unable to find {0} in any of indexers.", options.Episode);
notification.CurrentMessage = reportsFound ? String.Format("Sorry, couldn't find {0}, that matches your preferences.", options.Episode)
: String.Format("Sorry, couldn't find {0} in any of indexers.", options.Episode);
2013-01-11 01:35:33 +01:00
}
}
}