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;
|
2013-01-23 03:57:51 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2013-01-11 00:35:33 +00:00
|
|
|
using NLog;
|
2013-04-01 02:43:58 +00:00
|
|
|
using NzbDrone.Core.DataAugmentation;
|
|
|
|
using NzbDrone.Core.DataAugmentation.Scene;
|
2013-03-07 03:45:36 +00:00
|
|
|
using NzbDrone.Core.DecisionEngine;
|
2013-02-24 19:18:48 +00:00
|
|
|
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.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
|
|
|
{
|
2013-03-07 04:34:56 +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;
|
2013-03-07 04:34:56 +00:00
|
|
|
private readonly IEpisodeService _episodeService;
|
2013-04-01 06:22:16 +00:00
|
|
|
private readonly IDownloadService _downloadService;
|
2013-03-07 04:34:56 +00:00
|
|
|
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();
|
|
|
|
|
2013-04-01 06:22:16 +00:00
|
|
|
protected IndexerSearchBase(ISeriesRepository seriesRepository, IEpisodeService episodeService, IDownloadService downloadService,
|
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;
|
2013-02-20 02:05:15 +00:00
|
|
|
_episodeService = episodeService;
|
2013-04-01 06:22:16 +00:00
|
|
|
_downloadService = downloadService;
|
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
|
|
|
}
|
|
|
|
|
2013-03-07 04:34:56 +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);
|
2013-02-20 02:05:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
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
|
|
|
|
{
|
2013-04-01 06:22:16 +00:00
|
|
|
if (_downloadService.DownloadReport(episodeParseResult))
|
2013-01-11 00:35:33 +00:00
|
|
|
{
|
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-01-24 06:36:37 +00:00
|
|
|
{
|
2013-03-02 18:25:39 +00:00
|
|
|
var seasonTitle = _sceneMappingService.GetSceneName(series.Id, seasonNumber);
|
2013-01-24 06:36:37 +00:00
|
|
|
|
2013-03-07 01:51:47 +00:00
|
|
|
if (!String.IsNullOrWhiteSpace(seasonTitle))
|
2013-01-24 06:36:37 +00:00
|
|
|
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");
|
2013-01-23 03:57:51 +00:00
|
|
|
title = Regex.Replace(title, @"[^\w\d\s\-]", "");
|
2013-01-11 00:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|