2013-04-15 01:41:39 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2013-03-07 00:19:49 +00:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
using NzbDrone.Core.Model;
|
2013-04-15 01:41:39 +00:00
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
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-03-07 00:19:49 +00:00
|
|
|
|
2013-04-01 06:22:16 +00:00
|
|
|
public NotInQueueSpecification(IProvideDownloadClient downloadClientProvider)
|
2013-03-07 00:19:49 +00:00
|
|
|
{
|
2013-04-01 06:22:16 +00:00
|
|
|
_downloadClientProvider = downloadClientProvider;
|
2013-03-07 00:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string RejectionReason
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return "Already in download queue.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
public virtual bool IsSatisfiedBy(RemoteEpisode subject)
|
2013-03-07 00:19:49 +00:00
|
|
|
{
|
2013-04-15 01:41:39 +00:00
|
|
|
var downloadClient = _downloadClientProvider.GetDownloadClient();
|
|
|
|
|
|
|
|
var queue = downloadClient.GetQueue().Select(q => Parser.Parser.ParseTitle(q.Title));
|
|
|
|
|
|
|
|
return !IsInQueue(subject, queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool IsInQueue(RemoteEpisode newEpisode, IEnumerable<ParsedEpisodeInfo> queue)
|
|
|
|
{
|
|
|
|
var matchingTitle = queue.Where(q => String.Equals(q.SeriesTitle, newEpisode.Series.CleanTitle, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
|
|
var matchingTitleWithQuality = matchingTitle.Where(q => q.Quality >= newEpisode.Quality);
|
|
|
|
|
|
|
|
if (newEpisode.Series.SeriesType == SeriesTypes.Daily)
|
|
|
|
{
|
|
|
|
return matchingTitleWithQuality.Any(q => q.AirDate.Value.Date == newEpisode.AirDate.Value.Date);
|
|
|
|
}
|
|
|
|
|
|
|
|
var matchingSeason = matchingTitleWithQuality.Where(q => q.SeasonNumber == newEpisode.SeasonNumber);
|
|
|
|
|
|
|
|
if (newEpisode.FullSeason)
|
|
|
|
{
|
|
|
|
return matchingSeason.Any();
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchingSeason.Any(q => q.EpisodeNumbers != null && q.EpisodeNumbers.Any(e => newEpisode.EpisodeNumbers.Contains(e)));
|
2013-03-07 00:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|