2011-05-26 05:44:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2011-06-14 01:23:04 +00:00
|
|
|
|
using Ninject;
|
2011-05-26 05:44:59 +00:00
|
|
|
|
using NLog;
|
2011-06-03 01:15:02 +00:00
|
|
|
|
using NzbDrone.Core.Helpers;
|
2011-05-26 05:44:59 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
|
|
|
|
using NzbDrone.Core.Providers.Indexer;
|
2011-05-27 02:12:28 +00:00
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-05-26 05:44:59 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers.Jobs
|
|
|
|
|
{
|
|
|
|
|
public class EpisodeSearchJob : IJob
|
|
|
|
|
{
|
|
|
|
|
private readonly InventoryProvider _inventoryProvider;
|
|
|
|
|
private readonly DownloadProvider _downloadProvider;
|
|
|
|
|
private readonly IndexerProvider _indexerProvider;
|
|
|
|
|
private readonly EpisodeProvider _episodeProvider;
|
2011-06-14 02:15:55 +00:00
|
|
|
|
private readonly SceneNameMappingProvider _sceneNameMappingProvider;
|
2011-05-26 05:44:59 +00:00
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-06-14 02:15:55 +00:00
|
|
|
|
public EpisodeSearchJob(InventoryProvider inventoryProvider, DownloadProvider downloadProvider,
|
|
|
|
|
IndexerProvider indexerProvider, EpisodeProvider episodeProvider,
|
|
|
|
|
SceneNameMappingProvider sceneNameMappingProvider)
|
2011-05-26 05:44:59 +00:00
|
|
|
|
{
|
|
|
|
|
_inventoryProvider = inventoryProvider;
|
|
|
|
|
_downloadProvider = downloadProvider;
|
|
|
|
|
_indexerProvider = indexerProvider;
|
|
|
|
|
_episodeProvider = episodeProvider;
|
2011-06-14 02:15:55 +00:00
|
|
|
|
_sceneNameMappingProvider = sceneNameMappingProvider;
|
2011-05-26 05:44:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Episode Search"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int DefaultInterval
|
|
|
|
|
{
|
|
|
|
|
get { return 0; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start(ProgressNotification notification, int targetId)
|
|
|
|
|
{
|
2011-05-27 03:54:28 +00:00
|
|
|
|
if (targetId <= 0)
|
|
|
|
|
throw new ArgumentOutOfRangeException("targetId");
|
|
|
|
|
|
2011-05-26 05:44:59 +00:00
|
|
|
|
var episode = _episodeProvider.GetEpisode(targetId);
|
2011-06-14 01:23:04 +00:00
|
|
|
|
|
2011-05-27 03:54:28 +00:00
|
|
|
|
if (episode == null)
|
|
|
|
|
{
|
2011-05-28 19:23:35 +00:00
|
|
|
|
Logger.Error("Unable to find an episode {0} in database", targetId);
|
2011-05-27 03:54:28 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-03 01:15:02 +00:00
|
|
|
|
var series = episode.Series;
|
|
|
|
|
|
2011-05-27 02:12:28 +00:00
|
|
|
|
var indexers = _indexerProvider.GetEnabledIndexers();
|
2011-05-27 03:54:28 +00:00
|
|
|
|
var reports = new List<EpisodeParseResult>();
|
2011-05-26 05:44:59 +00:00
|
|
|
|
|
2011-06-14 02:15:55 +00:00
|
|
|
|
var title = _sceneNameMappingProvider.GetSceneName(series.SeriesId);
|
2011-06-03 01:15:02 +00:00
|
|
|
|
|
2011-06-14 01:23:04 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(title))
|
2011-06-03 01:15:02 +00:00
|
|
|
|
{
|
|
|
|
|
title = series.Title;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-27 02:12:28 +00:00
|
|
|
|
foreach (var indexer in indexers)
|
2011-05-26 05:44:59 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
notification.CurrentMessage = String.Format("Searching for {0} in {1}", episode, indexer.Name);
|
2011-05-27 03:54:28 +00:00
|
|
|
|
|
|
|
|
|
IList<EpisodeParseResult> indexerResults = new List<EpisodeParseResult>();
|
|
|
|
|
|
|
|
|
|
if (episode.IsDailyEpisode)
|
|
|
|
|
{
|
|
|
|
|
//TODO:Add support for daily episodes
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-06-03 01:15:02 +00:00
|
|
|
|
indexerResults = indexer.FetchEpisode(title, episode.SeasonNumber, episode.EpisodeNumber);
|
2011-05-27 03:54:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reports.AddRange(indexerResults);
|
2011-05-26 05:44:59 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2011-05-27 02:12:28 +00:00
|
|
|
|
Logger.ErrorException("An error has occurred while fetching items from " + indexer.Name, e);
|
2011-05-26 05:44:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
2011-05-27 02:12:28 +00:00
|
|
|
|
notification.CurrentMessage = "Processing search results";
|
|
|
|
|
|
2011-05-28 19:23:35 +00:00
|
|
|
|
reports.ForEach(c =>
|
|
|
|
|
{
|
2011-06-03 01:15:02 +00:00
|
|
|
|
c.Series = series;
|
2011-05-28 19:23:35 +00:00
|
|
|
|
c.Episodes = new List<Episode> { episode };
|
|
|
|
|
});
|
|
|
|
|
|
2011-05-27 02:12:28 +00:00
|
|
|
|
ProcessResults(notification, episode, reports);
|
|
|
|
|
}
|
2011-05-26 05:44:59 +00:00
|
|
|
|
|
2011-05-27 02:12:28 +00:00
|
|
|
|
public void ProcessResults(ProgressNotification notification, Episode episode, IEnumerable<EpisodeParseResult> reports)
|
|
|
|
|
{
|
2011-05-28 19:23:35 +00:00
|
|
|
|
foreach (var episodeParseResult in reports.OrderByDescending(c => c.Quality))
|
2011-05-26 05:44:59 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2011-05-28 19:23:35 +00:00
|
|
|
|
Logger.Trace("Analysing report " + episodeParseResult);
|
|
|
|
|
if (_inventoryProvider.IsQualityNeeded(episodeParseResult))
|
2011-05-26 05:44:59 +00:00
|
|
|
|
{
|
2011-05-28 19:23:35 +00:00
|
|
|
|
Logger.Debug("Found '{0}'. Adding to download queue.", episodeParseResult);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_downloadProvider.DownloadReport(episodeParseResult);
|
|
|
|
|
notification.CurrentMessage = String.Format("{0} {1} Added to download queue", episode, episodeParseResult.Quality);
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-26 05:44:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2011-05-27 02:12:28 +00:00
|
|
|
|
Logger.ErrorException("An error has occurred while processing parse result items from " + episodeParseResult, e);
|
2011-05-26 05:44:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-27 03:54:28 +00:00
|
|
|
|
|
2011-05-26 05:44:59 +00:00
|
|
|
|
Logger.Warn("Unable to find {0} in any of indexers.", episode);
|
2011-05-28 19:23:35 +00:00
|
|
|
|
notification.CurrentMessage = String.Format("Unable to find {0} in any of indexers.", episode);
|
2011-05-26 05:44:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|