1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Core/IndexerSearch/NzbSearchService.cs

187 lines
6.9 KiB
C#
Raw Normal View History

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