2013-01-11 03:35:33 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2013-01-23 06:57:51 +03:00
|
|
|
using System.Text.RegularExpressions;
|
2013-01-11 03:35:33 +03:00
|
|
|
using NLog;
|
2013-02-24 22:57:33 +03:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-03-07 03:19:49 +03:00
|
|
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
2013-02-24 22:18:48 +03:00
|
|
|
using NzbDrone.Core.Download;
|
2013-02-21 10:07:34 +03:00
|
|
|
using NzbDrone.Core.Indexers;
|
2013-03-02 21:25:39 +03:00
|
|
|
using NzbDrone.Core.ReferenceData;
|
2013-02-19 09:01:03 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
2013-01-11 03:35:33 +03:00
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2013-02-19 05:19:38 +03:00
|
|
|
using NzbDrone.Core.DecisionEngine;
|
2013-01-11 03:35:33 +03:00
|
|
|
using NzbDrone.Core.Repository.Search;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers.Search
|
|
|
|
{
|
|
|
|
public abstract class SearchBase
|
|
|
|
{
|
2013-02-19 09:56:02 +03:00
|
|
|
private readonly ISeriesRepository _seriesRepository;
|
2013-02-22 03:47:09 +03:00
|
|
|
protected readonly IEpisodeService _episodeService;
|
2013-01-11 03:35:33 +03:00
|
|
|
protected readonly DownloadProvider _downloadProvider;
|
2013-02-21 10:26:32 +03:00
|
|
|
protected readonly IIndexerService _indexerService;
|
2013-03-02 21:25:39 +03:00
|
|
|
protected readonly SceneMappingService _sceneMappingService;
|
2013-01-11 03:35:33 +03:00
|
|
|
protected readonly AllowedDownloadSpecification _allowedDownloadSpecification;
|
|
|
|
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
2013-03-05 22:58:53 +03:00
|
|
|
protected SearchBase(ISeriesRepository seriesRepository, IEpisodeService episodeService, DownloadProvider downloadProvider,
|
2013-03-02 21:25:39 +03:00
|
|
|
IIndexerService indexerService, SceneMappingService sceneMappingService,
|
2013-02-26 05:20:42 +03:00
|
|
|
AllowedDownloadSpecification allowedDownloadSpecification)
|
2013-01-11 03:35:33 +03:00
|
|
|
{
|
2013-02-19 09:56:02 +03:00
|
|
|
_seriesRepository = seriesRepository;
|
2013-02-20 05:05:15 +03:00
|
|
|
_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-01-11 03:35:33 +03:00
|
|
|
_allowedDownloadSpecification = allowedDownloadSpecification;
|
|
|
|
}
|
|
|
|
|
2013-01-13 21:29:53 +03:00
|
|
|
protected SearchBase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-13 11:24:48 +03:00
|
|
|
public abstract List<EpisodeParseResult> PerformSearch(Series series, dynamic options, ProgressNotification notification);
|
|
|
|
public abstract SearchHistoryItem CheckReport(Series series, dynamic options, EpisodeParseResult episodeParseResult,
|
|
|
|
SearchHistoryItem item);
|
|
|
|
|
|
|
|
protected abstract void FinalizeSearch(Series series, dynamic options, Boolean reportsFound, ProgressNotification notification);
|
|
|
|
|
|
|
|
public virtual List<Int32> 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
|
|
|
var searchResult = new SearchHistory
|
|
|
|
{
|
|
|
|
SearchTime = DateTime.Now,
|
2013-02-26 06:58:57 +03:00
|
|
|
SeriesId = series.Id,
|
2013-01-24 09:36:37 +03:00
|
|
|
EpisodeId = options.GetType().GetProperty("Episode") != null ? options.Episode.EpisodeId : null,
|
|
|
|
SeasonNumber = options.GetType().GetProperty("SeasonNumber") != null ? options.SeasonNumber : null
|
2013-01-13 11:24:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
List<EpisodeParseResult> reports = PerformSearch(series, options, notification);
|
|
|
|
|
|
|
|
logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
|
|
|
notification.CurrentMessage = "Processing search results";
|
|
|
|
|
|
|
|
ProcessReports(series, options, reports, searchResult, notification);
|
|
|
|
|
|
|
|
if(searchResult.Successes.Any())
|
|
|
|
return searchResult.Successes;
|
|
|
|
|
|
|
|
FinalizeSearch(series, options, reports.Any(), notification);
|
|
|
|
return new List<Int32>();
|
|
|
|
}
|
2013-01-11 03:35:33 +03:00
|
|
|
|
2013-01-13 11:24:48 +03:00
|
|
|
public virtual SearchHistory ProcessReports(Series series, dynamic options, List<EpisodeParseResult> episodeParseResults,
|
|
|
|
SearchHistory searchResult, ProgressNotification notification)
|
2013-01-11 03:35:33 +03:00
|
|
|
{
|
2013-01-13 11:24:48 +03:00
|
|
|
var items = new List<SearchHistoryItem>();
|
|
|
|
searchResult.Successes = new List<Int32>();
|
|
|
|
|
|
|
|
foreach(var episodeParseResult in episodeParseResults
|
|
|
|
.OrderByDescending(c => c.Quality)
|
|
|
|
.ThenBy(c => c.EpisodeNumbers.MinOrDefault())
|
|
|
|
.ThenBy(c => c.Age))
|
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-01-13 11:24:48 +03:00
|
|
|
var item = new SearchHistoryItem
|
|
|
|
{
|
|
|
|
ReportTitle = episodeParseResult.OriginalString,
|
|
|
|
NzbUrl = episodeParseResult.NzbUrl,
|
|
|
|
Indexer = episodeParseResult.Indexer,
|
|
|
|
Quality = episodeParseResult.Quality.Quality,
|
|
|
|
Proper = episodeParseResult.Quality.Proper,
|
|
|
|
Size = episodeParseResult.Size,
|
|
|
|
Age = episodeParseResult.Age,
|
|
|
|
Language = episodeParseResult.Language
|
|
|
|
};
|
|
|
|
|
|
|
|
items.Add(item);
|
|
|
|
|
|
|
|
logger.Trace("Analysing report " + episodeParseResult);
|
2013-02-20 05:05:15 +03:00
|
|
|
episodeParseResult.Series = _seriesRepository.GetByTitle(episodeParseResult.CleanTitle);
|
2013-01-13 11:24:48 +03:00
|
|
|
|
2013-02-26 06:58:57 +03:00
|
|
|
if(episodeParseResult.Series == null || ((ModelBase)episodeParseResult.Series).Id != series.Id)
|
2013-01-13 11:24:48 +03:00
|
|
|
{
|
2013-03-06 23:30:53 +03:00
|
|
|
item.SearchError = ReportRejectionReasons.WrongSeries;
|
2013-01-13 11:24:48 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-20 05:05:15 +03:00
|
|
|
episodeParseResult.Episodes = _episodeService.GetEpisodesByParseResult(episodeParseResult);
|
2013-01-13 11:24:48 +03:00
|
|
|
|
2013-02-26 06:58:57 +03:00
|
|
|
if (searchResult.Successes.Intersect(episodeParseResult.Episodes.Select(e => e.Id)).Any())
|
2013-01-13 11:24:48 +03:00
|
|
|
{
|
2013-03-06 23:30:53 +03:00
|
|
|
item.SearchError = ReportRejectionReasons.Skipped;
|
2013-01-13 11:24:48 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckReport(series, options, episodeParseResult, item);
|
2013-03-06 23:30:53 +03:00
|
|
|
if (item.SearchError != ReportRejectionReasons.None)
|
2013-01-13 11:24:48 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
item.SearchError = _allowedDownloadSpecification.IsSatisfiedBy(episodeParseResult);
|
|
|
|
|
2013-03-06 23:30:53 +03:00
|
|
|
if(item.SearchError == ReportRejectionReasons.None)
|
2013-01-13 11:24:48 +03:00
|
|
|
{
|
|
|
|
if(DownloadReport(notification, episodeParseResult, item))
|
2013-02-26 06:58:57 +03:00
|
|
|
searchResult.Successes.AddRange(episodeParseResult.Episodes.Select(e => e.Id));
|
2013-01-13 11:24:48 +03:00
|
|
|
}
|
2013-01-11 03:35:33 +03:00
|
|
|
}
|
2013-01-13 11:24:48 +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-01-13 11:24:48 +03:00
|
|
|
searchResult.SearchHistoryItems = items;
|
|
|
|
return searchResult;
|
2013-01-11 03:35:33 +03:00
|
|
|
}
|
|
|
|
|
2013-01-13 11:24:48 +03:00
|
|
|
public virtual Boolean DownloadReport(ProgressNotification notification, EpisodeParseResult episodeParseResult, SearchHistoryItem item)
|
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
|
|
|
notification.CurrentMessage = String.Format("{0} Added to download queue", episodeParseResult);
|
2013-01-11 03:35:33 +03:00
|
|
|
item.Success = true;
|
2013-01-13 11:24:48 +03:00
|
|
|
return true;
|
2013-01-11 03:35:33 +03:00
|
|
|
}
|
2013-01-13 11:24:48 +03:00
|
|
|
|
2013-03-06 23:30:53 +03:00
|
|
|
item.SearchError = ReportRejectionReasons.DownloadClientFailure;
|
2013-01-11 03:35:33 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
logger.ErrorException("Unable to add report to download queue." + episodeParseResult, e);
|
|
|
|
notification.CurrentMessage = String.Format("Unable to add report to download queue. {0}", episodeParseResult);
|
2013-03-06 23:30:53 +03:00
|
|
|
item.SearchError = ReportRejectionReasons.DownloadClientFailure;
|
2013-01-11 03:35:33 +03:00
|
|
|
}
|
|
|
|
|
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-01-24 09:36:37 +03:00
|
|
|
{
|
2013-03-02 21:25:39 +03:00
|
|
|
var seasonTitle = _sceneMappingService.GetSceneName(series.Id, seasonNumber);
|
2013-01-24 09:36:37 +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");
|
2013-01-23 06:57:51 +03:00
|
|
|
title = Regex.Replace(title, @"[^\w\d\s\-]", "");
|
2013-01-11 03:35:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|