2011-05-26 07:44:59 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
|
|
|
|
using NzbDrone.Core.Providers.Indexer;
|
2011-05-27 04:12:28 +02:00
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-05-26 07:44:59 +02: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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-05-27 04:12:28 +02:00
|
|
|
|
public EpisodeSearchJob(InventoryProvider inventoryProvider, DownloadProvider downloadProvider, IndexerProvider indexerProvider, EpisodeProvider episodeProvider)
|
2011-05-26 07:44:59 +02:00
|
|
|
|
{
|
|
|
|
|
_inventoryProvider = inventoryProvider;
|
|
|
|
|
_downloadProvider = downloadProvider;
|
|
|
|
|
_indexerProvider = indexerProvider;
|
|
|
|
|
_episodeProvider = episodeProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Episode Search"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int DefaultInterval
|
|
|
|
|
{
|
|
|
|
|
get { return 0; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start(ProgressNotification notification, int targetId)
|
|
|
|
|
{
|
|
|
|
|
var reports = new List<EpisodeParseResult>();
|
|
|
|
|
var episode = _episodeProvider.GetEpisode(targetId);
|
2011-05-27 04:12:28 +02:00
|
|
|
|
var indexers = _indexerProvider.GetEnabledIndexers();
|
2011-05-26 07:44:59 +02:00
|
|
|
|
|
2011-05-27 04:12:28 +02:00
|
|
|
|
foreach (var indexer in indexers)
|
2011-05-26 07:44:59 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
notification.CurrentMessage = String.Format("Searching for {0} in {1}", episode, indexer.Name);
|
|
|
|
|
reports.AddRange(indexer.FetchRss());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2011-05-27 04:12:28 +02:00
|
|
|
|
Logger.ErrorException("An error has occurred while fetching items from " + indexer.Name, e);
|
2011-05-26 07:44:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Debug("Finished searching all indexers. Total {0}", reports.Count);
|
2011-05-27 04:12:28 +02:00
|
|
|
|
notification.CurrentMessage = "Processing search results";
|
|
|
|
|
|
|
|
|
|
ProcessResults(notification, episode, reports);
|
|
|
|
|
}
|
2011-05-26 07:44:59 +02:00
|
|
|
|
|
2011-05-27 04:12:28 +02:00
|
|
|
|
public void ProcessResults(ProgressNotification notification, Episode episode, IEnumerable<EpisodeParseResult> reports)
|
|
|
|
|
{
|
2011-05-27 05:07:32 +02:00
|
|
|
|
foreach (var episodeParseResult in reports.OrderByDescending(c => c.Quality).ThenByDescending(c => c.Proper))
|
2011-05-26 07:44:59 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_inventoryProvider.IsNeeded(episodeParseResult))
|
|
|
|
|
{
|
|
|
|
|
_downloadProvider.DownloadReport(episodeParseResult);
|
|
|
|
|
notification.CurrentMessage = String.Format("{0} {1} Added to download queue", episode, episodeParseResult.Quality);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2011-05-27 04:12:28 +02:00
|
|
|
|
Logger.ErrorException("An error has occurred while processing parse result items from " + episodeParseResult, e);
|
2011-05-26 07:44:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-27 04:12:28 +02:00
|
|
|
|
|
2011-05-26 07:44:59 +02:00
|
|
|
|
Logger.Warn("Unable to find {0} in any of indexers.", episode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|