2013-04-07 07:30:37 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-07-26 03:25:24 +00:00
|
|
|
using System.Globalization;
|
2013-04-07 07:30:37 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.DataAugmentation.Scene;
|
|
|
|
using NzbDrone.Core.DecisionEngine;
|
2013-09-11 23:33:34 +00:00
|
|
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
2013-04-07 07:30:37 +00:00
|
|
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
|
|
|
using NzbDrone.Core.Indexers;
|
2013-09-11 06:33:47 +00:00
|
|
|
using NzbDrone.Core.Instrumentation;
|
2013-04-15 01:41:39 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-04-07 07:30:37 +00:00
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
using System.Linq;
|
2013-07-12 06:10:34 +00:00
|
|
|
using NzbDrone.Common.TPL;
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.IndexerSearch
|
|
|
|
{
|
|
|
|
public interface ISearchForNzb
|
|
|
|
{
|
2013-06-07 00:17:57 +00:00
|
|
|
List<DownloadDecision> EpisodeSearch(int episodeId);
|
|
|
|
List<DownloadDecision> SeasonSearch(int seriesId, int seasonNumber);
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class NzbSearchService : ISearchForNzb
|
|
|
|
{
|
|
|
|
private readonly IIndexerService _indexerService;
|
|
|
|
private readonly IFetchFeedFromIndexers _feedFetcher;
|
|
|
|
private readonly ISceneMappingService _sceneMapping;
|
|
|
|
private readonly ISeriesService _seriesService;
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
private readonly IMakeDownloadDecision _makeDownloadDecision;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-06-19 02:08:29 +00:00
|
|
|
public NzbSearchService(IIndexerService indexerService,
|
|
|
|
IFetchFeedFromIndexers feedFetcher,
|
|
|
|
ISceneMappingService sceneMapping,
|
|
|
|
ISeriesService seriesService,
|
|
|
|
IEpisodeService episodeService,
|
|
|
|
IMakeDownloadDecision makeDownloadDecision,
|
|
|
|
Logger logger)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
_indexerService = indexerService;
|
|
|
|
_feedFetcher = feedFetcher;
|
|
|
|
_sceneMapping = sceneMapping;
|
|
|
|
_seriesService = seriesService;
|
|
|
|
_episodeService = episodeService;
|
|
|
|
_makeDownloadDecision = makeDownloadDecision;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2013-06-07 00:17:57 +00:00
|
|
|
public List<DownloadDecision> EpisodeSearch(int episodeId)
|
|
|
|
{
|
|
|
|
var episode = _episodeService.GetEpisode(episodeId);
|
|
|
|
var series = _seriesService.GetSeries(episode.SeriesId);
|
|
|
|
|
|
|
|
if (series.SeriesType == SeriesTypes.Daily)
|
|
|
|
{
|
2013-07-30 20:30:34 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(episode.AirDate))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Daily episode is missing AirDate. Try to refresh series info.");
|
|
|
|
}
|
|
|
|
|
2013-08-08 00:20:47 +00:00
|
|
|
return SearchDaily(series, DateTime.ParseExact(episode.AirDate, Episode.AIR_DATE_FORMAT, CultureInfo.InvariantCulture));
|
2013-06-07 00:17:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 05:50:32 +00:00
|
|
|
return SearchSingle(series, episode);
|
2013-06-07 00:17:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 05:50:32 +00:00
|
|
|
private List<DownloadDecision> SearchSingle(Series series, Episode episode)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-08-08 00:20:47 +00:00
|
|
|
var searchSpec = Get<SingleEpisodeSearchCriteria>(series, episode.SeasonNumber);
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-07-23 05:50:32 +00:00
|
|
|
if (series.UseSceneNumbering)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-07-23 05:50:32 +00:00
|
|
|
if (episode.SceneSeasonNumber > 0 && episode.SceneEpisodeNumber > 0)
|
|
|
|
{
|
|
|
|
searchSpec.EpisodeNumber = episode.SceneEpisodeNumber;
|
|
|
|
searchSpec.SeasonNumber = episode.SceneSeasonNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
searchSpec.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
searchSpec.SeasonNumber = episode.SeasonNumber;
|
|
|
|
}
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-23 05:50:32 +00:00
|
|
|
searchSpec.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
searchSpec.SeasonNumber = episode.SeasonNumber;
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Dispatch(indexer => _feedFetcher.Fetch(indexer, searchSpec), searchSpec);
|
|
|
|
}
|
|
|
|
|
2013-08-08 00:20:47 +00:00
|
|
|
private List<DownloadDecision> SearchDaily(Series series, DateTime airDate)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-08-08 00:20:47 +00:00
|
|
|
var searchSpec = Get<DailyEpisodeSearchCriteria>(series);
|
2013-04-07 07:30:37 +00:00
|
|
|
searchSpec.Airtime = airDate;
|
|
|
|
|
|
|
|
return Dispatch(indexer => _feedFetcher.Fetch(indexer, searchSpec), searchSpec);
|
|
|
|
}
|
|
|
|
|
2013-06-07 00:17:57 +00:00
|
|
|
public List<DownloadDecision> SeasonSearch(int seriesId, int seasonNumber)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-08-08 00:20:47 +00:00
|
|
|
var series = _seriesService.GetSeries(seriesId);
|
|
|
|
|
|
|
|
var searchSpec = Get<SeasonSearchCriteria>(series, seasonNumber);
|
2013-04-07 07:30:37 +00:00
|
|
|
searchSpec.SeasonNumber = seasonNumber;
|
|
|
|
|
|
|
|
return Dispatch(indexer => _feedFetcher.Fetch(indexer, searchSpec), searchSpec);
|
|
|
|
}
|
|
|
|
|
2013-08-08 00:20:47 +00:00
|
|
|
private TSpec Get<TSpec>(Series series, int seasonNumber = -1) where TSpec : SearchCriteriaBase, new()
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
var spec = new TSpec();
|
|
|
|
|
2013-08-08 00:20:47 +00:00
|
|
|
spec.SeriesId = series.Id;
|
|
|
|
spec.SeriesTvRageId = series.TvRageId;
|
2013-08-20 06:23:36 +00:00
|
|
|
spec.SceneTitle = _sceneMapping.GetSceneName(series.TvdbId);
|
2013-06-07 00:17:57 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(spec.SceneTitle))
|
|
|
|
{
|
|
|
|
spec.SceneTitle = series.Title;
|
|
|
|
}
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
private List<DownloadDecision> Dispatch(Func<IIndexer, IEnumerable<ReportInfo>> searchAction, SearchCriteriaBase criteriaBase)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-05-02 01:59:09 +00:00
|
|
|
var indexers = _indexerService.GetAvailableIndexers().ToList();
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-09-11 06:33:47 +00:00
|
|
|
_logger.ProgressInfo("Searching {0} indexers for {1}", indexers.Count, criteriaBase);
|
2013-05-30 01:35:26 +00:00
|
|
|
|
2013-09-04 05:01:28 +00:00
|
|
|
var reports = new List<ReportInfo>();
|
2013-05-30 01:35:26 +00:00
|
|
|
var taskList = new List<Task>();
|
|
|
|
var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
|
|
|
|
|
|
|
|
foreach (var indexer in indexers)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-05-30 01:35:26 +00:00
|
|
|
var indexerLocal = indexer;
|
|
|
|
|
|
|
|
taskList.Add(taskFactory.StartNew(() =>
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-05-30 01:35:26 +00:00
|
|
|
try
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-05-30 01:35:26 +00:00
|
|
|
var indexerReports = searchAction(indexerLocal);
|
|
|
|
|
|
|
|
lock (reports)
|
|
|
|
{
|
|
|
|
reports.AddRange(indexerReports);
|
|
|
|
}
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
2013-05-30 01:35:26 +00:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
2013-06-06 14:42:23 +00:00
|
|
|
_logger.ErrorException("Error while searching for " + criteriaBase, e);
|
2013-05-30 01:35:26 +00:00
|
|
|
}
|
2013-07-12 06:10:34 +00:00
|
|
|
}).LogExceptions());
|
2013-05-30 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Task.WaitAll(taskList.ToArray());
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-09-04 05:01:28 +00:00
|
|
|
_logger.Debug("Total of {0} reports were found for {1} from {2} indexers", reports.Count, criteriaBase, indexers.Count);
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
return _makeDownloadDecision.GetSearchDecision(reports, criteriaBase).ToList();
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|