2011-08-28 05:45:36 +00:00
|
|
|
|
using System;
|
2011-11-26 06:13:47 +00:00
|
|
|
|
using System.Collections;
|
2011-08-28 05:45:36 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using NLog;
|
|
|
|
|
using Ninject;
|
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
|
|
|
|
using NzbDrone.Core.Providers.Jobs;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class SearchProvider
|
|
|
|
|
{
|
|
|
|
|
//Season and Episode Searching
|
|
|
|
|
private readonly EpisodeProvider _episodeProvider;
|
|
|
|
|
private readonly InventoryProvider _inventoryProvider;
|
|
|
|
|
private readonly DownloadProvider _downloadProvider;
|
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
|
|
|
|
private readonly IndexerProvider _indexerProvider;
|
|
|
|
|
private readonly SceneMappingProvider _sceneMappingProvider;
|
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
public SearchProvider(EpisodeProvider episodeProvider, InventoryProvider inventoryProvider,
|
|
|
|
|
DownloadProvider downloadProvider, SeriesProvider seriesProvider,
|
|
|
|
|
IndexerProvider indexerProvider, SceneMappingProvider sceneMappingProvider)
|
|
|
|
|
{
|
|
|
|
|
_episodeProvider = episodeProvider;
|
|
|
|
|
_inventoryProvider = inventoryProvider;
|
|
|
|
|
_downloadProvider = downloadProvider;
|
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
|
_indexerProvider = indexerProvider;
|
|
|
|
|
_sceneMappingProvider = sceneMappingProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SearchProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool SeasonSearch(ProgressNotification notification, int seriesId, int seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
var series = _seriesProvider.GetSeries(seriesId);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Unable to find an series {0} in database", seriesId);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-26 06:13:47 +00:00
|
|
|
|
//Return false if the series is a daily series (we only support individual episode searching
|
|
|
|
|
if (series.IsDaily)
|
|
|
|
|
return false;
|
|
|
|
|
|
2011-09-04 08:12:30 +00:00
|
|
|
|
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
|
2011-08-28 05:45:36 +00:00
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
var reports = PerformSearch(notification, series, seasonNumber);
|
2011-08-28 05:45:36 +00: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 19:07:56 +00:00
|
|
|
|
var episodeNumbers = _episodeProvider.GetEpisodeNumbersBySeason(seriesId, seasonNumber);
|
2011-08-28 05:45:36 +00:00
|
|
|
|
|
2011-08-28 19:07:56 +00:00
|
|
|
|
if (episodeNumbers == null || episodeNumbers.Count == 0)
|
2011-08-28 05:45:36 +00: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 02:36:53 +00:00
|
|
|
|
reports.Where(p => p.FullSeason && p.SeasonNumber == seasonNumber).ToList().ForEach(
|
|
|
|
|
e => e.EpisodeNumbers = episodeNumbers.ToList()
|
|
|
|
|
);
|
2011-08-28 05:45:36 +00:00
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
var downloadedEpisodes = ProcessSearchResults(notification, reports, series, seasonNumber);
|
2011-08-28 05:45:36 +00:00
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
downloadedEpisodes.Sort();
|
|
|
|
|
episodeNumbers.ToList().Sort();
|
|
|
|
|
|
|
|
|
|
//Returns true if the list of downloaded episodes matches the list of episode numbers
|
|
|
|
|
//(either a full season release was grabbed or all individual episodes)
|
|
|
|
|
return (downloadedEpisodes.SequenceEqual(episodeNumbers));
|
2011-08-28 05:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
public virtual List<int> PartialSeasonSearch(ProgressNotification notification, int seriesId, int seasonNumber)
|
2011-08-28 05:45:36 +00:00
|
|
|
|
{
|
2011-11-18 02:36:53 +00:00
|
|
|
|
//This method will search for episodes in a season in groups of 10 episodes S01E0, S01E1, S01E2, etc
|
|
|
|
|
|
|
|
|
|
var series = _seriesProvider.GetSeries(seriesId);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
2011-08-28 05:45:36 +00:00
|
|
|
|
{
|
2011-11-18 02:36:53 +00:00
|
|
|
|
Logger.Error("Unable to find an series {0} in database", seriesId);
|
|
|
|
|
return new List<int>();
|
|
|
|
|
}
|
2011-11-17 06:32:44 +00:00
|
|
|
|
|
2011-11-26 06:13:47 +00:00
|
|
|
|
//Return empty list if the series is a daily series (we only support individual episode searching
|
|
|
|
|
if (series.IsDaily)
|
|
|
|
|
return new List<int>();
|
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
|
2011-11-17 06:32:44 +00:00
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
var episodes = _episodeProvider.GetEpisodesBySeason(seriesId, seasonNumber);
|
2011-11-17 06:32:44 +00:00
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
var reports = PerformSearch(notification, series, seasonNumber, episodes);
|
2011-11-17 06:32:44 +00:00
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
Logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
2011-08-28 05:45:36 +00:00
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
if (reports.Count == 0)
|
|
|
|
|
return new List<int>();
|
2011-08-28 05:45:36 +00:00
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
notification.CurrentMessage = "Processing search results";
|
2011-08-28 05:45:36 +00:00
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
return ProcessSearchResults(notification, reports, series, seasonNumber);
|
2011-08-28 05:45:36 +00: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 06:13:47 +00:00
|
|
|
|
|
2011-08-28 05:45:36 +00:00
|
|
|
|
notification.CurrentMessage = "Searching for " + episode;
|
|
|
|
|
|
2011-09-06 00:44:03 +00:00
|
|
|
|
var series = _seriesProvider.GetSeries(episode.SeriesId);
|
2011-08-28 05:45:36 +00:00
|
|
|
|
|
2011-11-26 06:13:47 +00:00
|
|
|
|
if (episode.Series.IsDaily && !episode.AirDate.HasValue)
|
2011-08-28 05:45:36 +00:00
|
|
|
|
{
|
2011-11-26 06:13:47 +00:00
|
|
|
|
Logger.Warn("AirDate is not Valid for: {0}", episode);
|
|
|
|
|
return false;
|
2011-08-28 05:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-26 06:13:47 +00:00
|
|
|
|
var reports = PerformSearch(notification, series, episode.SeasonNumber, new List<Episode> { episode });
|
2011-08-28 05:45:36 +00:00
|
|
|
|
|
|
|
|
|
Logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
|
|
|
|
notification.CurrentMessage = "Processing search results";
|
|
|
|
|
|
2011-11-26 19:10:13 +00:00
|
|
|
|
if (!series.IsDaily && ProcessSearchResults(notification, reports, series, episode.SeasonNumber, episode.EpisodeNumber).Count == 1)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (series.IsDaily && ProcessSearchResults(notification, reports, series, episode.AirDate.Value))
|
2011-11-24 04:50:09 +00:00
|
|
|
|
return true;
|
2011-08-28 05:45:36 +00:00
|
|
|
|
|
2011-11-24 04:50:09 +00:00
|
|
|
|
Logger.Warn("Unable to find {0} in any of indexers.", episode);
|
|
|
|
|
notification.CurrentMessage = String.Format("Unable to find {0} in any of indexers.", episode);
|
|
|
|
|
return false;
|
2011-08-28 05:45:36 +00:00
|
|
|
|
}
|
2011-09-01 06:58:54 +00:00
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
public List<EpisodeParseResult> PerformSearch(ProgressNotification notification, Series series, int seasonNumber, IList<Episode> episodes = null)
|
2011-09-01 06:58:54 +00:00
|
|
|
|
{
|
2011-11-18 02:36:53 +00: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 06:58:54 +00:00
|
|
|
|
|
|
|
|
|
var indexers = _indexerProvider.GetEnabledIndexers();
|
|
|
|
|
var reports = new List<EpisodeParseResult>();
|
|
|
|
|
|
|
|
|
|
var title = _sceneMappingProvider.GetSceneName(series.SeriesId);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(title))
|
|
|
|
|
{
|
|
|
|
|
title = series.Title;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
foreach(var indexer in indexers)
|
2011-09-01 06:58:54 +00:00
|
|
|
|
{
|
2011-11-18 02:36:53 +00:00
|
|
|
|
try
|
2011-09-01 06:58:54 +00:00
|
|
|
|
{
|
2011-11-18 02:36:53 +00:00
|
|
|
|
if (episodes == null)
|
|
|
|
|
reports.AddRange(indexer.FetchSeason(title, seasonNumber));
|
2011-09-01 06:58:54 +00:00
|
|
|
|
|
2011-11-26 06:13:47 +00: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 02:36:53 +00:00
|
|
|
|
|
|
|
|
|
//Treat as Partial Season
|
|
|
|
|
else
|
2011-09-01 06:58:54 +00:00
|
|
|
|
{
|
2011-11-18 02:36:53 +00:00
|
|
|
|
var prefixes = GetEpisodeNumberPrefixes(episodes.Select(s => s.EpisodeNumber));
|
|
|
|
|
|
2011-11-26 06:13:47 +00:00
|
|
|
|
foreach (var episodePrefix in prefixes)
|
2011-11-18 02:36:53 +00:00
|
|
|
|
{
|
|
|
|
|
reports.AddRange(indexer.FetchPartialSeason(title, seasonNumber, episodePrefix));
|
|
|
|
|
}
|
2011-09-01 06:58:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("An error has occurred while fetching items from " + indexer.Name, e);
|
|
|
|
|
}
|
2011-09-01 06:58:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
return reports;
|
2011-09-01 06:58:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 02:36:53 +00:00
|
|
|
|
public List<int> ProcessSearchResults(ProgressNotification notification, IEnumerable<EpisodeParseResult> reports, Series series, int seasonNumber, int? episodeNumber = null)
|
2011-09-01 06:58:54 +00:00
|
|
|
|
{
|
|
|
|
|
var successes = new List<int>();
|
|
|
|
|
|
|
|
|
|
foreach (var episodeParseResult in reports.OrderByDescending(c => c.Quality))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Analysing report " + episodeParseResult);
|
2011-11-17 06:32:44 +00: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)
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-11-18 02:36:53 +00: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 06:32:44 +00:00
|
|
|
|
if (episodeParseResult.SeasonNumber != seasonNumber)
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-11-18 02:36:53 +00: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))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
//Make sure we haven't already downloaded a report with this episodenumber, if we have, skip the report.
|
|
|
|
|
if (successes.Intersect(episodeParseResult.EpisodeNumbers).Count() > 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-09-01 06:58:54 +00:00
|
|
|
|
if (_inventoryProvider.IsQualityNeeded(episodeParseResult))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Found '{0}'. Adding to download queue.", episodeParseResult);
|
|
|
|
|
try
|
|
|
|
|
{
|
2011-11-18 02:36:53 +00:00
|
|
|
|
if (_downloadProvider.DownloadReport(episodeParseResult))
|
|
|
|
|
{
|
|
|
|
|
notification.CurrentMessage =
|
|
|
|
|
String.Format("{0} - S{1:00}E{2:00} {3}Added to download queue",
|
|
|
|
|
episodeParseResult.Series.Title, episodeParseResult.SeasonNumber,
|
|
|
|
|
episodeParseResult.EpisodeNumbers[0], episodeParseResult.Quality);
|
|
|
|
|
|
|
|
|
|
//Add the list of episode numbers from this release
|
|
|
|
|
successes.AddRange(episodeParseResult.EpisodeNumbers);
|
|
|
|
|
}
|
2011-09-01 06:58:54 +00: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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return successes;
|
|
|
|
|
}
|
2011-11-18 02:36:53 +00:00
|
|
|
|
|
2011-11-26 19:10:13 +00:00
|
|
|
|
public bool ProcessSearchResults(ProgressNotification notification, IEnumerable<EpisodeParseResult> reports, Series series, DateTime airDate)
|
|
|
|
|
{
|
|
|
|
|
foreach (var episodeParseResult in reports.OrderByDescending(c => c.Quality))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
//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)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (_inventoryProvider.IsQualityNeeded(episodeParseResult))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Found '{0}'. Adding to download queue.", episodeParseResult);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_downloadProvider.DownloadReport(episodeParseResult))
|
|
|
|
|
{
|
|
|
|
|
notification.CurrentMessage =
|
|
|
|
|
String.Format("{0} - {1} {2}Added to download queue",
|
|
|
|
|
episodeParseResult.Series.Title, episodeParseResult.AirDate.Value.ToShortDateString(), episodeParseResult.Quality);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 02:36:53 +00: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 05:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|