2013-10-21 01:30:46 +00:00
|
|
|
|
using System;
|
|
|
|
|
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;
|
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
|
|
|
|
|
{
|
2013-08-17 23:27:18 +00:00
|
|
|
|
void DownloadReport(RemoteEpisode remoteEpisode);
|
2013-04-01 06:22:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-01 03:47:04 +00:00
|
|
|
|
|
2013-04-01 06:22:16 +00:00
|
|
|
|
public class DownloadService : IDownloadService
|
|
|
|
|
{
|
|
|
|
|
private readonly IProvideDownloadClient _downloadClientProvider;
|
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;
|
2013-04-01 06:22:16 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-05-19 23:17:32 +00:00
|
|
|
|
public DownloadService(IProvideDownloadClient downloadClientProvider,
|
2015-06-27 09:43:17 +00:00
|
|
|
|
IIndexerStatusService indexerStatusService,
|
2015-05-27 20:25:53 +00:00
|
|
|
|
IRateLimitService rateLimitService,
|
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
|
Logger logger)
|
2013-04-01 06:22:16 +00:00
|
|
|
|
{
|
|
|
|
|
_downloadClientProvider = downloadClientProvider;
|
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;
|
2013-04-01 06:22:16 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-17 23:27:18 +00:00
|
|
|
|
public void DownloadReport(RemoteEpisode remoteEpisode)
|
2013-04-01 06:22:16 +00:00
|
|
|
|
{
|
2013-11-30 23:53:07 +00:00
|
|
|
|
Ensure.That(remoteEpisode.Series, () => remoteEpisode.Series).IsNotNull();
|
|
|
|
|
Ensure.That(remoteEpisode.Episodes, () => remoteEpisode.Episodes).HasItems();
|
2013-09-20 07:49:36 +00:00
|
|
|
|
|
2013-09-13 23:17:58 +00:00
|
|
|
|
var downloadTitle = remoteEpisode.Release.Title;
|
2014-04-19 15:09:22 +00:00
|
|
|
|
var downloadClient = _downloadClientProvider.GetDownloadClient(remoteEpisode.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
|
|
|
|
{
|
2014-04-19 15:09:22 +00:00
|
|
|
|
_logger.Warn("{0} Download client isn't configured yet.", remoteEpisode.Release.DownloadProtocol);
|
2013-08-17 23:27:18 +00:00
|
|
|
|
return;
|
2013-07-31 05:49:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-27 20:25:53 +00:00
|
|
|
|
// Limit grabs to 2 per second.
|
|
|
|
|
if (remoteEpisode.Release.DownloadUrl.IsNotNullOrWhiteSpace() && !remoteEpisode.Release.DownloadUrl.StartsWith("magnet:"))
|
|
|
|
|
{
|
|
|
|
|
var uri = new Uri(remoteEpisode.Release.DownloadUrl);
|
|
|
|
|
_rateLimitService.WaitAndPulse(uri.Host, TimeSpan.FromSeconds(2));
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-27 09:43:17 +00:00
|
|
|
|
string downloadClientId;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
downloadClientId = downloadClient.Download(remoteEpisode);
|
|
|
|
|
_indexerStatusService.RecordSuccess(remoteEpisode.Release.IndexerId);
|
|
|
|
|
}
|
|
|
|
|
catch (ReleaseDownloadException ex)
|
|
|
|
|
{
|
|
|
|
|
var http429 = ex.InnerException as TooManyRequestsException;
|
|
|
|
|
if (http429 != null)
|
|
|
|
|
{
|
|
|
|
|
_indexerStatusService.RecordFailure(remoteEpisode.Release.IndexerId, http429.RetryAfter);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_indexerStatusService.RecordFailure(remoteEpisode.Release.IndexerId);
|
|
|
|
|
}
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 01:30:46 +00:00
|
|
|
|
var episodeGrabbedEvent = new EpisodeGrabbedEvent(remoteEpisode);
|
2014-01-05 19:49:04 +00:00
|
|
|
|
episodeGrabbedEvent.DownloadClient = downloadClient.GetType().Name;
|
2013-10-21 01:30:46 +00:00
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(downloadClientId))
|
|
|
|
|
{
|
2014-12-19 00:26:42 +00:00
|
|
|
|
episodeGrabbedEvent.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);
|
2013-10-21 01:30:46 +00:00
|
|
|
|
_eventAggregator.PublishEvent(episodeGrabbedEvent);
|
2013-04-01 06:22:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|