Radarr/src/NzbDrone.Core/DecisionEngine/Specifications/NotInQueueSpecification.cs

61 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.Download;
using NzbDrone.Core.IndexerSearch.Definitions;
2013-10-09 06:07:09 +00:00
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.DecisionEngine.Specifications
{
2013-04-07 07:30:37 +00:00
public class NotInQueueSpecification : IDecisionEngineSpecification
{
2013-04-01 06:22:16 +00:00
private readonly IProvideDownloadClient _downloadClientProvider;
2013-10-09 06:07:09 +00:00
private readonly IParsingService _parsingService;
private readonly Logger _logger;
2013-10-09 06:07:09 +00:00
public NotInQueueSpecification(IProvideDownloadClient downloadClientProvider, IParsingService parsingService, Logger logger)
{
2013-04-01 06:22:16 +00:00
_downloadClientProvider = downloadClientProvider;
2013-10-09 06:07:09 +00:00
_parsingService = parsingService;
_logger = logger;
}
public string RejectionReason
{
get
{
return "Already in download queue.";
}
}
public bool IsSatisfiedBy(RemoteEpisode subject, SearchCriteriaBase searchCriteria)
{
var downloadClient = _downloadClientProvider.GetDownloadClient();
if (!downloadClient.IsConfigured)
{
_logger.Warn("Download client {0} isn't configured yet.", downloadClient.GetType().Name);
return true;
}
var queue = downloadClient.GetQueue().Select(queueItem => Parser.Parser.ParseTitle(queueItem.Title)).Where(episodeInfo => episodeInfo != null);
2013-10-09 06:07:09 +00:00
var mappedQueue = queue.Select(queueItem => _parsingService.Map(queueItem, 0))
.Where(remoteEpisode => remoteEpisode.Series != null);
return !IsInQueue(subject, mappedQueue);
}
2013-10-09 06:07:09 +00:00
public bool IsInQueue(RemoteEpisode newEpisode, IEnumerable<RemoteEpisode> queue)
{
2013-10-09 06:07:09 +00:00
var matchingSeries = queue.Where(q => q.Series.Id == newEpisode.Series.Id);
var matchingTitleWithQuality = matchingSeries.Where(q => q.ParsedEpisodeInfo.Quality >= newEpisode.ParsedEpisodeInfo.Quality);
2013-10-09 06:07:09 +00:00
return matchingTitleWithQuality.Any(q => q.Episodes.Select(e => e.Id).Intersect(newEpisode.Episodes.Select(e => e.Id)).Any());
}
}
}