Lidarr/src/NzbDrone.Core/Download/DownloadService.cs

70 lines
2.6 KiB
C#
Raw Normal View History

using System;
using NLog;
using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.Extensions;
2014-07-23 23:43:54 +00:00
using NzbDrone.Common.Instrumentation.Extensions;
using NzbDrone.Common.TPL;
using NzbDrone.Core.Messaging.Events;
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;
private readonly IRateLimitService _rateLimitService;
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,
IRateLimitService rateLimitService,
IEventAggregator eventAggregator,
Logger logger)
2013-04-01 06:22:16 +00:00
{
_downloadClientProvider = downloadClientProvider;
_rateLimitService = rateLimitService;
_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-13 23:17:58 +00:00
var downloadTitle = remoteEpisode.Release.Title;
var downloadClient = _downloadClientProvider.GetDownloadClient(remoteEpisode.Release.DownloadProtocol);
2013-04-01 06:22:16 +00:00
if (downloadClient == null)
{
_logger.Warn("{0} Download client isn't configured yet.", remoteEpisode.Release.DownloadProtocol);
2013-08-17 23:27:18 +00:00
return;
}
// 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));
}
var downloadClientId = downloadClient.Download(remoteEpisode);
var episodeGrabbedEvent = new EpisodeGrabbedEvent(remoteEpisode);
2014-01-05 19:49:04 +00:00
episodeGrabbedEvent.DownloadClient = downloadClient.GetType().Name;
if (!String.IsNullOrWhiteSpace(downloadClientId))
{
episodeGrabbedEvent.DownloadId = downloadClientId;
}
2013-04-01 06:22:16 +00:00
2013-09-11 06:33:47 +00:00
_logger.ProgressInfo("Report sent to download client. {0}", downloadTitle);
_eventAggregator.PublishEvent(episodeGrabbedEvent);
2013-04-01 06:22:16 +00:00
}
}
}