1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Core/IndexerSearch/IndexerSearchBase.cs

152 lines
5.7 KiB
C#
Raw Normal View History

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