sonarr-repo-only/NzbDrone.Core/IndexerSearch/IndexerSearchBase.cs

152 lines
5.7 KiB
C#
Raw Normal View History

2013-03-07 03:45:36 +00:00
using System;
2013-01-11 00:35:33 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
2013-01-11 00:35:33 +00:00
using NLog;
2013-03-07 03:45:36 +00:00
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download;
2013-02-21 07:07:34 +00:00
using NzbDrone.Core.Indexers;
2013-01-11 00:35:33 +00:00
using NzbDrone.Core.Model;
using NzbDrone.Core.Model.Notification;
2013-03-07 03:45:36 +00:00
using NzbDrone.Core.ReferenceData;
using NzbDrone.Core.Tv;
2013-01-11 00:35:33 +00:00
2013-03-07 03:45:36 +00:00
namespace NzbDrone.Core.IndexerSearch
2013-01-11 00:35:33 +00:00
{
public abstract class IndexerSearchBase
2013-01-11 00:35:33 +00:00
{
2013-02-19 06:56:02 +00:00
private readonly ISeriesRepository _seriesRepository;
private readonly IEpisodeService _episodeService;
private readonly DownloadProvider _downloadProvider;
private readonly ISceneMappingService _sceneMappingService;
private readonly IDownloadDirector DownloadDirector;
2013-02-21 07:26:32 +00:00
protected readonly IIndexerService _indexerService;
2013-01-11 00:35:33 +00:00
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
protected IndexerSearchBase(ISeriesRepository seriesRepository, IEpisodeService episodeService, DownloadProvider downloadProvider,
2013-03-07 03:45:36 +00:00
IIndexerService indexerService, ISceneMappingService sceneMappingService,
IDownloadDirector downloadDirector)
2013-01-11 00:35:33 +00:00
{
2013-02-19 06:56:02 +00:00
_seriesRepository = seriesRepository;
_episodeService = episodeService;
2013-01-11 00:35:33 +00:00
_downloadProvider = downloadProvider;
2013-02-21 07:07:34 +00:00
_indexerService = indexerService;
2013-03-02 18:25:39 +00:00
_sceneMappingService = sceneMappingService;
2013-03-07 01:51:47 +00:00
DownloadDirector = downloadDirector;
2013-01-11 00:35:33 +00:00
}
protected IndexerSearchBase()
2013-01-13 18:29:53 +00:00
{
}
2013-03-07 03:45:36 +00:00
public abstract List<EpisodeParseResult> PerformSearch(Series series, List<Episode> episodes, ProgressNotification notification);
2013-03-07 01:51:47 +00:00
public abstract bool IsEpisodeMatch(Series series, dynamic options, EpisodeParseResult episodeParseResult);
2013-01-13 08:24:48 +00:00
2013-03-07 03:45:36 +00:00
public virtual List<int> Search(Series series, dynamic options, ProgressNotification notification)
2013-01-11 00:35:33 +00:00
{
2013-01-13 08:24:48 +00:00
if (options == null)
throw new ArgumentNullException(options);
2013-01-11 00:35:33 +00:00
2013-01-13 08:24:48 +00:00
List<EpisodeParseResult> reports = PerformSearch(series, options, notification);
2013-03-07 01:51:47 +00:00
2013-01-13 08:24:48 +00:00
logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
notification.CurrentMessage = "Processing search results";
2013-03-07 01:51:47 +00:00
var result = ProcessReports(series, options, reports);
if (!result.Grabbed.Any())
{
logger.Warn("Unable to find {0} in any of indexers.", options.Episode);
notification.CurrentMessage = reports.Any() ? 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-13 08:24:48 +00:00
2013-03-07 01:51:47 +00:00
return result.Grabbed;
2013-01-13 08:24:48 +00:00
}
2013-01-11 00:35:33 +00:00
2013-03-07 01:51:47 +00:00
public void ProcessReports(Series series, dynamic options, List<EpisodeParseResult> episodeParseResults)
2013-01-11 00:35:33 +00:00
{
2013-01-13 08:24:48 +00:00
2013-03-07 01:51:47 +00:00
var sortedResults = episodeParseResults.OrderByDescending(c => c.Quality)
.ThenBy(c => c.EpisodeNumbers.MinOrDefault())
.ThenBy(c => c.Age);
foreach (var episodeParseResult in sortedResults)
2013-01-11 00:35:33 +00:00
{
2013-01-13 08:24:48 +00:00
try
2013-01-11 00:35:33 +00:00
{
2013-03-07 01:51:47 +00:00
logger.Trace("Analyzing report " + episodeParseResult);
episodeParseResult.Series = _seriesRepository.GetByTitle(episodeParseResult.CleanTitle);
2013-01-13 08:24:48 +00:00
2013-03-07 01:51:47 +00:00
if (episodeParseResult.Series == null || episodeParseResult.Series.Id != series.Id)
2013-01-13 08:24:48 +00:00
{
2013-03-07 01:51:47 +00:00
episodeParseResult.Decision = new DownloadDecision("Invalid Series");
2013-01-13 08:24:48 +00:00
continue;
}
episodeParseResult.Episodes = _episodeService.GetEpisodesByParseResult(episodeParseResult);
2013-01-13 08:24:48 +00:00
2013-03-07 01:51:47 +00:00
if (!IsEpisodeMatch(series, options, episodeParseResult))
2013-01-13 08:24:48 +00:00
{
2013-03-07 01:51:47 +00:00
episodeParseResult.Decision = new DownloadDecision("Incorrect Episode/Season");
2013-01-13 08:24:48 +00:00
}
2013-03-07 01:51:47 +00:00
var downloadDecision = DownloadDirector.GetDownloadDecision(episodeParseResult);
2013-01-13 08:24:48 +00:00
2013-03-07 01:51:47 +00:00
if (downloadDecision.Approved)
2013-01-13 08:24:48 +00:00
{
2013-03-07 01:51:47 +00:00
DownloadReport(episodeParseResult);
2013-01-13 08:24:48 +00:00
}
2013-01-11 00:35:33 +00:00
}
2013-03-07 01:51:47 +00:00
catch (Exception e)
2013-01-11 00:35:33 +00:00
{
2013-01-13 08:24:48 +00:00
logger.ErrorException("An error has occurred while processing parse result items from " + episodeParseResult, e);
2013-01-11 00:35:33 +00:00
}
}
}
2013-03-07 01:51:47 +00:00
public virtual Boolean DownloadReport(EpisodeParseResult episodeParseResult)
2013-01-11 00:35:33 +00:00
{
logger.Debug("Found '{0}'. Adding to download queue.", episodeParseResult);
try
{
if (_downloadProvider.DownloadReport(episodeParseResult))
{
2013-01-13 08:24:48 +00:00
return true;
2013-01-11 00:35:33 +00:00
}
}
catch (Exception e)
{
logger.ErrorException("Unable to add report to download queue." + episodeParseResult, e);
}
2013-01-13 08:24:48 +00:00
return false;
2013-01-11 00:35:33 +00:00
}
2013-01-13 08:24:48 +00:00
public virtual string GetSearchTitle(Series series, int seasonNumber = -1)
{
2013-03-02 18:25:39 +00:00
var seasonTitle = _sceneMappingService.GetSceneName(series.Id, seasonNumber);
2013-03-07 01:51:47 +00:00
if (!String.IsNullOrWhiteSpace(seasonTitle))
return seasonTitle;
2013-03-02 18:25:39 +00:00
var title = _sceneMappingService.GetSceneName(series.Id);
2013-01-11 00:35:33 +00:00
if (String.IsNullOrWhiteSpace(title))
{
title = series.Title;
title = title.Replace("&", "and");
title = Regex.Replace(title, @"[^\w\d\s\-]", "");
2013-01-11 00:35:33 +00:00
}
return title;
}
}
}