Merge pull request #747 from Sonarr/release-pushing

Release pushing
This commit is contained in:
Mark McDowall 2015-08-17 22:20:26 -07:00
commit ac587168cd
5 changed files with 123 additions and 53 deletions

View File

@ -9,8 +9,6 @@ using NzbDrone.Core.Exceptions;
using NzbDrone.Core.IndexerSearch;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Parser.Model;
using Omu.ValueInjecter;
using System.Linq;
using Nancy.ModelBinding;
using NzbDrone.Api.Extensions;
using NzbDrone.Common.Cache;
@ -18,7 +16,7 @@ using HttpStatusCode = System.Net.HttpStatusCode;
namespace NzbDrone.Api.Indexers
{
public class ReleaseModule : NzbDroneRestModule<ReleaseResource>
public class ReleaseModule : ReleaseModuleBase
{
private readonly IFetchAndParseRss _rssFetcherAndParser;
private readonly ISearchForNzb _nzbSearchService;
@ -113,55 +111,10 @@ namespace NzbDrone.Api.Indexers
return MapDecisions(prioritizedDecisions);
}
private List<ReleaseResource> MapDecisions(IEnumerable<DownloadDecision> decisions)
protected override ReleaseResource MapDecision(DownloadDecision decision, int initialWeight)
{
var result = new List<ReleaseResource>();
foreach (var downloadDecision in decisions)
{
_remoteEpisodeCache.Set(downloadDecision.RemoteEpisode.Release.Guid, downloadDecision.RemoteEpisode, TimeSpan.FromMinutes(30));
var release = new ReleaseResource();
release.InjectFrom(downloadDecision.RemoteEpisode.Release);
release.InjectFrom(downloadDecision.RemoteEpisode.ParsedEpisodeInfo);
release.InjectFrom(downloadDecision);
release.Rejections = downloadDecision.Rejections.Select(r => r.Reason).ToList();
release.DownloadAllowed = downloadDecision.RemoteEpisode.DownloadAllowed;
release.ReleaseWeight = result.Count;
if (downloadDecision.RemoteEpisode.Series != null)
{
release.QualityWeight = downloadDecision.RemoteEpisode
.Series
.Profile
.Value
.Items
.FindIndex(v => v.Quality == release.Quality.Quality) * 100;
}
release.QualityWeight += release.Quality.Revision.Real * 10;
release.QualityWeight += release.Quality.Revision.Version;
var torrentRelease = downloadDecision.RemoteEpisode.Release as TorrentInfo;
if (torrentRelease != null)
{
release.Protocol = DownloadProtocol.Torrent;
release.Seeders = torrentRelease.Seeders;
//TODO: move this up the chains
release.Leechers = torrentRelease.Peers - torrentRelease.Seeders;
}
else
{
release.Protocol = DownloadProtocol.Usenet;
}
result.Add(release);
}
return result;
_remoteEpisodeCache.Set(decision.RemoteEpisode.Release.Guid, decision.RemoteEpisode, TimeSpan.FromMinutes(30));
return base.MapDecision(decision, initialWeight);
}
}
}

View File

@ -0,0 +1,67 @@
using System.Collections.Generic;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Parser.Model;
using Omu.ValueInjecter;
using System.Linq;
namespace NzbDrone.Api.Indexers
{
public abstract class ReleaseModuleBase : NzbDroneRestModule<ReleaseResource>
{
protected virtual List<ReleaseResource> MapDecisions(IEnumerable<DownloadDecision> decisions)
{
var result = new List<ReleaseResource>();
foreach (var downloadDecision in decisions)
{
var release = MapDecision(downloadDecision, result.Count);
result.Add(release);
}
return result;
}
protected virtual ReleaseResource MapDecision(DownloadDecision decision, int initialWeight)
{
var release = new ReleaseResource();
release.InjectFrom(decision.RemoteEpisode.Release);
release.InjectFrom(decision.RemoteEpisode.ParsedEpisodeInfo);
release.InjectFrom(decision);
release.Rejections = decision.Rejections.Select(r => r.Reason).ToList();
release.DownloadAllowed = decision.RemoteEpisode.DownloadAllowed;
release.ReleaseWeight = initialWeight;
if (decision.RemoteEpisode.Series != null)
{
release.QualityWeight = decision.RemoteEpisode
.Series
.Profile
.Value
.Items
.FindIndex(v => v.Quality == release.Quality.Quality) * 100;
}
release.QualityWeight += release.Quality.Revision.Real * 10;
release.QualityWeight += release.Quality.Revision.Version;
var torrentRelease = decision.RemoteEpisode.Release as TorrentInfo;
if (torrentRelease != null)
{
release.Protocol = DownloadProtocol.Torrent;
release.Seeders = torrentRelease.Seeders;
//TODO: move this up the chains
release.Leechers = torrentRelease.Peers - torrentRelease.Seeders;
}
else
{
release.Protocol = DownloadProtocol.Usenet;
}
return release;
}
}
}

View File

@ -0,0 +1,50 @@
using Nancy;
using Nancy.ModelBinding;
using FluentValidation;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Api.Mapping;
using NzbDrone.Api.Extensions;
using NLog;
namespace NzbDrone.Api.Indexers
{
class ReleasePushModule : ReleaseModuleBase
{
private readonly IMakeDownloadDecision _downloadDecisionMaker;
private readonly IProcessDownloadDecisions _downloadDecisionProcessor;
private readonly Logger _logger;
public ReleasePushModule(IMakeDownloadDecision downloadDecisionMaker,
IProcessDownloadDecisions downloadDecisionProcessor,
Logger logger)
{
_downloadDecisionMaker = downloadDecisionMaker;
_downloadDecisionProcessor = downloadDecisionProcessor;
_logger = logger;
Post["/push"] = x => ProcessRelease(this.Bind<ReleaseResource>());
PostValidator.RuleFor(s => s.Title).NotEmpty();
PostValidator.RuleFor(s => s.DownloadUrl).NotEmpty();
PostValidator.RuleFor(s => s.DownloadProtocol).NotEmpty();
PostValidator.RuleFor(s => s.PublishDate).NotEmpty();
}
private Response ProcessRelease(ReleaseResource release)
{
_logger.Info("Release pushed: {0} - {1}", release.Title, release.DownloadUrl);
var info = release.InjectTo<ReleaseInfo>();
info.Guid = "PUSH-" + info.DownloadUrl;
var decisions = _downloadDecisionMaker.GetRssDecision(new List<ReleaseInfo> { info });
var processed = _downloadDecisionProcessor.ProcessDecisions(decisions);
return MapDecisions(decisions).First().AsResponse();
}
}
}

View File

@ -101,6 +101,8 @@
<Compile Include="Extensions\Pipelines\CorsPipeline.cs" />
<Compile Include="Frontend\Mappers\LoginHtmlMapper.cs" />
<Compile Include="Frontend\Mappers\RobotsTxtMapper.cs" />
<Compile Include="Indexers\ReleaseModuleBase.cs" />
<Compile Include="Indexers\ReleasePushModule.cs" />
<Compile Include="Parse\ParseModule.cs" />
<Compile Include="Parse\ParseResource.cs" />
<Compile Include="ManualImport\ManualImportModule.cs" />

View File

@ -2,10 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download.Pending;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.Download
{