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