2013-04-01 08:22:16 +02:00
|
|
|
|
using System;
|
2013-04-07 09:30:37 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-04-01 08:22:16 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-04-24 03:56:00 +02:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-04-01 08:22:16 +02:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2013-04-15 03:41:39 +02:00
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-04-01 08:22:16 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Download
|
|
|
|
|
{
|
|
|
|
|
public interface IDownloadService
|
|
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
|
bool DownloadReport(RemoteEpisode episode);
|
2013-04-01 08:22:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DownloadService : IDownloadService
|
|
|
|
|
{
|
|
|
|
|
private readonly IProvideDownloadClient _downloadClientProvider;
|
2013-04-24 03:56:00 +02:00
|
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-04-01 08:22:16 +02:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
|
2013-05-20 01:17:32 +02:00
|
|
|
|
public DownloadService(IProvideDownloadClient downloadClientProvider,
|
2013-04-24 03:56:00 +02:00
|
|
|
|
IMessageAggregator messageAggregator, Logger logger)
|
2013-04-01 08:22:16 +02:00
|
|
|
|
{
|
|
|
|
|
_downloadClientProvider = downloadClientProvider;
|
2013-04-24 03:56:00 +02:00
|
|
|
|
_messageAggregator = messageAggregator;
|
2013-04-01 08:22:16 +02:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
|
public bool DownloadReport(RemoteEpisode episode)
|
2013-04-01 08:22:16 +02:00
|
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
|
var downloadTitle = episode.Report.Title;
|
2013-04-01 08:22:16 +02:00
|
|
|
|
|
|
|
|
|
var provider = _downloadClientProvider.GetDownloadClient();
|
2013-04-15 03:41:39 +02:00
|
|
|
|
var recentEpisode = ContainsRecentEpisode(episode);
|
2013-04-01 08:22:16 +02:00
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
|
bool success = provider.DownloadNzb(episode.Report.NzbUrl, downloadTitle, recentEpisode);
|
2013-04-01 08:22:16 +02:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
2013-04-07 09:30:37 +02:00
|
|
|
|
_logger.Info("Report sent to download client. {0}", downloadTitle);
|
2013-04-27 04:03:34 +02:00
|
|
|
|
_messageAggregator.PublishEvent(new EpisodeGrabbedEvent(episode));
|
2013-04-01 08:22:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
|
private static bool ContainsRecentEpisode(RemoteEpisode episode)
|
2013-04-01 08:22:16 +02:00
|
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
|
return episode.Episodes.Any(e => e.AirDate >= DateTime.Today.AddDays(-7));
|
2013-04-01 08:22:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|