2013-06-07 00:17:57 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-08-26 05:14:55 +00:00
|
|
|
|
using Nancy;
|
2013-09-04 05:01:28 +00:00
|
|
|
|
using NLog;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
using NzbDrone.Api.Mapping;
|
|
|
|
|
using NzbDrone.Core.DecisionEngine;
|
2013-09-11 23:33:34 +00:00
|
|
|
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
2013-06-09 20:50:57 +00:00
|
|
|
|
using NzbDrone.Core.Download;
|
2013-06-07 00:17:57 +00:00
|
|
|
|
using NzbDrone.Core.IndexerSearch;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
using NzbDrone.Core.Indexers;
|
2013-06-09 20:50:57 +00:00
|
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
using Omu.ValueInjecter;
|
|
|
|
|
using System.Linq;
|
2013-08-26 05:14:55 +00:00
|
|
|
|
using Nancy.ModelBinding;
|
|
|
|
|
using NzbDrone.Api.Extensions;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Indexers
|
|
|
|
|
{
|
|
|
|
|
public class ReleaseModule : NzbDroneRestModule<ReleaseResource>
|
|
|
|
|
{
|
|
|
|
|
private readonly IFetchAndParseRss _rssFetcherAndParser;
|
2013-06-07 00:17:57 +00:00
|
|
|
|
private readonly ISearchForNzb _nzbSearchService;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
private readonly IMakeDownloadDecision _downloadDecisionMaker;
|
2013-06-09 20:50:57 +00:00
|
|
|
|
private readonly IDownloadService _downloadService;
|
|
|
|
|
private readonly IParsingService _parsingService;
|
2013-09-04 05:01:28 +00:00
|
|
|
|
private readonly Logger _logger;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
|
2013-06-19 02:08:29 +00:00
|
|
|
|
public ReleaseModule(IFetchAndParseRss rssFetcherAndParser,
|
2013-09-04 05:01:28 +00:00
|
|
|
|
ISearchForNzb nzbSearchService,
|
|
|
|
|
IMakeDownloadDecision downloadDecisionMaker,
|
|
|
|
|
IDownloadService downloadService,
|
|
|
|
|
IParsingService parsingService,
|
|
|
|
|
Logger logger)
|
2013-04-28 19:46:13 +00:00
|
|
|
|
{
|
|
|
|
|
_rssFetcherAndParser = rssFetcherAndParser;
|
2013-06-07 00:17:57 +00:00
|
|
|
|
_nzbSearchService = nzbSearchService;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
_downloadDecisionMaker = downloadDecisionMaker;
|
2013-06-09 20:50:57 +00:00
|
|
|
|
_downloadService = downloadService;
|
|
|
|
|
_parsingService = parsingService;
|
2013-09-04 05:01:28 +00:00
|
|
|
|
_logger = logger;
|
2013-06-07 00:17:57 +00:00
|
|
|
|
GetResourceAll = GetReleases;
|
2013-08-26 05:14:55 +00:00
|
|
|
|
Post["/"] = x=> DownloadRelease(this.Bind<ReleaseResource>());
|
2013-06-09 20:50:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-26 05:14:55 +00:00
|
|
|
|
private Response DownloadRelease(ReleaseResource release)
|
2013-06-09 20:50:57 +00:00
|
|
|
|
{
|
2013-08-19 05:53:26 +00:00
|
|
|
|
var remoteEpisode = _parsingService.Map(release.InjectTo<ParsedEpisodeInfo>(), 0);
|
2013-06-09 20:50:57 +00:00
|
|
|
|
remoteEpisode.Report = release.InjectTo<ReportInfo>();
|
|
|
|
|
|
|
|
|
|
_downloadService.DownloadReport(remoteEpisode);
|
|
|
|
|
|
2013-08-26 05:14:55 +00:00
|
|
|
|
return release.AsResponse();
|
2013-06-07 00:17:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<ReleaseResource> GetReleases()
|
|
|
|
|
{
|
|
|
|
|
if (Request.Query.episodeId != null)
|
|
|
|
|
{
|
|
|
|
|
return GetEpisodeReleases(Request.Query.episodeId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return GetRss();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<ReleaseResource> GetEpisodeReleases(int episodeId)
|
|
|
|
|
{
|
|
|
|
|
var decisions = _nzbSearchService.EpisodeSearch(episodeId);
|
2013-09-04 05:01:28 +00:00
|
|
|
|
|
2013-06-07 00:17:57 +00:00
|
|
|
|
return MapDecisions(decisions);
|
2013-04-28 19:46:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<ReleaseResource> GetRss()
|
|
|
|
|
{
|
2013-06-09 20:50:57 +00:00
|
|
|
|
var reports = _rssFetcherAndParser.Fetch();
|
|
|
|
|
var decisions = _downloadDecisionMaker.GetRssDecision(reports);
|
2013-04-28 19:46:13 +00:00
|
|
|
|
|
2013-06-09 20:50:57 +00:00
|
|
|
|
return MapDecisions(decisions);
|
2013-06-07 00:17:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<ReleaseResource> MapDecisions(IEnumerable<DownloadDecision> decisions)
|
|
|
|
|
{
|
2013-04-28 19:46:13 +00:00
|
|
|
|
var result = new List<ReleaseResource>();
|
|
|
|
|
|
|
|
|
|
foreach (var downloadDecision in decisions)
|
|
|
|
|
{
|
|
|
|
|
var release = new ReleaseResource();
|
|
|
|
|
|
|
|
|
|
release.InjectFrom(downloadDecision.RemoteEpisode.Report);
|
|
|
|
|
release.InjectFrom(downloadDecision.RemoteEpisode.ParsedEpisodeInfo);
|
|
|
|
|
release.InjectFrom(downloadDecision);
|
|
|
|
|
release.Rejections = downloadDecision.Rejections.ToList();
|
|
|
|
|
|
|
|
|
|
result.Add(release);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|