2011-08-28 08:45:36 +03:00
|
|
|
using System;
|
2011-11-26 09:13:47 +03:00
|
|
|
using System.Collections;
|
2011-08-28 08:45:36 +03:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using Ninject;
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2012-02-07 08:08:07 +03:00
|
|
|
using NzbDrone.Core.Providers.DecisionEngine;
|
2011-08-28 08:45:36 +03:00
|
|
|
using NzbDrone.Core.Repository;
|
2012-04-20 09:42:13 +03:00
|
|
|
using NzbDrone.Core.Repository.Search;
|
2011-08-28 08:45:36 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
|
|
|
public class SearchProvider
|
|
|
|
{
|
|
|
|
//Season and Episode Searching
|
|
|
|
private readonly EpisodeProvider _episodeProvider;
|
|
|
|
private readonly DownloadProvider _downloadProvider;
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
|
|
|
private readonly IndexerProvider _indexerProvider;
|
|
|
|
private readonly SceneMappingProvider _sceneMappingProvider;
|
2012-02-07 08:08:07 +03:00
|
|
|
private readonly UpgradePossibleSpecification _upgradePossibleSpecification;
|
|
|
|
private readonly AllowedDownloadSpecification _allowedDownloadSpecification;
|
2012-04-20 09:42:13 +03:00
|
|
|
private readonly SearchResultProvider _searchResultProvider;
|
2011-08-28 08:45:36 +03:00
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
[Inject]
|
2012-02-07 08:08:07 +03:00
|
|
|
public SearchProvider(EpisodeProvider episodeProvider, DownloadProvider downloadProvider, SeriesProvider seriesProvider,
|
|
|
|
IndexerProvider indexerProvider, SceneMappingProvider sceneMappingProvider,
|
2012-04-20 09:42:13 +03:00
|
|
|
UpgradePossibleSpecification upgradePossibleSpecification, AllowedDownloadSpecification allowedDownloadSpecification,
|
|
|
|
SearchResultProvider searchResultProvider)
|
2011-08-28 08:45:36 +03:00
|
|
|
{
|
|
|
|
_episodeProvider = episodeProvider;
|
|
|
|
_downloadProvider = downloadProvider;
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
_indexerProvider = indexerProvider;
|
|
|
|
_sceneMappingProvider = sceneMappingProvider;
|
2012-02-07 08:08:07 +03:00
|
|
|
_upgradePossibleSpecification = upgradePossibleSpecification;
|
|
|
|
_allowedDownloadSpecification = allowedDownloadSpecification;
|
2012-04-20 09:42:13 +03:00
|
|
|
_searchResultProvider = searchResultProvider;
|
2011-08-28 08:45:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public SearchProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool SeasonSearch(ProgressNotification notification, int seriesId, int seasonNumber)
|
|
|
|
{
|
2012-04-20 09:42:13 +03:00
|
|
|
var searchResult = new SearchResult
|
|
|
|
{
|
|
|
|
SearchTime = DateTime.Now,
|
|
|
|
SeriesId = seriesId,
|
|
|
|
SeasonNumber = seasonNumber
|
|
|
|
};
|
|
|
|
|
2011-08-28 08:45:36 +03:00
|
|
|
var series = _seriesProvider.GetSeries(seriesId);
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
{
|
|
|
|
Logger.Error("Unable to find an series {0} in database", seriesId);
|
|
|
|
return false;
|
2012-04-20 09:42:13 +03:00
|
|
|
}
|
2011-08-28 08:45:36 +03:00
|
|
|
|
2011-11-26 09:13:47 +03:00
|
|
|
//Return false if the series is a daily series (we only support individual episode searching
|
|
|
|
if (series.IsDaily)
|
|
|
|
return false;
|
|
|
|
|
2011-09-04 11:12:30 +03:00
|
|
|
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
|
2011-08-28 08:45:36 +03:00
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
var reports = PerformSearch(notification, series, seasonNumber);
|
2011-08-28 08:45:36 +03:00
|
|
|
|
|
|
|
Logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
|
|
|
|
|
|
|
if (reports.Count == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Logger.Debug("Getting episodes from database for series: {0} and season: {1}", seriesId, seasonNumber);
|
2011-08-28 22:07:56 +03:00
|
|
|
var episodeNumbers = _episodeProvider.GetEpisodeNumbersBySeason(seriesId, seasonNumber);
|
2011-08-28 08:45:36 +03:00
|
|
|
|
2011-08-28 22:07:56 +03:00
|
|
|
if (episodeNumbers == null || episodeNumbers.Count == 0)
|
2011-08-28 08:45:36 +03:00
|
|
|
{
|
|
|
|
Logger.Warn("No episodes in database found for series: {0} and season: {1}.", seriesId, seasonNumber);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
notification.CurrentMessage = "Processing search results";
|
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
reports.Where(p => p.FullSeason && p.SeasonNumber == seasonNumber).ToList().ForEach(
|
|
|
|
e => e.EpisodeNumbers = episodeNumbers.ToList()
|
|
|
|
);
|
2011-08-28 08:45:36 +03:00
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
searchResult.SearchResultItems = ProcessSearchResults(notification, reports, series, seasonNumber);
|
2011-08-28 08:45:36 +03:00
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
return (searchResult.SearchResultItems.Select(s => s.Success).Count() == episodeNumbers.Count);
|
2011-08-28 08:45:36 +03:00
|
|
|
}
|
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
public virtual List<SearchResultItem> PartialSeasonSearch(ProgressNotification notification, int seriesId, int seasonNumber)
|
2011-08-28 08:45:36 +03:00
|
|
|
{
|
2012-04-20 09:42:13 +03:00
|
|
|
var searchResult = new SearchResult
|
|
|
|
{
|
|
|
|
SearchTime = DateTime.Now,
|
|
|
|
SeriesId = seriesId,
|
|
|
|
SeasonNumber = seasonNumber
|
|
|
|
};
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
var series = _seriesProvider.GetSeries(seriesId);
|
|
|
|
|
|
|
|
if (series == null)
|
2011-08-28 08:45:36 +03:00
|
|
|
{
|
2011-11-18 05:36:53 +03:00
|
|
|
Logger.Error("Unable to find an series {0} in database", seriesId);
|
2012-04-20 09:42:13 +03:00
|
|
|
return new List<SearchResultItem>();
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
2011-11-17 09:32:44 +03:00
|
|
|
|
2011-11-26 09:13:47 +03:00
|
|
|
//Return empty list if the series is a daily series (we only support individual episode searching
|
|
|
|
if (series.IsDaily)
|
2012-04-20 09:42:13 +03:00
|
|
|
return new List<SearchResultItem>();
|
2011-11-26 09:13:47 +03:00
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
|
|
|
|
var episodes = _episodeProvider.GetEpisodesBySeason(seriesId, seasonNumber);
|
|
|
|
var reports = PerformSearch(notification, series, seasonNumber, episodes);
|
|
|
|
Logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
2011-08-28 08:45:36 +03:00
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
if (reports.Count == 0)
|
2012-04-20 09:42:13 +03:00
|
|
|
return new List<SearchResultItem>();
|
2011-08-28 08:45:36 +03:00
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
notification.CurrentMessage = "Processing search results";
|
2012-04-20 09:42:13 +03:00
|
|
|
searchResult.SearchResultItems = ProcessSearchResults(notification, reports, series, seasonNumber);
|
2011-08-28 08:45:36 +03:00
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
_searchResultProvider.Add(searchResult);
|
|
|
|
return searchResult.SearchResultItems;
|
2011-08-28 08:45:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool EpisodeSearch(ProgressNotification notification, int episodeId)
|
|
|
|
{
|
|
|
|
var episode = _episodeProvider.GetEpisode(episodeId);
|
|
|
|
|
|
|
|
if (episode == null)
|
|
|
|
{
|
|
|
|
Logger.Error("Unable to find an episode {0} in database", episodeId);
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-26 09:13:47 +03:00
|
|
|
|
2012-01-18 10:45:21 +03:00
|
|
|
//Check to see if an upgrade is possible before attempting
|
2012-02-07 08:08:07 +03:00
|
|
|
if (!_upgradePossibleSpecification.IsSatisfiedBy(episode))
|
2012-01-18 10:45:21 +03:00
|
|
|
{
|
|
|
|
Logger.Info("Search for {0} was aborted, file in disk meets or exceeds Profile's Cutoff", episode);
|
2012-04-20 09:42:13 +03:00
|
|
|
notification.CurrentMessage = String.Format("Skipping search for {0}, the file you have is already at cutoff", episode);
|
2012-01-18 10:45:21 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-23 10:07:00 +03:00
|
|
|
notification.CurrentMessage = "Looking for " + episode;
|
2011-08-28 08:45:36 +03:00
|
|
|
|
2011-11-26 09:13:47 +03:00
|
|
|
if (episode.Series.IsDaily && !episode.AirDate.HasValue)
|
2011-08-28 08:45:36 +03:00
|
|
|
{
|
2011-11-26 09:13:47 +03:00
|
|
|
Logger.Warn("AirDate is not Valid for: {0}", episode);
|
2012-04-20 09:42:13 +03:00
|
|
|
notification.CurrentMessage = String.Format("Search for {0} Failed, AirDate is invalid", episode);
|
2011-11-26 09:13:47 +03:00
|
|
|
return false;
|
2011-08-28 08:45:36 +03:00
|
|
|
}
|
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
var searchResult = new SearchResult
|
|
|
|
{
|
|
|
|
SearchTime = DateTime.Now,
|
|
|
|
SeriesId = episode.Series.SeriesId
|
|
|
|
};
|
|
|
|
|
2011-12-01 08:08:36 +03:00
|
|
|
var reports = PerformSearch(notification, episode.Series, episode.SeasonNumber, new List<Episode> { episode });
|
2011-08-28 08:45:36 +03:00
|
|
|
|
|
|
|
Logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
|
|
|
notification.CurrentMessage = "Processing search results";
|
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
if (episode.Series.IsDaily)
|
|
|
|
{
|
|
|
|
searchResult.SearchResultItems = ProcessSearchResults(notification, reports, episode.Series, episode.AirDate.Value);
|
|
|
|
_searchResultProvider.Add(searchResult);
|
|
|
|
|
|
|
|
if (searchResult.SearchResultItems.Any(r => r.Success))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-21 11:16:15 +03:00
|
|
|
else
|
2012-04-20 09:42:13 +03:00
|
|
|
{
|
|
|
|
searchResult.EpisodeId = episodeId;
|
2012-04-21 11:16:15 +03:00
|
|
|
searchResult.SearchResultItems = ProcessSearchResults(notification, reports, episode.Series, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
_searchResultProvider.Add(searchResult);
|
|
|
|
|
|
|
|
if (searchResult.SearchResultItems.Any(r => r.Success))
|
2011-11-24 07:50:09 +03:00
|
|
|
return true;
|
2012-04-20 09:42:13 +03:00
|
|
|
}
|
2011-08-28 08:45:36 +03:00
|
|
|
|
2011-11-24 07:50:09 +03:00
|
|
|
Logger.Warn("Unable to find {0} in any of indexers.", episode);
|
2012-02-07 08:08:07 +03:00
|
|
|
|
2012-01-23 10:07:00 +03:00
|
|
|
if (reports.Any())
|
|
|
|
{
|
2012-04-15 01:33:58 +03:00
|
|
|
notification.CurrentMessage = String.Format("Sorry, couldn't find {0}, that matches your preferences.", episode);
|
2012-01-23 10:07:00 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notification.CurrentMessage = String.Format("Sorry, couldn't find you {0} in any of indexers.", episode);
|
|
|
|
}
|
|
|
|
|
2011-11-24 07:50:09 +03:00
|
|
|
return false;
|
2011-08-28 08:45:36 +03:00
|
|
|
}
|
2011-09-01 09:58:54 +03:00
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
public List<EpisodeParseResult> PerformSearch(ProgressNotification notification, Series series, int seasonNumber, IList<Episode> episodes = null)
|
2011-09-01 09:58:54 +03:00
|
|
|
{
|
2011-11-18 05:36:53 +03:00
|
|
|
//If single episode, do a single episode search, if full season then do a full season search, otherwise, do a partial search
|
2011-09-01 09:58:54 +03:00
|
|
|
|
|
|
|
var indexers = _indexerProvider.GetEnabledIndexers();
|
|
|
|
var reports = new List<EpisodeParseResult>();
|
|
|
|
|
|
|
|
var title = _sceneMappingProvider.GetSceneName(series.SeriesId);
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(title))
|
|
|
|
{
|
|
|
|
title = series.Title;
|
|
|
|
}
|
|
|
|
|
2012-02-07 08:08:07 +03:00
|
|
|
foreach (var indexer in indexers)
|
2011-09-01 09:58:54 +03:00
|
|
|
{
|
2011-11-18 05:36:53 +03:00
|
|
|
try
|
2011-09-01 09:58:54 +03:00
|
|
|
{
|
2011-11-18 05:36:53 +03:00
|
|
|
if (episodes == null)
|
|
|
|
reports.AddRange(indexer.FetchSeason(title, seasonNumber));
|
2011-09-01 09:58:54 +03:00
|
|
|
|
2011-11-26 09:13:47 +03:00
|
|
|
//Treat as single episode
|
|
|
|
else if (episodes.Count == 1)
|
|
|
|
{
|
|
|
|
if (!series.IsDaily)
|
|
|
|
reports.AddRange(indexer.FetchEpisode(title, seasonNumber, episodes.First().EpisodeNumber));
|
|
|
|
|
|
|
|
//Daily Episode
|
|
|
|
else
|
|
|
|
reports.AddRange(indexer.FetchDailyEpisode(title, episodes.First().AirDate.Value));
|
|
|
|
}
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Treat as Partial Season
|
|
|
|
else
|
2011-09-01 09:58:54 +03:00
|
|
|
{
|
2011-11-18 05:36:53 +03:00
|
|
|
var prefixes = GetEpisodeNumberPrefixes(episodes.Select(s => s.EpisodeNumber));
|
|
|
|
|
2011-11-26 09:13:47 +03:00
|
|
|
foreach (var episodePrefix in prefixes)
|
2011-11-18 05:36:53 +03:00
|
|
|
{
|
|
|
|
reports.AddRange(indexer.FetchPartialSeason(title, seasonNumber, episodePrefix));
|
|
|
|
}
|
2011-09-01 09:58:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.ErrorException("An error has occurred while fetching items from " + indexer.Name, e);
|
|
|
|
}
|
2011-09-01 09:58:54 +03:00
|
|
|
}
|
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
return reports;
|
2011-09-01 09:58:54 +03:00
|
|
|
}
|
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
public List<SearchResultItem> ProcessSearchResults(ProgressNotification notification, IEnumerable<EpisodeParseResult> reports, Series series, int seasonNumber, int? episodeNumber = null)
|
2011-09-01 09:58:54 +03:00
|
|
|
{
|
|
|
|
var successes = new List<int>();
|
2012-04-20 09:42:13 +03:00
|
|
|
var items = new List<SearchResultItem>();
|
2011-09-01 09:58:54 +03:00
|
|
|
|
2012-02-18 22:20:18 +03:00
|
|
|
foreach (var episodeParseResult in reports.OrderByDescending(c => c.Quality).ThenBy(c => c.Age))
|
2011-09-01 09:58:54 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Logger.Trace("Analysing report " + episodeParseResult);
|
2011-11-17 09:32:44 +03:00
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
var item = new SearchResultItem
|
|
|
|
{
|
|
|
|
ReportTitle = episodeParseResult.OriginalString,
|
2012-04-21 11:16:15 +03:00
|
|
|
NzbUrl = episodeParseResult.NzbUrl,
|
|
|
|
Indexer = episodeParseResult.Indexer
|
2012-04-20 09:42:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
items.Add(item);
|
|
|
|
|
2011-11-17 09:32:44 +03:00
|
|
|
//Get the matching series
|
|
|
|
episodeParseResult.Series = _seriesProvider.FindSeries(episodeParseResult.CleanTitle);
|
|
|
|
|
|
|
|
//If series is null or doesn't match the series we're looking for return
|
|
|
|
if (episodeParseResult.Series == null || episodeParseResult.Series.SeriesId != series.SeriesId)
|
2012-01-23 00:16:25 +03:00
|
|
|
{
|
|
|
|
Logger.Trace("Unexpected series for search: {0}. Skipping.", episodeParseResult.CleanTitle);
|
2012-04-20 09:42:13 +03:00
|
|
|
item.SearchError = ReportRejectionType.WrongSeries;
|
2011-11-17 09:32:44 +03:00
|
|
|
continue;
|
2012-01-23 00:16:25 +03:00
|
|
|
}
|
2011-11-17 09:32:44 +03:00
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
//If SeasonNumber doesn't match or episode is not in the in the list in the parse result, skip the report.
|
2011-11-17 09:32:44 +03:00
|
|
|
if (episodeParseResult.SeasonNumber != seasonNumber)
|
2012-01-23 00:16:25 +03:00
|
|
|
{
|
|
|
|
Logger.Trace("Season number does not match searched season number, skipping.");
|
2012-04-20 09:42:13 +03:00
|
|
|
item.SearchError = ReportRejectionType.WrongSeason;
|
2011-11-17 09:32:44 +03:00
|
|
|
continue;
|
2012-01-23 00:16:25 +03:00
|
|
|
}
|
2011-11-17 09:32:44 +03:00
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
//If the EpisodeNumber was passed in and it is not contained in the parseResult, skip the report.
|
|
|
|
if (episodeNumber.HasValue && !episodeParseResult.EpisodeNumbers.Contains(episodeNumber.Value))
|
2012-01-23 00:16:25 +03:00
|
|
|
{
|
|
|
|
Logger.Trace("Searched episode number is not contained in post, skipping.");
|
2012-04-20 09:42:13 +03:00
|
|
|
item.SearchError = ReportRejectionType.WrongEpisode;
|
2011-11-18 05:36:53 +03:00
|
|
|
continue;
|
2012-01-23 00:16:25 +03:00
|
|
|
}
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Make sure we haven't already downloaded a report with this episodenumber, if we have, skip the report.
|
2012-02-07 08:08:07 +03:00
|
|
|
if (successes.Intersect(episodeParseResult.EpisodeNumbers).Any())
|
2012-01-23 00:16:25 +03:00
|
|
|
{
|
|
|
|
Logger.Trace("Episode has already been downloaded in this search, skipping.");
|
2012-04-20 09:42:13 +03:00
|
|
|
item.SearchError = ReportRejectionType.Skipped;
|
2011-11-18 05:36:53 +03:00
|
|
|
continue;
|
2012-01-23 00:16:25 +03:00
|
|
|
}
|
2011-11-18 05:36:53 +03:00
|
|
|
|
2012-04-21 11:16:15 +03:00
|
|
|
item.SearchError = _allowedDownloadSpecification.IsSatisfiedBy(episodeParseResult);
|
|
|
|
if (item.SearchError == ReportRejectionType.None)
|
2011-09-01 09:58:54 +03:00
|
|
|
{
|
|
|
|
Logger.Debug("Found '{0}'. Adding to download queue.", episodeParseResult);
|
|
|
|
try
|
|
|
|
{
|
2011-11-18 05:36:53 +03:00
|
|
|
if (_downloadProvider.DownloadReport(episodeParseResult))
|
|
|
|
{
|
2012-02-22 07:43:19 +03:00
|
|
|
notification.CurrentMessage = String.Format("{0} Added to download queue", episodeParseResult);
|
2011-11-18 05:36:53 +03:00
|
|
|
|
|
|
|
//Add the list of episode numbers from this release
|
|
|
|
successes.AddRange(episodeParseResult.EpisodeNumbers);
|
2012-04-20 09:42:13 +03:00
|
|
|
item.Success = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item.SearchError = ReportRejectionType.DownloadClientFailure;
|
2011-11-18 05:36:53 +03:00
|
|
|
}
|
2011-09-01 09:58:54 +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);
|
2012-04-20 09:42:13 +03:00
|
|
|
item.SearchError = ReportRejectionType.DownloadClientFailure;
|
2011-09-01 09:58:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.ErrorException("An error has occurred while processing parse result items from " + episodeParseResult, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
return items;
|
2011-09-01 09:58:54 +03:00
|
|
|
}
|
2011-11-18 05:36:53 +03:00
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
public List<SearchResultItem> ProcessSearchResults(ProgressNotification notification, IEnumerable<EpisodeParseResult> reports, Series series, DateTime airDate)
|
2011-11-26 22:10:13 +03:00
|
|
|
{
|
2012-04-20 09:42:13 +03:00
|
|
|
var items = new List<SearchResultItem>();
|
|
|
|
var skip = false;
|
|
|
|
|
2011-11-26 22:10:13 +03:00
|
|
|
foreach (var episodeParseResult in reports.OrderByDescending(c => c.Quality))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-04-20 09:42:13 +03:00
|
|
|
var item = new SearchResultItem
|
|
|
|
{
|
|
|
|
ReportTitle = episodeParseResult.OriginalString,
|
2012-04-21 11:16:15 +03:00
|
|
|
NzbUrl = episodeParseResult.NzbUrl,
|
|
|
|
Indexer = episodeParseResult.Indexer
|
2012-04-20 09:42:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
items.Add(item);
|
|
|
|
|
|
|
|
if (skip)
|
|
|
|
{
|
|
|
|
item.SearchError = ReportRejectionType.Skipped;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-26 22:10:13 +03:00
|
|
|
Logger.Trace("Analysing report " + episodeParseResult);
|
|
|
|
|
|
|
|
//Get the matching series
|
|
|
|
episodeParseResult.Series = _seriesProvider.FindSeries(episodeParseResult.CleanTitle);
|
|
|
|
|
|
|
|
//If series is null or doesn't match the series we're looking for return
|
|
|
|
if (episodeParseResult.Series == null || episodeParseResult.Series.SeriesId != series.SeriesId)
|
2012-04-20 09:42:13 +03:00
|
|
|
{
|
|
|
|
item.SearchError = ReportRejectionType.WrongSeries;
|
2011-11-26 22:10:13 +03:00
|
|
|
continue;
|
2012-04-20 09:42:13 +03:00
|
|
|
}
|
2011-11-26 22:10:13 +03:00
|
|
|
|
|
|
|
//If parse result doesn't have an air date or it doesn't match passed in airdate, skip the report.
|
|
|
|
if (!episodeParseResult.AirDate.HasValue || episodeParseResult.AirDate.Value.Date != airDate.Date)
|
2012-04-20 09:42:13 +03:00
|
|
|
{
|
|
|
|
item.SearchError = ReportRejectionType.WrongEpisode;
|
2011-11-26 22:10:13 +03:00
|
|
|
continue;
|
2012-04-20 09:42:13 +03:00
|
|
|
}
|
2011-11-26 22:10:13 +03:00
|
|
|
|
2012-04-21 11:16:15 +03:00
|
|
|
item.SearchError = _allowedDownloadSpecification.IsSatisfiedBy(episodeParseResult);
|
|
|
|
if (item.SearchError == ReportRejectionType.None)
|
2011-11-26 22:10:13 +03:00
|
|
|
{
|
|
|
|
Logger.Debug("Found '{0}'. Adding to download queue.", episodeParseResult);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_downloadProvider.DownloadReport(episodeParseResult))
|
|
|
|
{
|
|
|
|
notification.CurrentMessage =
|
2012-01-27 09:57:07 +03:00
|
|
|
String.Format("{0} - {1} {2} Added to download queue",
|
2011-11-26 22:10:13 +03:00
|
|
|
episodeParseResult.Series.Title, episodeParseResult.AirDate.Value.ToShortDateString(), episodeParseResult.Quality);
|
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
item.Success = true;
|
|
|
|
skip = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item.SearchError = ReportRejectionType.DownloadClientFailure;
|
2011-11-26 22:10:13 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.ErrorException("An error has occurred while processing parse result items from " + episodeParseResult, e);
|
|
|
|
}
|
|
|
|
}
|
2012-04-20 09:42:13 +03:00
|
|
|
|
|
|
|
return items;
|
2011-11-26 22:10:13 +03:00
|
|
|
}
|
|
|
|
|
2011-11-18 05:36:53 +03:00
|
|
|
private List<int> GetEpisodeNumberPrefixes(IEnumerable<int> episodeNumbers)
|
|
|
|
{
|
|
|
|
var results = new List<int>();
|
|
|
|
|
|
|
|
foreach (var i in episodeNumbers)
|
|
|
|
{
|
|
|
|
results.Add(i / 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results.Distinct().ToList();
|
|
|
|
}
|
2011-08-28 08:45:36 +03:00
|
|
|
}
|
|
|
|
}
|