Merge branch 'markus' into kay.one

This commit is contained in:
kay.one 2011-11-26 19:19:40 -08:00
commit ed9460da66
25 changed files with 966 additions and 67 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NLog;
@ -48,6 +49,10 @@ namespace NzbDrone.Core.Providers
return false;
}
//Return false if the series is a daily series (we only support individual episode searching
if (series.IsDaily)
return false;
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
var reports = PerformSearch(notification, series, seasonNumber);
@ -94,6 +99,10 @@ namespace NzbDrone.Core.Providers
return new List<int>();
}
//Return empty list if the series is a daily series (we only support individual episode searching
if (series.IsDaily)
return new List<int>();
notification.CurrentMessage = String.Format("Searching for {0} Season {1}", series.Title, seasonNumber);
var episodes = _episodeProvider.GetEpisodesBySeason(seriesId, seasonNumber);
@ -119,41 +128,26 @@ namespace NzbDrone.Core.Providers
Logger.Error("Unable to find an episode {0} in database", episodeId);
return false;
}
notification.CurrentMessage = "Searching for " + episode;
var series = _seriesProvider.GetSeries(episode.SeriesId);
var indexers = _indexerProvider.GetEnabledIndexers();
var reports = new List<EpisodeParseResult>();
var title = _sceneMappingProvider.GetSceneName(series.SeriesId);
if (string.IsNullOrWhiteSpace(title))
if (episode.Series.IsDaily && !episode.AirDate.HasValue)
{
title = series.Title;
Logger.Warn("AirDate is not Valid for: {0}", episode);
return false;
}
foreach (var indexer in indexers)
{
try
{
//notification.CurrentMessage = String.Format("Searching for {0} in {1}", episode, indexer.Name);
//TODO:Add support for daily episodes, maybe search using both date and season/episode?
var indexerResults = indexer.FetchEpisode(title, episode.SeasonNumber, episode.EpisodeNumber);
reports.AddRange(indexerResults);
}
catch (Exception e)
{
Logger.ErrorException("An error has occurred while fetching items from " + indexer.Name, e);
}
}
var reports = PerformSearch(notification, series, episode.SeasonNumber, new List<Episode> { episode });
Logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
notification.CurrentMessage = "Processing search results";
if (ProcessSearchResults(notification, reports, series, episode.SeasonNumber, episode.EpisodeNumber).Count == 1)
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))
return true;
Logger.Warn("Unable to find {0} in any of indexers.", episode);
@ -182,15 +176,23 @@ namespace NzbDrone.Core.Providers
if (episodes == null)
reports.AddRange(indexer.FetchSeason(title, seasonNumber));
else if(episodes.Count == 1)
reports.AddRange(indexer.FetchEpisode(title, seasonNumber, episodes.First().EpisodeNumber));
//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));
}
//Treat as Partial Season
else
{
var prefixes = GetEpisodeNumberPrefixes(episodes.Select(s => s.EpisodeNumber));
foreach(var episodePrefix in prefixes)
foreach (var episodePrefix in prefixes)
{
reports.AddRange(indexer.FetchPartialSeason(title, seasonNumber, episodePrefix));
}
@ -267,6 +269,54 @@ namespace NzbDrone.Core.Providers
return successes;
}
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;
}
private List<int> GetEpisodeNumberPrefixes(IEnumerable<int> episodeNumbers)
{
var results = new List<int>();