2013-06-07 00:17:57 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
using NzbDrone.Api.Mapping;
|
|
|
|
|
using NzbDrone.Core.DecisionEngine;
|
2013-06-07 00:17:57 +00:00
|
|
|
|
using NzbDrone.Core.IndexerSearch;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
using NzbDrone.Core.Indexers;
|
|
|
|
|
using Omu.ValueInjecter;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
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-07 23:54:46 +00:00
|
|
|
|
private static List<DownloadDecision> results;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
|
2013-06-07 00:17:57 +00:00
|
|
|
|
public ReleaseModule(IFetchAndParseRss rssFetcherAndParser, ISearchForNzb nzbSearchService, IMakeDownloadDecision downloadDecisionMaker)
|
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-07 00:17:57 +00:00
|
|
|
|
GetResourceAll = GetReleases;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
return MapDecisions(decisions);
|
2013-04-28 19:46:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<ReleaseResource> GetRss()
|
|
|
|
|
{
|
2013-06-08 17:53:26 +00:00
|
|
|
|
//if (results == null)
|
2013-06-07 23:54:46 +00:00
|
|
|
|
{
|
|
|
|
|
var reports = _rssFetcherAndParser.Fetch();
|
|
|
|
|
var decisions = _downloadDecisionMaker.GetRssDecision(reports);
|
|
|
|
|
results = decisions;
|
|
|
|
|
}
|
2013-04-28 19:46:13 +00:00
|
|
|
|
|
2013-06-07 23:54:46 +00:00
|
|
|
|
return MapDecisions(results);
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|