2013-04-15 01:41:39 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2013-07-31 05:49:41 +00:00
|
|
|
using NLog;
|
2013-03-07 00:19:49 +00:00
|
|
|
using NzbDrone.Core.Download;
|
2013-08-07 03:18:05 +00:00
|
|
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
2013-10-09 06:07:09 +00:00
|
|
|
using NzbDrone.Core.Parser;
|
2013-04-15 01:41:39 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
using NzbDrone.Core.Tv;
|
2013-03-07 00:19:49 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.DecisionEngine.Specifications
|
|
|
|
{
|
2013-04-07 07:30:37 +00:00
|
|
|
public class NotInQueueSpecification : IDecisionEngineSpecification
|
2013-03-07 00:19:49 +00:00
|
|
|
{
|
2013-04-01 06:22:16 +00:00
|
|
|
private readonly IProvideDownloadClient _downloadClientProvider;
|
2013-10-09 06:07:09 +00:00
|
|
|
private readonly IParsingService _parsingService;
|
2013-07-31 05:49:41 +00:00
|
|
|
private readonly Logger _logger;
|
2013-03-07 00:19:49 +00:00
|
|
|
|
2013-10-09 06:07:09 +00:00
|
|
|
public NotInQueueSpecification(IProvideDownloadClient downloadClientProvider, IParsingService parsingService, Logger logger)
|
2013-03-07 00:19:49 +00:00
|
|
|
{
|
2013-04-01 06:22:16 +00:00
|
|
|
_downloadClientProvider = downloadClientProvider;
|
2013-10-09 06:07:09 +00:00
|
|
|
_parsingService = parsingService;
|
2013-07-31 05:49:41 +00:00
|
|
|
_logger = logger;
|
2013-03-07 00:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string RejectionReason
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return "Already in download queue.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 03:30:20 +00:00
|
|
|
public bool IsSatisfiedBy(RemoteEpisode subject, SearchCriteriaBase searchCriteria)
|
2013-03-07 00:19:49 +00:00
|
|
|
{
|
2013-04-15 01:41:39 +00:00
|
|
|
var downloadClient = _downloadClientProvider.GetDownloadClient();
|
|
|
|
|
2013-07-31 05:49:41 +00:00
|
|
|
if (!downloadClient.IsConfigured)
|
|
|
|
{
|
|
|
|
_logger.Warn("Download client {0} isn't configured yet.", downloadClient.GetType().Name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-14 18:59:06 +00:00
|
|
|
var queue = downloadClient.GetQueue().Select(queueItem => Parser.Parser.ParseTitle(queueItem.Title)).Where(episodeInfo => episodeInfo != null);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
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-04-15 01:41:39 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 06:07:09 +00:00
|
|
|
public bool IsInQueue(RemoteEpisode newEpisode, IEnumerable<RemoteEpisode> queue)
|
2013-04-15 01:41:39 +00:00
|
|
|
{
|
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-04-15 01:41:39 +00:00
|
|
|
|
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());
|
2013-03-07 00:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|