2011-08-28 07:45:36 +02:00
|
|
|
|
using System;
|
2011-11-26 07:13:47 +01:00
|
|
|
|
using System.Collections;
|
2011-08-28 07:45:36 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2012-09-09 01:38:42 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2011-08-28 07:45:36 +02:00
|
|
|
|
using NLog;
|
|
|
|
|
using Ninject;
|
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2012-02-07 06:08:07 +01:00
|
|
|
|
using NzbDrone.Core.Providers.DecisionEngine;
|
2011-08-28 07:45:36 +02:00
|
|
|
|
using NzbDrone.Core.Repository;
|
2012-04-20 08:42:13 +02:00
|
|
|
|
using NzbDrone.Core.Repository.Search;
|
2011-08-28 07:45:36 +02: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 06:08:07 +01:00
|
|
|
|
private readonly UpgradePossibleSpecification _upgradePossibleSpecification;
|
|
|
|
|
private readonly AllowedDownloadSpecification _allowedDownloadSpecification;
|
2012-04-23 09:38:42 +02:00
|
|
|
|
private readonly SearchHistoryProvider _searchHistoryProvider;
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-10-18 02:06:14 +02:00
|
|
|
|
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
|
|
|
|
[Inject]
|
2012-02-07 06:08:07 +01:00
|
|
|
|
public SearchProvider(EpisodeProvider episodeProvider, DownloadProvider downloadProvider, SeriesProvider seriesProvider,
|
|
|
|
|
IndexerProvider indexerProvider, SceneMappingProvider sceneMappingProvider,
|
2012-04-20 08:42:13 +02:00
|
|
|
|
UpgradePossibleSpecification upgradePossibleSpecification, AllowedDownloadSpecification allowedDownloadSpecification,
|
2012-04-23 09:38:42 +02:00
|
|
|
|
SearchHistoryProvider searchHistoryProvider)
|
2011-08-28 07:45:36 +02:00
|
|
|
|
{
|
|
|
|
|
_episodeProvider = episodeProvider;
|
|
|
|
|
_downloadProvider = downloadProvider;
|
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
|
_indexerProvider = indexerProvider;
|
|
|
|
|
_sceneMappingProvider = sceneMappingProvider;
|
2012-02-07 06:08:07 +01:00
|
|
|
|
_upgradePossibleSpecification = upgradePossibleSpecification;
|
|
|
|
|
_allowedDownloadSpecification = allowedDownloadSpecification;
|
2012-04-23 09:38:42 +02:00
|
|
|
|
_searchHistoryProvider = searchHistoryProvider;
|
2011-08-28 07:45:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SearchProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-18 01:52:26 +02:00
|
|
|
|
public virtual List<int> SeasonSearch(ProgressNotification notification, int seriesId, int seasonNumber)
|
2011-08-28 07:45:36 +02:00
|
|
|
|
{
|
2012-04-23 09:38:42 +02:00
|
|
|
|
var searchResult = new SearchHistory
|
2012-04-20 08:42:13 +02:00
|
|
|
|
{
|
|
|
|
|
SearchTime = DateTime.Now,
|
|
|
|
|
SeriesId = seriesId,
|
2012-05-09 01:05:08 +02:00
|
|
|
|
SeasonNumber = seasonNumber
|
2012-04-20 08:42:13 +02:00
|
|
|
|
};
|
|
|
|
|
|
2011-08-28 07:45:36 +02:00
|
|
|
|
var series = _seriesProvider.GetSeries(seriesId);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Error("Unable to find an series {0} in database", seriesId);
|
2012-05-18 01:52:26 +02:00
|
|
|
|
return new List<int>();
|
2012-11-16 17:39:54 +01:00
|
|
|
|
}
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2011-11-26 07:13:47 +01:00
|
|
|
|
if (series.IsDaily)
|
2012-11-16 17:39:54 +01:00
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Daily series detected, skipping season search: {0}", series.Title);
|
|
|
|
|
return new List<int>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Getting episodes from database for series: {0} and season: {1}", seriesId, seasonNumber);
|
|
|
|
|
var episodes = _episodeProvider.GetEpisodesBySeason(seriesId, seasonNumber);
|
|
|
|
|
|
|
|
|
|
if (episodes == null || episodes.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.Warn("No episodes in database found for series: {0} and season: {1}.", seriesId, seasonNumber);
|
2012-05-18 01:52:26 +02:00
|
|
|
|
return new List<int>();
|
2012-11-16 17:39:54 +01:00
|
|
|
|
}
|
2011-11-26 07:13:47 +01:00
|
|
|
|
|
2011-09-04 10:12:30 +02:00
|
|
|
|
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-11-16 17:39:54 +01:00
|
|
|
|
List<EpisodeParseResult> reports;
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-11-16 17:39:54 +01:00
|
|
|
|
if (series.UseSceneNumbering)
|
|
|
|
|
{
|
|
|
|
|
var sceneSeasonNumbers = episodes.Select(e => e.SceneSeasonNumber).ToList();
|
|
|
|
|
var sceneEpisodeNumbers = episodes.Select(e => e.SceneEpisodeNumber).ToList();
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-11-16 17:39:54 +01:00
|
|
|
|
if (sceneSeasonNumbers.Distinct().Count() > 1)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Uses scene numbering, but multiple seasons found, skipping.");
|
|
|
|
|
return new List<int>();
|
|
|
|
|
}
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-11-16 17:39:54 +01:00
|
|
|
|
reports = PerformSeasonSearch(series, sceneSeasonNumbers.First());
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-11-16 17:39:54 +01:00
|
|
|
|
reports.Where(p => p.FullSeason && p.SeasonNumber == sceneSeasonNumbers.First()).ToList().ForEach(
|
|
|
|
|
e => e.EpisodeNumbers = sceneEpisodeNumbers.ToList()
|
|
|
|
|
);
|
2011-08-28 07:45:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-16 17:39:54 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reports = PerformSeasonSearch(series, seasonNumber);
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-11-16 17:39:54 +01:00
|
|
|
|
reports.Where(p => p.FullSeason && p.SeasonNumber == seasonNumber).ToList().ForEach(
|
|
|
|
|
e => e.EpisodeNumbers = episodes.Select(ep => ep.EpisodeNumber).ToList()
|
2011-11-18 03:36:53 +01:00
|
|
|
|
);
|
2012-11-16 17:39:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
|
|
|
|
|
|
|
|
|
if (reports.Count == 0)
|
|
|
|
|
return new List<int>();
|
|
|
|
|
|
|
|
|
|
notification.CurrentMessage = "Processing search results";
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-04-23 09:38:42 +02:00
|
|
|
|
searchResult.SearchHistoryItems = ProcessSearchResults(notification, reports, searchResult, series, seasonNumber);
|
|
|
|
|
_searchHistoryProvider.Add(searchResult);
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-05-18 01:52:26 +02:00
|
|
|
|
return searchResult.Successes;
|
2011-08-28 07:45:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-23 08:31:11 +02:00
|
|
|
|
public virtual List<int> PartialSeasonSearch(ProgressNotification notification, int seriesId, int seasonNumber)
|
2011-08-28 07:45:36 +02:00
|
|
|
|
{
|
2012-04-23 09:38:42 +02:00
|
|
|
|
var searchResult = new SearchHistory
|
2012-04-20 08:42:13 +02:00
|
|
|
|
{
|
|
|
|
|
SearchTime = DateTime.Now,
|
|
|
|
|
SeriesId = seriesId,
|
2012-05-09 01:05:08 +02:00
|
|
|
|
SeasonNumber = seasonNumber
|
2012-04-20 08:42:13 +02:00
|
|
|
|
};
|
2011-11-18 03:36:53 +01:00
|
|
|
|
|
|
|
|
|
var series = _seriesProvider.GetSeries(seriesId);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
2011-08-28 07:45:36 +02:00
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Error("Unable to find an series {0} in database", seriesId);
|
2012-04-23 08:31:11 +02:00
|
|
|
|
return new List<int>();
|
2011-11-18 03:36:53 +01:00
|
|
|
|
}
|
2011-11-17 07:32:44 +01:00
|
|
|
|
|
2011-11-26 07:13:47 +01:00
|
|
|
|
if (series.IsDaily)
|
2012-11-16 17:39:54 +01:00
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Daily series detected, skipping season search: {0}", series.Title);
|
2012-04-23 08:31:11 +02:00
|
|
|
|
return new List<int>();
|
2012-11-16 17:39:54 +01:00
|
|
|
|
}
|
2011-11-26 07:13:47 +01:00
|
|
|
|
|
2011-11-18 03:36:53 +01:00
|
|
|
|
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
|
|
|
|
|
var episodes = _episodeProvider.GetEpisodesBySeason(seriesId, seasonNumber);
|
2012-11-16 17:39:54 +01:00
|
|
|
|
|
|
|
|
|
List<EpisodeParseResult> reports;
|
|
|
|
|
|
|
|
|
|
if (series.UseSceneNumbering)
|
|
|
|
|
{
|
|
|
|
|
var sceneSeasonNumbers = episodes.Select(e => e.SceneSeasonNumber).ToList();
|
|
|
|
|
var sceneEpisodeNumbers = episodes.Select(e => e.SceneEpisodeNumber).ToList();
|
|
|
|
|
|
|
|
|
|
if (sceneSeasonNumbers.Distinct().Count() > 1)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Uses scene numbering, but multiple seasons found, skipping.");
|
|
|
|
|
return new List<int>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reports = PerformPartialSeasonSearch(series, sceneSeasonNumbers.First(), GetEpisodeNumberPrefixes(sceneEpisodeNumbers));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reports = PerformPartialSeasonSearch(series, seasonNumber, GetEpisodeNumberPrefixes(episodes.Select(e => e.EpisodeNumber)));
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2011-11-18 03:36:53 +01:00
|
|
|
|
if (reports.Count == 0)
|
2012-04-23 08:31:11 +02:00
|
|
|
|
return new List<int>();
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2011-11-18 03:36:53 +01:00
|
|
|
|
notification.CurrentMessage = "Processing search results";
|
2012-04-23 09:38:42 +02:00
|
|
|
|
searchResult.SearchHistoryItems = ProcessSearchResults(notification, reports, searchResult, series, seasonNumber);
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-04-23 09:38:42 +02:00
|
|
|
|
_searchHistoryProvider.Add(searchResult);
|
2012-04-23 08:31:11 +02:00
|
|
|
|
return searchResult.Successes;
|
2011-08-28 07:45:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool EpisodeSearch(ProgressNotification notification, int episodeId)
|
|
|
|
|
{
|
|
|
|
|
var episode = _episodeProvider.GetEpisode(episodeId);
|
|
|
|
|
|
|
|
|
|
if (episode == null)
|
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Error("Unable to find an episode {0} in database", episodeId);
|
2011-08-28 07:45:36 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-11-26 07:13:47 +01:00
|
|
|
|
|
2012-01-18 08:45:21 +01:00
|
|
|
|
//Check to see if an upgrade is possible before attempting
|
2012-02-07 06:08:07 +01:00
|
|
|
|
if (!_upgradePossibleSpecification.IsSatisfiedBy(episode))
|
2012-01-18 08:45:21 +01:00
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Info("Search for {0} was aborted, file in disk meets or exceeds Profile's Cutoff", episode);
|
2012-04-20 08:42:13 +02:00
|
|
|
|
notification.CurrentMessage = String.Format("Skipping search for {0}, the file you have is already at cutoff", episode);
|
2012-01-18 08:45:21 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-23 08:07:00 +01:00
|
|
|
|
notification.CurrentMessage = "Looking for " + episode;
|
2012-11-16 17:39:54 +01:00
|
|
|
|
List<EpisodeParseResult> reports;
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-04-23 09:38:42 +02:00
|
|
|
|
var searchResult = new SearchHistory
|
2012-04-20 08:42:13 +02:00
|
|
|
|
{
|
|
|
|
|
SearchTime = DateTime.Now,
|
2012-11-16 17:39:54 +01:00
|
|
|
|
SeriesId = episode.Series.SeriesId,
|
|
|
|
|
EpisodeId = episodeId
|
2012-04-20 08:42:13 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (episode.Series.IsDaily)
|
|
|
|
|
{
|
2012-11-16 17:39:54 +01:00
|
|
|
|
if (!episode.AirDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_logger.Warn("AirDate is not Valid for: {0}", episode);
|
|
|
|
|
notification.CurrentMessage = String.Format("Search for {0} Failed, AirDate is invalid", episode);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reports = PerformDailyEpisodeSearch(episode.Series, episode);
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
|
|
|
|
notification.CurrentMessage = "Processing search results";
|
|
|
|
|
|
2012-04-23 09:38:42 +02:00
|
|
|
|
searchResult.SearchHistoryItems = ProcessSearchResults(notification, reports, episode.Series, episode.AirDate.Value);
|
|
|
|
|
_searchHistoryProvider.Add(searchResult);
|
2012-04-20 08:42:13 +02:00
|
|
|
|
|
2012-04-23 09:38:42 +02:00
|
|
|
|
if (searchResult.SearchHistoryItems.Any(r => r.Success))
|
2012-04-20 08:42:13 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-18 02:06:14 +02:00
|
|
|
|
else if (episode.Series.UseSceneNumbering)
|
|
|
|
|
{
|
2012-10-18 17:33:18 +02:00
|
|
|
|
var seasonNumber = episode.SceneSeasonNumber;
|
|
|
|
|
var episodeNumber = episode.SceneEpisodeNumber;
|
|
|
|
|
|
2012-11-16 17:39:54 +01:00
|
|
|
|
if (seasonNumber == 0 && episodeNumber == 0)
|
2012-10-18 17:33:18 +02:00
|
|
|
|
{
|
|
|
|
|
seasonNumber = episode.SeasonNumber;
|
|
|
|
|
episodeNumber = episode.EpisodeNumber;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-16 17:39:54 +01:00
|
|
|
|
reports = PerformEpisodeSearch(episode.Series, seasonNumber, episodeNumber);
|
|
|
|
|
|
2012-10-18 02:06:14 +02:00
|
|
|
|
searchResult.SearchHistoryItems = ProcessSearchResults(
|
|
|
|
|
notification,
|
|
|
|
|
reports,
|
|
|
|
|
searchResult,
|
|
|
|
|
episode.Series,
|
2012-10-18 17:33:18 +02:00
|
|
|
|
seasonNumber,
|
|
|
|
|
episodeNumber
|
2012-10-18 02:06:14 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
_searchHistoryProvider.Add(searchResult);
|
|
|
|
|
|
|
|
|
|
if (searchResult.SearchHistoryItems.Any(r => r.Success))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-21 10:16:15 +02:00
|
|
|
|
else
|
2012-04-20 08:42:13 +02:00
|
|
|
|
{
|
2012-11-16 17:39:54 +01:00
|
|
|
|
reports = PerformEpisodeSearch(episode.Series, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
|
2012-04-23 09:38:42 +02:00
|
|
|
|
searchResult.SearchHistoryItems = ProcessSearchResults(notification, reports, searchResult, episode.Series, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
_searchHistoryProvider.Add(searchResult);
|
2012-04-21 10:16:15 +02:00
|
|
|
|
|
2012-04-23 09:38:42 +02:00
|
|
|
|
if (searchResult.SearchHistoryItems.Any(r => r.Success))
|
2012-10-18 02:06:14 +02:00
|
|
|
|
return true;
|
2012-04-20 08:42:13 +02:00
|
|
|
|
}
|
2011-08-28 07:45:36 +02:00
|
|
|
|
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Warn("Unable to find {0} in any of indexers.", episode);
|
2012-02-07 06:08:07 +01:00
|
|
|
|
|
2012-11-16 17:39:54 +01:00
|
|
|
|
notification.CurrentMessage = reports.Any() ? String.Format("Sorry, couldn't find {0}, that matches your preferences.", episode)
|
|
|
|
|
: String.Format("Sorry, couldn't find {0} in any of indexers.", episode);
|
2012-01-23 08:07:00 +01:00
|
|
|
|
|
2011-11-24 05:50:09 +01:00
|
|
|
|
return false;
|
2011-08-28 07:45:36 +02:00
|
|
|
|
}
|
2011-09-01 08:58:54 +02:00
|
|
|
|
|
2012-04-23 09:38:42 +02:00
|
|
|
|
public List<SearchHistoryItem> ProcessSearchResults(ProgressNotification notification, IEnumerable<EpisodeParseResult> reports, SearchHistory searchResult, Series series, int seasonNumber, int? episodeNumber = null)
|
2011-09-01 08:58:54 +02:00
|
|
|
|
{
|
2012-04-23 09:38:42 +02:00
|
|
|
|
var items = new List<SearchHistoryItem>();
|
2012-05-09 01:05:08 +02:00
|
|
|
|
searchResult.Successes = new List<int>();
|
2011-09-01 08:58:54 +02:00
|
|
|
|
|
2012-05-18 05:10:33 +02:00
|
|
|
|
foreach (var episodeParseResult in reports.OrderByDescending(c => c.Quality)
|
2012-05-26 21:17:51 +02:00
|
|
|
|
.ThenBy(c => c.EpisodeNumbers.MinOrDefault())
|
2012-05-18 05:10:33 +02:00
|
|
|
|
.ThenBy(c => c.Age))
|
2011-09-01 08:58:54 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Trace("Analysing report " + episodeParseResult);
|
2011-11-17 07:32:44 +01:00
|
|
|
|
|
2012-04-23 09:38:42 +02:00
|
|
|
|
var item = new SearchHistoryItem
|
2012-04-20 08:42:13 +02:00
|
|
|
|
{
|
|
|
|
|
ReportTitle = episodeParseResult.OriginalString,
|
2012-04-21 10:16:15 +02:00
|
|
|
|
NzbUrl = episodeParseResult.NzbUrl,
|
2012-04-23 08:31:11 +02:00
|
|
|
|
Indexer = episodeParseResult.Indexer,
|
2012-10-14 02:36:16 +02:00
|
|
|
|
Quality = episodeParseResult.Quality.Quality,
|
2012-04-23 08:31:11 +02:00
|
|
|
|
Proper = episodeParseResult.Quality.Proper,
|
|
|
|
|
Size = episodeParseResult.Size,
|
|
|
|
|
Age = episodeParseResult.Age,
|
|
|
|
|
Language = episodeParseResult.Language
|
2012-04-20 08:42:13 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
items.Add(item);
|
|
|
|
|
|
2011-11-17 07:32:44 +01: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-22 22:16:25 +01:00
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Trace("Unexpected series for search: {0}. Skipping.", episodeParseResult.CleanTitle);
|
2012-04-20 08:42:13 +02:00
|
|
|
|
item.SearchError = ReportRejectionType.WrongSeries;
|
2011-11-17 07:32:44 +01:00
|
|
|
|
continue;
|
2012-01-22 22:16:25 +01:00
|
|
|
|
}
|
2011-11-17 07:32:44 +01:00
|
|
|
|
|
2011-11-18 03:36:53 +01: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 07:32:44 +01:00
|
|
|
|
if (episodeParseResult.SeasonNumber != seasonNumber)
|
2012-01-22 22:16:25 +01:00
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Trace("Season number does not match searched season number, skipping.");
|
2012-04-20 08:42:13 +02:00
|
|
|
|
item.SearchError = ReportRejectionType.WrongSeason;
|
2011-11-17 07:32:44 +01:00
|
|
|
|
continue;
|
2012-01-22 22:16:25 +01:00
|
|
|
|
}
|
2011-11-17 07:32:44 +01:00
|
|
|
|
|
2011-11-18 03:36:53 +01: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-22 22:16:25 +01:00
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Trace("Searched episode number is not contained in post, skipping.");
|
2012-04-20 08:42:13 +02:00
|
|
|
|
item.SearchError = ReportRejectionType.WrongEpisode;
|
2011-11-18 03:36:53 +01:00
|
|
|
|
continue;
|
2012-01-22 22:16:25 +01:00
|
|
|
|
}
|
2011-11-18 03:36:53 +01:00
|
|
|
|
|
|
|
|
|
//Make sure we haven't already downloaded a report with this episodenumber, if we have, skip the report.
|
2012-05-09 00:49:10 +02:00
|
|
|
|
if (searchResult.Successes.Intersect(episodeParseResult.EpisodeNumbers).Any())
|
2012-01-22 22:16:25 +01:00
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Trace("Episode has already been downloaded in this search, skipping.");
|
2012-04-20 08:42:13 +02:00
|
|
|
|
item.SearchError = ReportRejectionType.Skipped;
|
2011-11-18 03:36:53 +01:00
|
|
|
|
continue;
|
2012-01-22 22:16:25 +01:00
|
|
|
|
}
|
2011-11-18 03:36:53 +01:00
|
|
|
|
|
2012-10-17 09:39:06 +02:00
|
|
|
|
episodeParseResult.Episodes = _episodeProvider.GetEpisodesByParseResult(episodeParseResult);
|
|
|
|
|
|
2012-04-21 10:16:15 +02:00
|
|
|
|
item.SearchError = _allowedDownloadSpecification.IsSatisfiedBy(episodeParseResult);
|
|
|
|
|
if (item.SearchError == ReportRejectionType.None)
|
2011-09-01 08:58:54 +02:00
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Debug("Found '{0}'. Adding to download queue.", episodeParseResult);
|
2011-09-01 08:58:54 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2011-11-18 03:36:53 +01:00
|
|
|
|
if (_downloadProvider.DownloadReport(episodeParseResult))
|
|
|
|
|
{
|
2012-02-22 05:43:19 +01:00
|
|
|
|
notification.CurrentMessage = String.Format("{0} Added to download queue", episodeParseResult);
|
2011-11-18 03:36:53 +01:00
|
|
|
|
|
|
|
|
|
//Add the list of episode numbers from this release
|
2012-05-09 00:49:10 +02:00
|
|
|
|
searchResult.Successes.AddRange(episodeParseResult.EpisodeNumbers);
|
2012-04-20 08:42:13 +02:00
|
|
|
|
item.Success = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item.SearchError = ReportRejectionType.DownloadClientFailure;
|
2011-11-18 03:36:53 +01:00
|
|
|
|
}
|
2011-09-01 08:58:54 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.ErrorException("Unable to add report to download queue." + episodeParseResult, e);
|
2011-09-01 08:58:54 +02:00
|
|
|
|
notification.CurrentMessage = String.Format("Unable to add report to download queue. {0}", episodeParseResult);
|
2012-04-20 08:42:13 +02:00
|
|
|
|
item.SearchError = ReportRejectionType.DownloadClientFailure;
|
2011-09-01 08:58:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.ErrorException("An error has occurred while processing parse result items from " + episodeParseResult, e);
|
2011-09-01 08:58:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-20 08:42:13 +02:00
|
|
|
|
return items;
|
2011-09-01 08:58:54 +02:00
|
|
|
|
}
|
2011-11-18 03:36:53 +01:00
|
|
|
|
|
2012-04-23 09:38:42 +02:00
|
|
|
|
public List<SearchHistoryItem> ProcessSearchResults(ProgressNotification notification, IEnumerable<EpisodeParseResult> reports, Series series, DateTime airDate)
|
2011-11-26 20:10:13 +01:00
|
|
|
|
{
|
2012-04-23 09:38:42 +02:00
|
|
|
|
var items = new List<SearchHistoryItem>();
|
2012-04-20 08:42:13 +02:00
|
|
|
|
var skip = false;
|
|
|
|
|
|
2011-11-26 20:10:13 +01:00
|
|
|
|
foreach (var episodeParseResult in reports.OrderByDescending(c => c.Quality))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-04-23 09:38:42 +02:00
|
|
|
|
var item = new SearchHistoryItem
|
2012-04-20 08:42:13 +02:00
|
|
|
|
{
|
|
|
|
|
ReportTitle = episodeParseResult.OriginalString,
|
2012-04-21 10:16:15 +02:00
|
|
|
|
NzbUrl = episodeParseResult.NzbUrl,
|
2012-04-23 08:31:11 +02:00
|
|
|
|
Indexer = episodeParseResult.Indexer,
|
2012-10-14 02:36:16 +02:00
|
|
|
|
Quality = episodeParseResult.Quality.Quality,
|
2012-04-23 08:31:11 +02:00
|
|
|
|
Proper = episodeParseResult.Quality.Proper,
|
|
|
|
|
Size = episodeParseResult.Size,
|
|
|
|
|
Age = episodeParseResult.Age,
|
|
|
|
|
Language = episodeParseResult.Language
|
2012-04-20 08:42:13 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
items.Add(item);
|
|
|
|
|
|
|
|
|
|
if (skip)
|
|
|
|
|
{
|
|
|
|
|
item.SearchError = ReportRejectionType.Skipped;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Trace("Analysing report " + episodeParseResult);
|
2011-11-26 20:10:13 +01: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-04-20 08:42:13 +02:00
|
|
|
|
{
|
|
|
|
|
item.SearchError = ReportRejectionType.WrongSeries;
|
2011-11-26 20:10:13 +01:00
|
|
|
|
continue;
|
2012-04-20 08:42:13 +02:00
|
|
|
|
}
|
2011-11-26 20:10:13 +01: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 08:42:13 +02:00
|
|
|
|
{
|
|
|
|
|
item.SearchError = ReportRejectionType.WrongEpisode;
|
2011-11-26 20:10:13 +01:00
|
|
|
|
continue;
|
2012-04-20 08:42:13 +02:00
|
|
|
|
}
|
2011-11-26 20:10:13 +01:00
|
|
|
|
|
2012-10-17 09:39:06 +02:00
|
|
|
|
episodeParseResult.Episodes = _episodeProvider.GetEpisodesByParseResult(episodeParseResult);
|
|
|
|
|
|
2012-04-21 10:16:15 +02:00
|
|
|
|
item.SearchError = _allowedDownloadSpecification.IsSatisfiedBy(episodeParseResult);
|
|
|
|
|
if (item.SearchError == ReportRejectionType.None)
|
2011-11-26 20:10:13 +01:00
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.Debug("Found '{0}'. Adding to download queue.", episodeParseResult);
|
2011-11-26 20:10:13 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_downloadProvider.DownloadReport(episodeParseResult))
|
|
|
|
|
{
|
|
|
|
|
notification.CurrentMessage =
|
2012-01-27 07:57:07 +01:00
|
|
|
|
String.Format("{0} - {1} {2} Added to download queue",
|
2011-11-26 20:10:13 +01:00
|
|
|
|
episodeParseResult.Series.Title, episodeParseResult.AirDate.Value.ToShortDateString(), episodeParseResult.Quality);
|
|
|
|
|
|
2012-04-20 08:42:13 +02:00
|
|
|
|
item.Success = true;
|
|
|
|
|
skip = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item.SearchError = ReportRejectionType.DownloadClientFailure;
|
2011-11-26 20:10:13 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.ErrorException("Unable to add report to download queue." + episodeParseResult, e);
|
2011-11-26 20:10:13 +01:00
|
|
|
|
notification.CurrentMessage = String.Format("Unable to add report to download queue. {0}", episodeParseResult);
|
2012-04-23 08:31:11 +02:00
|
|
|
|
item.SearchError = ReportRejectionType.DownloadClientFailure;
|
2011-11-26 20:10:13 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2012-10-18 02:06:14 +02:00
|
|
|
|
_logger.ErrorException("An error has occurred while processing parse result items from " + episodeParseResult, e);
|
2011-11-26 20:10:13 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-20 08:42:13 +02:00
|
|
|
|
|
|
|
|
|
return items;
|
2011-11-26 20:10:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 03:36:53 +01: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();
|
|
|
|
|
}
|
2012-11-16 17:39:54 +01:00
|
|
|
|
|
|
|
|
|
public List<EpisodeParseResult> PerformEpisodeSearch(Series series, int seasonNumber, int episodeNumber)
|
|
|
|
|
{
|
|
|
|
|
var reports = new List<EpisodeParseResult>();
|
|
|
|
|
var title = GetSeriesTitle(series);
|
|
|
|
|
|
|
|
|
|
Parallel.ForEach(_indexerProvider.GetEnabledIndexers(), indexer =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
reports.AddRange(indexer.FetchEpisode(title, seasonNumber, episodeNumber));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException(String.Format("An error has occurred while searching for {0}-S{1:00}E{2:00} from: {3}",
|
|
|
|
|
series.Title, seasonNumber, episodeNumber, indexer.Name), e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return reports;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<EpisodeParseResult> PerformDailyEpisodeSearch(Series series, Episode episode)
|
|
|
|
|
{
|
|
|
|
|
var reports = new List<EpisodeParseResult>();
|
|
|
|
|
var title = GetSeriesTitle(series);
|
|
|
|
|
|
|
|
|
|
Parallel.ForEach(_indexerProvider.GetEnabledIndexers(), indexer =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Episode {0} is a daily episode, searching as daily", episode);
|
|
|
|
|
reports.AddRange(indexer.FetchDailyEpisode(title, episode.AirDate.Value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException(String.Format("An error has occurred while searching for {0}-{1} from: {2}",
|
|
|
|
|
series.Title, episode.AirDate, indexer.Name), e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return reports;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<EpisodeParseResult> PerformPartialSeasonSearch(Series series, int seasonNumber, List<int> prefixes)
|
|
|
|
|
{
|
|
|
|
|
var reports = new List<EpisodeParseResult>();
|
|
|
|
|
var title = GetSeriesTitle(series);
|
|
|
|
|
|
|
|
|
|
Parallel.ForEach(_indexerProvider.GetEnabledIndexers(), indexer =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var episodePrefix in prefixes)
|
|
|
|
|
{
|
|
|
|
|
reports.AddRange(indexer.FetchPartialSeason(title, seasonNumber, episodePrefix));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException(String.Format("An error has occurred while searching for {0}-S{1:00} from: {2}",
|
|
|
|
|
series.Title, seasonNumber, indexer.Name), e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return reports;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<EpisodeParseResult> PerformSeasonSearch(Series series, int seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
var reports = new List<EpisodeParseResult>();
|
|
|
|
|
var title = GetSeriesTitle(series);
|
|
|
|
|
|
|
|
|
|
Parallel.ForEach(_indexerProvider.GetEnabledIndexers(), indexer =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
reports.AddRange(indexer.FetchSeason(title, seasonNumber));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("An error has occurred while searching for items from: " + indexer.Name, e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return reports;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetSeriesTitle(Series series)
|
|
|
|
|
{
|
|
|
|
|
var title = _sceneMappingProvider.GetSceneName(series.SeriesId);
|
|
|
|
|
|
|
|
|
|
if (String.IsNullOrWhiteSpace(title))
|
|
|
|
|
title = series.Title;
|
|
|
|
|
|
|
|
|
|
return title;
|
|
|
|
|
}
|
2011-08-28 07:45:36 +02:00
|
|
|
|
}
|
|
|
|
|
}
|