New: Automatic search for missing episodes if RSS Sync hasn't been run recently

pull/4/head
Mark McDowall 11 years ago
parent 0f75a9008a
commit 192e79d2ff

@ -11,7 +11,12 @@ using NzbDrone.Core.Tv;
namespace NzbDrone.Core.IndexerSearch namespace NzbDrone.Core.IndexerSearch
{ {
public class MissingEpisodeSearchService : IExecute<EpisodeSearchCommand>, IExecute<MissingEpisodeSearchCommand> public interface IEpisodeSearchService
{
void MissingEpisodesAiredAfter(DateTime dateTime);
}
public class MissingEpisodeSearchService : IEpisodeSearchService, IExecute<EpisodeSearchCommand>, IExecute<MissingEpisodeSearchCommand>
{ {
private readonly ISearchForNzb _nzbSearchService; private readonly ISearchForNzb _nzbSearchService;
private readonly IDownloadApprovedReports _downloadApprovedReports; private readonly IDownloadApprovedReports _downloadApprovedReports;
@ -32,6 +37,26 @@ namespace NzbDrone.Core.IndexerSearch
_logger = logger; _logger = logger;
} }
public void MissingEpisodesAiredAfter(DateTime dateTime)
{
var missing = _episodeService.EpisodesBetweenDates(dateTime, DateTime.UtcNow)
.Where(e => !e.HasFile &&
!_queueService.GetQueue().Select(q => q.Episode.Id).Contains(e.Id))
.ToList();
var downloadedCount = 0;
_logger.Info("Searching for {0} missing episodes since last RSS Sync", missing.Count);
foreach (var episode in missing)
{
var decisions = _nzbSearchService.EpisodeSearch(episode);
var downloaded = _downloadApprovedReports.DownloadApproved(decisions);
downloadedCount += downloaded.Count;
}
_logger.ProgressInfo("Completed search for {0} episodes. {1} reports downloaded.", missing.Count, downloadedCount);
}
public void Execute(EpisodeSearchCommand message) public void Execute(EpisodeSearchCommand message)
{ {
foreach (var episodeId in message.EpisodeIds) foreach (var episodeId in message.EpisodeIds)
@ -57,9 +82,9 @@ namespace NzbDrone.Core.IndexerSearch
FilterExpression = v => v.Monitored == true && v.Series.Monitored == true FilterExpression = v => v.Monitored == true && v.Series.Monitored == true
}).Records.ToList(); }).Records.ToList();
var missing = episodes.Where(e => !_queueService.GetQueue().Select(q => q.Episode.Id).Contains(e.Id)); var missing = episodes.Where(e => !_queueService.GetQueue().Select(q => q.Episode.Id).Contains(e.Id)).ToList();
_logger.ProgressInfo("Performing missing search for {0} episodes", episodes.Count); _logger.ProgressInfo("Performing missing search for {0} episodes", missing.Count);
var downloadedCount = 0; var downloadedCount = 0;
//Limit requests to indexers at 100 per minute //Limit requests to indexers at 100 per minute
@ -71,12 +96,10 @@ namespace NzbDrone.Core.IndexerSearch
var decisions = _nzbSearchService.EpisodeSearch(episode); var decisions = _nzbSearchService.EpisodeSearch(episode);
var downloaded = _downloadApprovedReports.DownloadApproved(decisions); var downloaded = _downloadApprovedReports.DownloadApproved(decisions);
downloadedCount += downloaded.Count; downloadedCount += downloaded.Count;
_logger.ProgressInfo("Episode search completed. {0} reports downloaded.", downloaded.Count);
} }
} }
_logger.ProgressInfo("Completed missing search for {0} episodes. {1} reports downloaded.", episodes.Count, downloadedCount); _logger.ProgressInfo("Completed missing search for {0} episodes. {1} reports downloaded.", missing.Count, downloadedCount);
} }
} }
} }

@ -1,7 +1,9 @@
using System.Linq; using System;
using System.Linq;
using NLog; using NLog;
using NzbDrone.Core.DecisionEngine; using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.IndexerSearch;
using NzbDrone.Core.Instrumentation.Extensions; using NzbDrone.Core.Instrumentation.Extensions;
using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Commands;
@ -17,16 +19,19 @@ namespace NzbDrone.Core.Indexers
private readonly IFetchAndParseRss _rssFetcherAndParser; private readonly IFetchAndParseRss _rssFetcherAndParser;
private readonly IMakeDownloadDecision _downloadDecisionMaker; private readonly IMakeDownloadDecision _downloadDecisionMaker;
private readonly IDownloadApprovedReports _downloadApprovedReports; private readonly IDownloadApprovedReports _downloadApprovedReports;
private readonly IEpisodeSearchService _episodeSearchService;
private readonly Logger _logger; private readonly Logger _logger;
public RssSyncService(IFetchAndParseRss rssFetcherAndParser, public RssSyncService(IFetchAndParseRss rssFetcherAndParser,
IMakeDownloadDecision downloadDecisionMaker, IMakeDownloadDecision downloadDecisionMaker,
IDownloadApprovedReports downloadApprovedReports, IDownloadApprovedReports downloadApprovedReports,
IEpisodeSearchService episodeSearchService,
Logger logger) Logger logger)
{ {
_rssFetcherAndParser = rssFetcherAndParser; _rssFetcherAndParser = rssFetcherAndParser;
_downloadDecisionMaker = downloadDecisionMaker; _downloadDecisionMaker = downloadDecisionMaker;
_downloadApprovedReports = downloadApprovedReports; _downloadApprovedReports = downloadApprovedReports;
_episodeSearchService = episodeSearchService;
_logger = logger; _logger = logger;
} }
@ -45,6 +50,12 @@ namespace NzbDrone.Core.Indexers
public void Execute(RssSyncCommand message) public void Execute(RssSyncCommand message)
{ {
Sync(); Sync();
if (message.LastExecutionTime.HasValue && DateTime.UtcNow.Subtract(message.LastExecutionTime.Value).TotalHours > 3)
{
_logger.Info("RSS Sync hasn't run since: {0}. Searching for any missing episodes since then.", message.LastExecutionTime.Value);
_episodeSearchService.MissingEpisodesAiredAfter(message.LastExecutionTime.Value.AddDays(-1));
}
} }
} }
} }

@ -43,7 +43,7 @@ namespace NzbDrone.Core.Jobs
try try
{ {
_commandExecutor.PublishCommand(task.TypeName); _commandExecutor.PublishCommand(task.TypeName, task.LastExecution);
} }
catch (Exception e) catch (Exception e)
{ {

@ -35,6 +35,7 @@ namespace NzbDrone.Core.Messaging.Commands
public string Message { get; private set; } public string Message { get; private set; }
public string Name { get; private set; } public string Name { get; private set; }
public DateTime? LastExecutionTime { get; set; }
protected Command() protected Command()
{ {

@ -49,8 +49,15 @@ namespace NzbDrone.Core.Messaging.Commands
} }
public void PublishCommand(string commandTypeName) public void PublishCommand(string commandTypeName)
{
PublishCommand(commandTypeName, null);
}
public void PublishCommand(string commandTypeName, DateTime? lastExecutionTime)
{ {
dynamic command = GetCommand(commandTypeName); dynamic command = GetCommand(commandTypeName);
command.LastExecutionTime = lastExecutionTime;
PublishCommand(command); PublishCommand(command);
} }

@ -1,9 +1,12 @@
using System;
namespace NzbDrone.Core.Messaging.Commands namespace NzbDrone.Core.Messaging.Commands
{ {
public interface ICommandExecutor public interface ICommandExecutor
{ {
void PublishCommand<TCommand>(TCommand command) where TCommand : Command; void PublishCommand<TCommand>(TCommand command) where TCommand : Command;
void PublishCommand(string commandTypeName); void PublishCommand(string commandTypeName);
void PublishCommand(string commandTypeName, DateTime? lastEecutionTime);
Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command; Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command;
Command PublishCommandAsync(string commandTypeName); Command PublishCommandAsync(string commandTypeName);
} }

Loading…
Cancel
Save