You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
5.7 KiB
152 lines
5.7 KiB
12 years ago
|
using System;
|
||
12 years ago
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
12 years ago
|
using System.Text.RegularExpressions;
|
||
12 years ago
|
using NLog;
|
||
12 years ago
|
using NzbDrone.Core.DecisionEngine;
|
||
12 years ago
|
using NzbDrone.Core.Download;
|
||
12 years ago
|
using NzbDrone.Core.Indexers;
|
||
12 years ago
|
using NzbDrone.Core.Model;
|
||
|
using NzbDrone.Core.Model.Notification;
|
||
12 years ago
|
using NzbDrone.Core.ReferenceData;
|
||
|
using NzbDrone.Core.Tv;
|
||
12 years ago
|
|
||
12 years ago
|
namespace NzbDrone.Core.IndexerSearch
|
||
12 years ago
|
{
|
||
12 years ago
|
public abstract class IndexerSearchBase
|
||
12 years ago
|
{
|
||
12 years ago
|
private readonly ISeriesRepository _seriesRepository;
|
||
12 years ago
|
private readonly IEpisodeService _episodeService;
|
||
|
private readonly DownloadProvider _downloadProvider;
|
||
|
private readonly ISceneMappingService _sceneMappingService;
|
||
|
private readonly IDownloadDirector DownloadDirector;
|
||
|
|
||
12 years ago
|
protected readonly IIndexerService _indexerService;
|
||
12 years ago
|
|
||
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
||
|
|
||
12 years ago
|
protected IndexerSearchBase(ISeriesRepository seriesRepository, IEpisodeService episodeService, DownloadProvider downloadProvider,
|
||
12 years ago
|
IIndexerService indexerService, ISceneMappingService sceneMappingService,
|
||
|
IDownloadDirector downloadDirector)
|
||
12 years ago
|
{
|
||
12 years ago
|
_seriesRepository = seriesRepository;
|
||
12 years ago
|
_episodeService = episodeService;
|
||
12 years ago
|
_downloadProvider = downloadProvider;
|
||
12 years ago
|
_indexerService = indexerService;
|
||
12 years ago
|
_sceneMappingService = sceneMappingService;
|
||
12 years ago
|
DownloadDirector = downloadDirector;
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
protected IndexerSearchBase()
|
||
12 years ago
|
{
|
||
|
}
|
||
|
|
||
12 years ago
|
public abstract List<EpisodeParseResult> PerformSearch(Series series, List<Episode> episodes, ProgressNotification notification);
|
||
12 years ago
|
public abstract bool IsEpisodeMatch(Series series, dynamic options, EpisodeParseResult episodeParseResult);
|
||
12 years ago
|
|
||
12 years ago
|
public virtual List<int> Search(Series series, dynamic options, ProgressNotification notification)
|
||
12 years ago
|
{
|
||
12 years ago
|
if (options == null)
|
||
|
throw new ArgumentNullException(options);
|
||
12 years ago
|
|
||
12 years ago
|
|
||
|
List<EpisodeParseResult> reports = PerformSearch(series, options, notification);
|
||
12 years ago
|
|
||
12 years ago
|
logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
||
|
notification.CurrentMessage = "Processing search results";
|
||
|
|
||
12 years ago
|
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);
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
return result.Grabbed;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
public void ProcessReports(Series series, dynamic options, List<EpisodeParseResult> episodeParseResults)
|
||
12 years ago
|
{
|
||
12 years ago
|
|
||
12 years ago
|
var sortedResults = episodeParseResults.OrderByDescending(c => c.Quality)
|
||
|
.ThenBy(c => c.EpisodeNumbers.MinOrDefault())
|
||
|
.ThenBy(c => c.Age);
|
||
|
|
||
|
foreach (var episodeParseResult in sortedResults)
|
||
12 years ago
|
{
|
||
12 years ago
|
try
|
||
12 years ago
|
{
|
||
12 years ago
|
|
||
|
logger.Trace("Analyzing report " + episodeParseResult);
|
||
12 years ago
|
episodeParseResult.Series = _seriesRepository.GetByTitle(episodeParseResult.CleanTitle);
|
||
12 years ago
|
|
||
12 years ago
|
if (episodeParseResult.Series == null || episodeParseResult.Series.Id != series.Id)
|
||
12 years ago
|
{
|
||
12 years ago
|
episodeParseResult.Decision = new DownloadDecision("Invalid Series");
|
||
12 years ago
|
continue;
|
||
|
}
|
||
|
|
||
12 years ago
|
episodeParseResult.Episodes = _episodeService.GetEpisodesByParseResult(episodeParseResult);
|
||
12 years ago
|
|
||
12 years ago
|
|
||
|
if (!IsEpisodeMatch(series, options, episodeParseResult))
|
||
12 years ago
|
{
|
||
12 years ago
|
episodeParseResult.Decision = new DownloadDecision("Incorrect Episode/Season");
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
var downloadDecision = DownloadDirector.GetDownloadDecision(episodeParseResult);
|
||
12 years ago
|
|
||
12 years ago
|
if (downloadDecision.Approved)
|
||
12 years ago
|
{
|
||
12 years ago
|
DownloadReport(episodeParseResult);
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
catch (Exception e)
|
||
12 years ago
|
{
|
||
12 years ago
|
logger.ErrorException("An error has occurred while processing parse result items from " + episodeParseResult, e);
|
||
12 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
public virtual Boolean DownloadReport(EpisodeParseResult episodeParseResult)
|
||
12 years ago
|
{
|
||
|
logger.Debug("Found '{0}'. Adding to download queue.", episodeParseResult);
|
||
|
try
|
||
|
{
|
||
|
if (_downloadProvider.DownloadReport(episodeParseResult))
|
||
|
{
|
||
12 years ago
|
return true;
|
||
12 years ago
|
}
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
logger.ErrorException("Unable to add report to download queue." + episodeParseResult, e);
|
||
|
}
|
||
|
|
||
12 years ago
|
return false;
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
public virtual string GetSearchTitle(Series series, int seasonNumber = -1)
|
||
12 years ago
|
{
|
||
12 years ago
|
var seasonTitle = _sceneMappingService.GetSceneName(series.Id, seasonNumber);
|
||
12 years ago
|
|
||
12 years ago
|
if (!String.IsNullOrWhiteSpace(seasonTitle))
|
||
12 years ago
|
return seasonTitle;
|
||
|
|
||
12 years ago
|
var title = _sceneMappingService.GetSceneName(series.Id);
|
||
12 years ago
|
|
||
|
if (String.IsNullOrWhiteSpace(title))
|
||
|
{
|
||
|
title = series.Title;
|
||
|
title = title.Replace("&", "and");
|
||
12 years ago
|
title = Regex.Replace(title, @"[^\w\d\s\-]", "");
|
||
12 years ago
|
}
|
||
|
|
||
|
return title;
|
||
|
}
|
||
|
}
|
||
|
}
|