2017-09-04 02:20:56 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using FluentValidation;
|
|
|
|
using Nancy;
|
|
|
|
using Nancy.ModelBinding;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
using NzbDrone.Core.DecisionEngine;
|
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
using NzbDrone.Core.Exceptions;
|
|
|
|
using NzbDrone.Core.Indexers;
|
|
|
|
using NzbDrone.Core.IndexerSearch;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
using Lidarr.Http.Extensions;
|
|
|
|
using HttpStatusCode = System.Net.HttpStatusCode;
|
|
|
|
|
2017-10-31 01:28:29 +00:00
|
|
|
namespace Lidarr.Api.V1.Indexers
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
public class ReleaseModule : ReleaseModuleBase
|
|
|
|
{
|
|
|
|
private readonly IFetchAndParseRss _rssFetcherAndParser;
|
|
|
|
private readonly ISearchForNzb _nzbSearchService;
|
|
|
|
private readonly IMakeDownloadDecision _downloadDecisionMaker;
|
|
|
|
private readonly IPrioritizeDownloadDecision _prioritizeDownloadDecision;
|
|
|
|
private readonly IDownloadService _downloadService;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
private readonly ICached<RemoteAlbum> _remoteAlbumCache;
|
|
|
|
|
|
|
|
public ReleaseModule(IFetchAndParseRss rssFetcherAndParser,
|
|
|
|
ISearchForNzb nzbSearchService,
|
|
|
|
IMakeDownloadDecision downloadDecisionMaker,
|
|
|
|
IPrioritizeDownloadDecision prioritizeDownloadDecision,
|
|
|
|
IDownloadService downloadService,
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
_rssFetcherAndParser = rssFetcherAndParser;
|
|
|
|
_nzbSearchService = nzbSearchService;
|
|
|
|
_downloadDecisionMaker = downloadDecisionMaker;
|
|
|
|
_prioritizeDownloadDecision = prioritizeDownloadDecision;
|
|
|
|
_downloadService = downloadService;
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
GetResourceAll = GetReleases;
|
|
|
|
Post["/"] = x => DownloadRelease(this.Bind<ReleaseResource>());
|
|
|
|
|
|
|
|
PostValidator.RuleFor(s => s.DownloadAllowed).Equal(true);
|
|
|
|
PostValidator.RuleFor(s => s.Guid).NotEmpty();
|
|
|
|
|
|
|
|
_remoteAlbumCache = cacheManager.GetCache<RemoteAlbum>(GetType(), "remoteAlbums");
|
|
|
|
}
|
|
|
|
|
|
|
|
private Response DownloadRelease(ReleaseResource release)
|
|
|
|
{
|
2017-10-08 04:43:07 +00:00
|
|
|
var remoteAlbum = _remoteAlbumCache.Find(GetCacheKey(release));
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-09-15 03:33:13 +00:00
|
|
|
if (remoteAlbum == null)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
_logger.Debug("Couldn't find requested release in cache, cache timeout probably expired.");
|
|
|
|
|
|
|
|
throw new NzbDroneClientException(HttpStatusCode.NotFound, "Couldn't find requested release in cache, try searching again");
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-09-15 03:33:13 +00:00
|
|
|
_downloadService.DownloadReport(remoteAlbum);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
catch (ReleaseDownloadException ex)
|
|
|
|
{
|
2017-10-08 04:43:07 +00:00
|
|
|
_logger.Error(ex, ex.Message);
|
2017-09-04 02:20:56 +00:00
|
|
|
throw new NzbDroneClientException(HttpStatusCode.Conflict, "Getting release from indexer failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return release.AsResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<ReleaseResource> GetReleases()
|
|
|
|
{
|
2017-09-15 03:33:13 +00:00
|
|
|
if (Request.Query.albumId.HasValue)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2017-09-15 03:33:13 +00:00
|
|
|
return GetAlbumReleases(Request.Query.albumId);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GetRss();
|
|
|
|
}
|
|
|
|
|
2017-09-15 03:33:13 +00:00
|
|
|
private List<ReleaseResource> GetAlbumReleases(int albumId)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-12-02 05:03:12 +00:00
|
|
|
var decisions = _nzbSearchService.AlbumSearch(albumId, true, true, true);
|
2017-09-04 02:20:56 +00:00
|
|
|
var prioritizedDecisions = _prioritizeDownloadDecision.PrioritizeDecisions(decisions);
|
|
|
|
|
|
|
|
return MapDecisions(prioritizedDecisions);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2017-10-08 04:43:07 +00:00
|
|
|
_logger.Error(ex, "Album search failed: " + ex.Message);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new List<ReleaseResource>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<ReleaseResource> GetRss()
|
|
|
|
{
|
|
|
|
var reports = _rssFetcherAndParser.Fetch();
|
|
|
|
var decisions = _downloadDecisionMaker.GetRssDecision(reports);
|
|
|
|
var prioritizedDecisions = _prioritizeDownloadDecision.PrioritizeDecisions(decisions);
|
|
|
|
|
|
|
|
return MapDecisions(prioritizedDecisions);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override ReleaseResource MapDecision(DownloadDecision decision, int initialWeight)
|
|
|
|
{
|
2017-10-08 04:43:07 +00:00
|
|
|
var resource = base.MapDecision(decision, initialWeight);
|
|
|
|
_remoteAlbumCache.Set(GetCacheKey(resource), decision.RemoteAlbum, TimeSpan.FromMinutes(30));
|
|
|
|
return resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetCacheKey(ReleaseResource resource)
|
|
|
|
{
|
|
|
|
return string.Concat(resource.IndexerId, "_", resource.Guid);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|