2014-07-15 19:03:53 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Collections.Generic;
|
2014-11-24 00:07:46 +00:00
|
|
|
|
using NzbDrone.Core.Indexers;
|
2014-05-13 17:57:46 +00:00
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2014-11-24 00:07:46 +00:00
|
|
|
|
using NzbDrone.Core.Profiles.Delay;
|
2014-07-15 19:03:53 +00:00
|
|
|
|
using NzbDrone.Core.Qualities;
|
2014-11-24 00:07:46 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2014-07-15 19:03:53 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.DecisionEngine
|
|
|
|
|
{
|
|
|
|
|
public interface IPrioritizeDownloadDecision
|
|
|
|
|
{
|
|
|
|
|
List<DownloadDecision> PrioritizeDecisions(List<DownloadDecision> decisions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DownloadDecisionPriorizationService : IPrioritizeDownloadDecision
|
|
|
|
|
{
|
2014-11-24 00:07:46 +00:00
|
|
|
|
private readonly IDelayProfileService _delayProfileService;
|
|
|
|
|
|
|
|
|
|
public DownloadDecisionPriorizationService(IDelayProfileService delayProfileService)
|
|
|
|
|
{
|
|
|
|
|
_delayProfileService = delayProfileService;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-15 19:03:53 +00:00
|
|
|
|
public List<DownloadDecision> PrioritizeDecisions(List<DownloadDecision> decisions)
|
|
|
|
|
{
|
2014-11-24 00:07:46 +00:00
|
|
|
|
return decisions.Where(c => c.RemoteEpisode.Series != null)
|
|
|
|
|
.GroupBy(c => c.RemoteEpisode.Series.Id, (seriesId, d) =>
|
|
|
|
|
{
|
|
|
|
|
var downloadDecisions = d.ToList();
|
|
|
|
|
var series = downloadDecisions.First().RemoteEpisode.Series;
|
|
|
|
|
|
|
|
|
|
return downloadDecisions
|
|
|
|
|
.OrderByDescending(c => c.RemoteEpisode.ParsedEpisodeInfo.Quality, new QualityModelComparer(series.Profile))
|
|
|
|
|
.ThenBy(c => c.RemoteEpisode.Episodes.Select(e => e.EpisodeNumber).MinOrDefault())
|
|
|
|
|
.ThenBy(c => PrioritizeDownloadProtocol(series, c.RemoteEpisode.Release.DownloadProtocol))
|
|
|
|
|
.ThenBy(c => c.RemoteEpisode.Release.Size.Round(200.Megabytes()) / Math.Max(1, c.RemoteEpisode.Episodes.Count))
|
|
|
|
|
.ThenByDescending(c => TorrentInfo.GetSeeders(c.RemoteEpisode.Release))
|
|
|
|
|
.ThenBy(c => c.RemoteEpisode.Release.Age);
|
|
|
|
|
})
|
|
|
|
|
.SelectMany(c => c)
|
|
|
|
|
.Union(decisions.Where(c => c.RemoteEpisode.Series == null))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int PrioritizeDownloadProtocol(Series series, DownloadProtocol downloadProtocol)
|
|
|
|
|
{
|
|
|
|
|
var delayProfile = _delayProfileService.BestForTags(series.Tags);
|
|
|
|
|
|
|
|
|
|
if (downloadProtocol == delayProfile.PreferredProtocol)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
2014-07-15 19:03:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|