From 8ee0789c62eb86d5ac66d7da2926ea39492c4ba5 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 18 Jun 2013 19:08:29 -0700 Subject: [PATCH] Added SeasonSearchCommand --- NzbDrone.Api/Indexers/ReleaseModule.cs | 7 ++- .../DownloadApprovedReportsService.cs | 59 +++++++++++++++++++ .../IndexerSearch/NzbSearchService.cs | 11 ++-- .../IndexerSearch/SeasonSearchCommand.cs | 10 ++++ .../IndexerSearch/SeasonSearchService.cs | 27 +++++++++ NzbDrone.Core/Indexers/RssSyncService.cs | 34 +++-------- 6 files changed, 115 insertions(+), 33 deletions(-) create mode 100644 NzbDrone.Core/Download/DownloadApprovedReportsService.cs create mode 100644 NzbDrone.Core/IndexerSearch/SeasonSearchCommand.cs create mode 100644 NzbDrone.Core/IndexerSearch/SeasonSearchService.cs diff --git a/NzbDrone.Api/Indexers/ReleaseModule.cs b/NzbDrone.Api/Indexers/ReleaseModule.cs index a8949c6bb..97fc58cd6 100644 --- a/NzbDrone.Api/Indexers/ReleaseModule.cs +++ b/NzbDrone.Api/Indexers/ReleaseModule.cs @@ -19,7 +19,11 @@ namespace NzbDrone.Api.Indexers private readonly IDownloadService _downloadService; private readonly IParsingService _parsingService; - public ReleaseModule(IFetchAndParseRss rssFetcherAndParser, ISearchForNzb nzbSearchService, IMakeDownloadDecision downloadDecisionMaker, IDownloadService downloadService, IParsingService parsingService) + public ReleaseModule(IFetchAndParseRss rssFetcherAndParser, + ISearchForNzb nzbSearchService, + IMakeDownloadDecision downloadDecisionMaker, + IDownloadService downloadService, + IParsingService parsingService) { _rssFetcherAndParser = rssFetcherAndParser; _nzbSearchService = nzbSearchService; @@ -40,7 +44,6 @@ namespace NzbDrone.Api.Indexers return release; } - private List GetReleases() { if (Request.Query.episodeId != null) diff --git a/NzbDrone.Core/Download/DownloadApprovedReportsService.cs b/NzbDrone.Core/Download/DownloadApprovedReportsService.cs new file mode 100644 index 000000000..579f05828 --- /dev/null +++ b/NzbDrone.Core/Download/DownloadApprovedReportsService.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NLog; +using NzbDrone.Core.DecisionEngine; + +namespace NzbDrone.Core.Download +{ + public interface IDownloadApprovedReportsService + { + List DownloadApproved(List decisions); + } + + public class DownloadApprovedReportsService : IDownloadApprovedReportsService + { + private readonly IDownloadService _downloadService; + private readonly Logger _logger; + + public DownloadApprovedReportsService(IDownloadService downloadService, Logger logger) + { + _downloadService = downloadService; + _logger = logger; + } + + public List DownloadApproved(List decisions) + { + var qualifiedReports = decisions + .Where(c => c.Approved) + .ToList(); + + var remoteEpisodes = qualifiedReports + .Select(c => c.RemoteEpisode) + .OrderByDescending(c => c.ParsedEpisodeInfo.Quality) + .ThenBy(c => c.Episodes.Select(e => e.EpisodeNumber).MinOrDefault()) + .ThenBy(c => c.Report.Age) + .ToList(); + + var downloadedReports = new List(); + + foreach (var episodeParseResult in remoteEpisodes) + { + try + { + if (downloadedReports.Intersect(episodeParseResult.Episodes.Select(e => e.Id)).Any()) continue; + + _downloadService.DownloadReport(episodeParseResult); + downloadedReports.AddRange(episodeParseResult.Episodes.Select(e => e.Id)); + } + catch (Exception e) + { + _logger.WarnException("Couldn't add report to download queue. " + episodeParseResult, e); + } + } + + return qualifiedReports; + } + } +} diff --git a/NzbDrone.Core/IndexerSearch/NzbSearchService.cs b/NzbDrone.Core/IndexerSearch/NzbSearchService.cs index c72dd7aa1..e3f434c7a 100644 --- a/NzbDrone.Core/IndexerSearch/NzbSearchService.cs +++ b/NzbDrone.Core/IndexerSearch/NzbSearchService.cs @@ -28,7 +28,13 @@ namespace NzbDrone.Core.IndexerSearch private readonly IMakeDownloadDecision _makeDownloadDecision; private readonly Logger _logger; - public NzbSearchService(IIndexerService indexerService, IFetchFeedFromIndexers feedFetcher, ISceneMappingService sceneMapping, ISeriesService seriesService, IEpisodeService episodeService, IMakeDownloadDecision makeDownloadDecision, Logger logger) + public NzbSearchService(IIndexerService indexerService, + IFetchFeedFromIndexers feedFetcher, + ISceneMappingService sceneMapping, + ISeriesService seriesService, + IEpisodeService episodeService, + IMakeDownloadDecision makeDownloadDecision, + Logger logger) { _indexerService = indexerService; _feedFetcher = feedFetcher; @@ -39,8 +45,6 @@ namespace NzbDrone.Core.IndexerSearch _logger = logger; } - - public List EpisodeSearch(int episodeId) { var episode = _episodeService.GetEpisode(episodeId); @@ -54,7 +58,6 @@ namespace NzbDrone.Core.IndexerSearch return SearchSingle(episode.SeriesId, episode.SeasonNumber, episode.EpisodeNumber); } - private List SearchSingle(int seriesId, int seasonNumber, int episodeNumber) { var searchSpec = Get(seriesId, seasonNumber); diff --git a/NzbDrone.Core/IndexerSearch/SeasonSearchCommand.cs b/NzbDrone.Core/IndexerSearch/SeasonSearchCommand.cs new file mode 100644 index 000000000..cf9a8ba3e --- /dev/null +++ b/NzbDrone.Core/IndexerSearch/SeasonSearchCommand.cs @@ -0,0 +1,10 @@ +using NzbDrone.Common.Messaging; + +namespace NzbDrone.Core.IndexerSearch +{ + public class SeasonSearchCommand : ICommand + { + public int SeriesId { get; set; } + public int SeasonNumber { get; set; } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/IndexerSearch/SeasonSearchService.cs b/NzbDrone.Core/IndexerSearch/SeasonSearchService.cs new file mode 100644 index 000000000..e56079c7a --- /dev/null +++ b/NzbDrone.Core/IndexerSearch/SeasonSearchService.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NzbDrone.Common.Messaging; +using NzbDrone.Core.Download; + +namespace NzbDrone.Core.IndexerSearch +{ + public class SeasonSearchService : IExecute + { + private readonly ISearchForNzb _nzbSearchService; + private readonly IDownloadApprovedReportsService _downloadApprovedReportsService; + + public SeasonSearchService(ISearchForNzb nzbSearchService, IDownloadApprovedReportsService downloadApprovedReportsService) + { + _nzbSearchService = nzbSearchService; + _downloadApprovedReportsService = downloadApprovedReportsService; + } + + public void Execute(SeasonSearchCommand message) + { + var decisions = _nzbSearchService.SeasonSearch(message.SeriesId, message.SeasonNumber); + var qualified = _downloadApprovedReportsService.DownloadApproved(decisions); + } + } +} diff --git a/NzbDrone.Core/Indexers/RssSyncService.cs b/NzbDrone.Core/Indexers/RssSyncService.cs index 455ef7ed5..a581f2244 100644 --- a/NzbDrone.Core/Indexers/RssSyncService.cs +++ b/NzbDrone.Core/Indexers/RssSyncService.cs @@ -17,14 +17,17 @@ namespace NzbDrone.Core.Indexers { private readonly IFetchAndParseRss _rssFetcherAndParser; private readonly IMakeDownloadDecision _downloadDecisionMaker; - private readonly IDownloadService _downloadService; + private readonly IDownloadApprovedReportsService _downloadApprovedReportsService; private readonly Logger _logger; - public RssSyncService(IFetchAndParseRss rssFetcherAndParser, IMakeDownloadDecision downloadDecisionMaker, IDownloadService downloadService, Logger logger) + public RssSyncService(IFetchAndParseRss rssFetcherAndParser, + IMakeDownloadDecision downloadDecisionMaker, + IDownloadApprovedReportsService downloadApprovedReportsService, + Logger logger) { _rssFetcherAndParser = rssFetcherAndParser; _downloadDecisionMaker = downloadDecisionMaker; - _downloadService = downloadService; + _downloadApprovedReportsService = downloadApprovedReportsService; _logger = logger; } @@ -35,30 +38,7 @@ namespace NzbDrone.Core.Indexers var reports = _rssFetcherAndParser.Fetch(); var decisions = _downloadDecisionMaker.GetRssDecision(reports); - - var qualifiedReports = decisions - .Where(c => c.Approved) - .Select(c => c.RemoteEpisode) - .OrderByDescending(c => c.ParsedEpisodeInfo.Quality) - .ThenBy(c => c.Episodes.Select(e => e.EpisodeNumber).MinOrDefault()) - .ThenBy(c => c.Report.Age); - - var downloadedReports = new List(); - - foreach (var episodeParseResult in qualifiedReports) - { - try - { - if (downloadedReports.Intersect(episodeParseResult.Episodes.Select(e => e.Id)).Any()) continue; - - _downloadService.DownloadReport(episodeParseResult); - downloadedReports.AddRange(episodeParseResult.Episodes.Select(e => e.Id)); - } - catch (Exception e) - { - _logger.WarnException("Couldn't add report to download queue. " + episodeParseResult, e); - } - } + var qualifiedReports = _downloadApprovedReportsService.DownloadApproved(decisions); _logger.Info("RSS Sync Completed. Reports found: {0}, Fetches attempted: {1}", reports.Count, qualifiedReports.Count()); }