2018-03-14 20:41:36 +00:00
|
|
|
using System;
|
2014-05-19 19:14:41 +00:00
|
|
|
using System.Collections.Generic;
|
2013-09-20 07:49:36 +00:00
|
|
|
using FluentValidation;
|
2013-08-26 05:14:55 +00:00
|
|
|
using Nancy;
|
2019-12-22 22:08:53 +00:00
|
|
|
using Nancy.ModelBinding;
|
2014-05-19 19:14:41 +00:00
|
|
|
using NLog;
|
2019-12-22 22:08:53 +00:00
|
|
|
using NzbDrone.Common.Cache;
|
2013-04-28 19:46:13 +00:00
|
|
|
using NzbDrone.Core.DecisionEngine;
|
2013-06-09 20:50:57 +00:00
|
|
|
using NzbDrone.Core.Download;
|
2014-09-07 11:39:00 +00:00
|
|
|
using NzbDrone.Core.Exceptions;
|
2013-04-28 19:46:13 +00:00
|
|
|
using NzbDrone.Core.Indexers;
|
2019-12-22 22:08:53 +00:00
|
|
|
using NzbDrone.Core.IndexerSearch;
|
2013-06-09 20:50:57 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2014-10-03 23:29:52 +00:00
|
|
|
using HttpStatusCode = System.Net.HttpStatusCode;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Api.Indexers
|
|
|
|
{
|
2015-08-17 05:52:26 +00:00
|
|
|
public class ReleaseModule : ReleaseModuleBase
|
2013-04-28 19:46:13 +00:00
|
|
|
{
|
|
|
|
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;
|
2014-07-15 19:03:53 +00:00
|
|
|
private readonly IPrioritizeDownloadDecision _prioritizeDownloadDecision;
|
2013-06-09 20:50:57 +00:00
|
|
|
private readonly IDownloadService _downloadService;
|
2014-05-19 19:14:41 +00:00
|
|
|
private readonly Logger _logger;
|
2019-12-22 21:24:11 +00:00
|
|
|
|
2017-01-02 18:20:32 +00:00
|
|
|
private readonly ICached<RemoteMovie> _remoteMovieCache;
|
2014-08-06 17:29:34 +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,
|
2014-07-15 19:03:53 +00:00
|
|
|
IPrioritizeDownloadDecision prioritizeDownloadDecision,
|
2013-09-04 05:01:28 +00:00
|
|
|
IDownloadService downloadService,
|
2014-08-06 17:29:34 +00:00
|
|
|
ICacheManager cacheManager,
|
2014-05-19 19:14:41 +00:00
|
|
|
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;
|
2014-07-15 19:03:53 +00:00
|
|
|
_prioritizeDownloadDecision = prioritizeDownloadDecision;
|
2013-06-09 20:50:57 +00:00
|
|
|
_downloadService = downloadService;
|
2014-05-19 19:14:41 +00:00
|
|
|
_logger = logger;
|
2015-04-10 14:30:06 +00:00
|
|
|
|
2013-06-07 00:17:57 +00:00
|
|
|
GetResourceAll = GetReleases;
|
2019-12-22 22:08:53 +00:00
|
|
|
Post("/", x => DownloadRelease(this.Bind<ReleaseResource>()));
|
2013-09-20 07:49:36 +00:00
|
|
|
|
2017-08-09 20:14:01 +00:00
|
|
|
//PostValidator.RuleFor(s => s.DownloadAllowed).Equal(true);
|
2015-04-10 14:30:06 +00:00
|
|
|
PostValidator.RuleFor(s => s.Guid).NotEmpty();
|
2019-12-22 21:24:11 +00:00
|
|
|
|
2017-01-02 18:20:32 +00:00
|
|
|
_remoteMovieCache = cacheManager.GetCache<RemoteMovie>(GetType(), "remoteMovies");
|
2013-06-09 20:50:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 21:43:55 +00:00
|
|
|
private object DownloadRelease(ReleaseResource release)
|
2013-06-09 20:50:57 +00:00
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
var remoteMovie = _remoteMovieCache.Find(release.Guid);
|
2014-12-20 01:21:03 +00:00
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
if (remoteMovie == null)
|
2014-08-06 17:29:34 +00:00
|
|
|
{
|
|
|
|
_logger.Debug("Couldn't find requested release in cache, cache timeout probably expired.");
|
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
return new NotFoundResponse();
|
2014-08-06 17:29:34 +00:00
|
|
|
}
|
2019-12-22 22:08:53 +00:00
|
|
|
|
2014-09-07 11:39:00 +00:00
|
|
|
try
|
|
|
|
{
|
2019-06-14 03:54:25 +00:00
|
|
|
_downloadService.DownloadReport(remoteMovie);
|
2014-09-07 11:39:00 +00:00
|
|
|
}
|
|
|
|
catch (ReleaseDownloadException ex)
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
_logger.Error(ex, ex.Message);
|
2014-10-03 23:29:52 +00:00
|
|
|
throw new NzbDroneClientException(HttpStatusCode.Conflict, "Getting release from indexer failed");
|
2014-09-07 11:39:00 +00:00
|
|
|
}
|
2013-06-09 20:50:57 +00:00
|
|
|
|
2019-08-28 21:43:55 +00:00
|
|
|
return release;
|
2013-06-07 00:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private List<ReleaseResource> GetReleases()
|
|
|
|
{
|
2017-01-02 17:05:55 +00:00
|
|
|
if (Request.Query.movieId != null)
|
|
|
|
{
|
|
|
|
return GetMovieReleases(Request.Query.movieId);
|
|
|
|
}
|
|
|
|
|
2013-06-07 00:17:57 +00:00
|
|
|
return GetRss();
|
|
|
|
}
|
|
|
|
|
2017-01-02 17:05:55 +00:00
|
|
|
private List<ReleaseResource> GetMovieReleases(int movieId)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-07-16 02:41:43 +00:00
|
|
|
var decisions = _nzbSearchService.MovieSearch(movieId, true, true);
|
2017-01-02 17:05:55 +00:00
|
|
|
var prioritizedDecisions = _prioritizeDownloadDecision.PrioritizeDecisionsForMovies(decisions);
|
|
|
|
|
|
|
|
return MapDecisions(prioritizedDecisions);
|
|
|
|
}
|
|
|
|
catch (NotImplementedException ex)
|
|
|
|
{
|
|
|
|
_logger.Error(ex, "One or more indexer you selected does not support movie search yet: " + ex.Message);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Error(ex, "Movie search failed: " + ex.Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new List<ReleaseResource>();
|
|
|
|
}
|
|
|
|
|
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);
|
2018-03-14 20:41:36 +00:00
|
|
|
var prioritizedDecisions = _prioritizeDownloadDecision.PrioritizeDecisionsForMovies(decisions);
|
2013-04-28 19:46:13 +00:00
|
|
|
|
2014-07-15 19:03:53 +00:00
|
|
|
return MapDecisions(prioritizedDecisions);
|
2013-06-07 00:17:57 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 05:52:26 +00:00
|
|
|
protected override ReleaseResource MapDecision(DownloadDecision decision, int initialWeight)
|
2013-06-07 00:17:57 +00:00
|
|
|
{
|
2019-12-22 22:08:53 +00:00
|
|
|
_remoteMovieCache.Set(decision.RemoteMovie.Release.Guid, decision.RemoteMovie, TimeSpan.FromMinutes(30));
|
2018-03-14 20:41:36 +00:00
|
|
|
|
2019-12-22 22:08:53 +00:00
|
|
|
return base.MapDecision(decision, initialWeight);
|
2013-04-28 19:46:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-14 20:41:36 +00:00
|
|
|
}
|