2017-10-27 03:21:06 +00:00
|
|
|
using System;
|
2013-10-21 01:30:46 +00:00
|
|
|
using NLog;
|
2013-09-20 07:49:36 +00:00
|
|
|
using NzbDrone.Common.EnsureThat;
|
2015-05-27 20:25:53 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2015-06-27 09:43:17 +00:00
|
|
|
using NzbDrone.Common.Http;
|
2014-07-23 23:43:54 +00:00
|
|
|
using NzbDrone.Common.Instrumentation.Extensions;
|
2015-05-27 20:25:53 +00:00
|
|
|
using NzbDrone.Common.TPL;
|
2018-06-02 01:59:54 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
2017-10-29 04:37:15 +00:00
|
|
|
using NzbDrone.Core.Download.Clients;
|
2015-06-27 09:43:17 +00:00
|
|
|
using NzbDrone.Core.Exceptions;
|
|
|
|
using NzbDrone.Core.Indexers;
|
2013-09-14 06:36:07 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-04-15 01:41:39 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-04-01 06:22:16 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Download
|
|
|
|
{
|
|
|
|
public interface IDownloadService
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
void DownloadReport(RemoteAlbum remoteAlbum);
|
2013-04-01 06:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class DownloadService : IDownloadService
|
|
|
|
{
|
|
|
|
private readonly IProvideDownloadClient _downloadClientProvider;
|
2017-10-27 03:21:06 +00:00
|
|
|
private readonly IDownloadClientStatusService _downloadClientStatusService;
|
2015-06-27 09:43:17 +00:00
|
|
|
private readonly IIndexerStatusService _indexerStatusService;
|
2015-05-27 20:25:53 +00:00
|
|
|
private readonly IRateLimitService _rateLimitService;
|
2013-09-14 06:36:07 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2018-06-02 01:59:54 +00:00
|
|
|
private readonly ISeedConfigProvider _seedConfigProvider;
|
2013-04-01 06:22:16 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-05-19 23:17:32 +00:00
|
|
|
public DownloadService(IProvideDownloadClient downloadClientProvider,
|
2017-10-27 03:21:06 +00:00
|
|
|
IDownloadClientStatusService downloadClientStatusService,
|
|
|
|
IIndexerStatusService indexerStatusService,
|
|
|
|
IRateLimitService rateLimitService,
|
|
|
|
IEventAggregator eventAggregator,
|
2018-06-02 01:59:54 +00:00
|
|
|
ISeedConfigProvider seedConfigProvider,
|
2017-10-27 03:21:06 +00:00
|
|
|
Logger logger)
|
2013-04-01 06:22:16 +00:00
|
|
|
{
|
|
|
|
_downloadClientProvider = downloadClientProvider;
|
2017-10-27 03:21:06 +00:00
|
|
|
_downloadClientStatusService = downloadClientStatusService;
|
2015-06-27 09:43:17 +00:00
|
|
|
_indexerStatusService = indexerStatusService;
|
2015-05-27 20:25:53 +00:00
|
|
|
_rateLimitService = rateLimitService;
|
2013-09-14 06:36:07 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
2018-06-02 01:59:54 +00:00
|
|
|
_seedConfigProvider = seedConfigProvider;
|
2013-04-01 06:22:16 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
public void DownloadReport(RemoteAlbum remoteAlbum)
|
2013-04-01 06:22:16 +00:00
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
Ensure.That(remoteAlbum.Artist, () => remoteAlbum.Artist).IsNotNull();
|
|
|
|
Ensure.That(remoteAlbum.Albums, () => remoteAlbum.Albums).HasItems();
|
2013-09-20 07:49:36 +00:00
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
var downloadTitle = remoteAlbum.Release.Title;
|
|
|
|
var downloadClient = _downloadClientProvider.GetDownloadClient(remoteAlbum.Release.DownloadProtocol);
|
2013-04-01 06:22:16 +00:00
|
|
|
|
2014-02-14 05:31:49 +00:00
|
|
|
if (downloadClient == null)
|
2013-07-31 05:49:41 +00:00
|
|
|
{
|
2017-10-29 04:37:15 +00:00
|
|
|
throw new DownloadClientUnavailableException($"{remoteAlbum.Release.DownloadProtocol} Download client isn't configured yet");
|
2013-07-31 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 01:59:54 +00:00
|
|
|
// Get the seed configuration for this release.
|
|
|
|
remoteAlbum.SeedConfiguration = _seedConfigProvider.GetSeedConfiguration(remoteAlbum);
|
|
|
|
|
2015-05-27 20:25:53 +00:00
|
|
|
// Limit grabs to 2 per second.
|
2017-08-14 02:58:42 +00:00
|
|
|
if (remoteAlbum.Release.DownloadUrl.IsNotNullOrWhiteSpace() && !remoteAlbum.Release.DownloadUrl.StartsWith("magnet:"))
|
2015-05-27 20:25:53 +00:00
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
var url = new HttpUri(remoteAlbum.Release.DownloadUrl);
|
2016-02-29 23:33:28 +00:00
|
|
|
_rateLimitService.WaitAndPulse(url.Host, TimeSpan.FromSeconds(2));
|
2015-05-27 20:25:53 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 09:43:17 +00:00
|
|
|
string downloadClientId;
|
|
|
|
try
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
downloadClientId = downloadClient.Download(remoteAlbum);
|
2017-10-27 03:21:06 +00:00
|
|
|
_downloadClientStatusService.RecordSuccess(downloadClient.Definition.Id);
|
2017-08-14 02:58:42 +00:00
|
|
|
_indexerStatusService.RecordSuccess(remoteAlbum.Release.IndexerId);
|
2015-06-27 09:43:17 +00:00
|
|
|
}
|
2017-11-16 17:32:31 +00:00
|
|
|
catch (ReleaseUnavailableException)
|
|
|
|
{
|
|
|
|
_logger.Trace("Release {0} no longer available on indexer.", remoteAlbum);
|
|
|
|
throw;
|
|
|
|
}
|
2015-06-27 09:43:17 +00:00
|
|
|
catch (ReleaseDownloadException ex)
|
|
|
|
{
|
|
|
|
var http429 = ex.InnerException as TooManyRequestsException;
|
|
|
|
if (http429 != null)
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
_indexerStatusService.RecordFailure(remoteAlbum.Release.IndexerId, http429.RetryAfter);
|
2015-06-27 09:43:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
_indexerStatusService.RecordFailure(remoteAlbum.Release.IndexerId);
|
2015-06-27 09:43:17 +00:00
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
var albumGrabbedEvent = new AlbumGrabbedEvent(remoteAlbum);
|
|
|
|
albumGrabbedEvent.DownloadClient = downloadClient.GetType().Name;
|
2013-10-21 01:30:46 +00:00
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(downloadClientId))
|
2013-10-21 01:30:46 +00:00
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
albumGrabbedEvent.DownloadId = downloadClientId;
|
2013-10-21 01:30:46 +00:00
|
|
|
}
|
2013-04-01 06:22:16 +00:00
|
|
|
|
2015-07-17 06:19:59 +00:00
|
|
|
_logger.ProgressInfo("Report sent to {0}. {1}", downloadClient.Definition.Name, downloadTitle);
|
2017-08-14 02:58:42 +00:00
|
|
|
_eventAggregator.PublishEvent(albumGrabbedEvent);
|
2013-04-01 06:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 03:21:06 +00:00
|
|
|
}
|