1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Core/Download/DownloadService.cs

64 lines
2.0 KiB
C#
Raw Normal View History

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;
using NzbDrone.Common.Messaging;
2013-04-01 08:22:16 +02:00
using NzbDrone.Core.Configuration;
2013-04-07 09:30:37 +02:00
using NzbDrone.Core.DecisionEngine;
2013-04-01 08:22:16 +02:00
using NzbDrone.Core.Model;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
2013-04-07 09:30:37 +02:00
using NzbDrone.Core.Tv;
2013-04-01 08:22:16 +02:00
namespace NzbDrone.Core.Download
{
public interface IDownloadService
{
bool DownloadReport(RemoteEpisode episode);
2013-04-01 08:22:16 +02:00
}
public class DownloadService : IDownloadService
{
private readonly IProvideDownloadClient _downloadClientProvider;
private readonly IConfigService _configService;
private readonly IMessageAggregator _messageAggregator;
2013-04-01 08:22:16 +02:00
private readonly Logger _logger;
public DownloadService(IProvideDownloadClient downloadClientProvider, IConfigService configService,
IMessageAggregator messageAggregator, Logger logger)
2013-04-01 08:22:16 +02:00
{
_downloadClientProvider = downloadClientProvider;
_configService = configService;
_messageAggregator = messageAggregator;
2013-04-01 08:22:16 +02:00
_logger = logger;
}
public bool DownloadReport(RemoteEpisode episode)
2013-04-01 08:22:16 +02:00
{
var downloadTitle = episode.Report.Title;
2013-04-01 08:22:16 +02:00
if (!_configService.DownloadClientUseSceneName)
{
downloadTitle = episode.GetDownloadTitle();
2013-04-01 08:22:16 +02:00
}
var provider = _downloadClientProvider.GetDownloadClient();
var recentEpisode = ContainsRecentEpisode(episode);
2013-04-01 08:22:16 +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);
_messageAggregator.Publish(new EpisodeGrabbedEvent(episode));
2013-04-01 08:22:16 +02:00
}
return success;
}
private static bool ContainsRecentEpisode(RemoteEpisode episode)
2013-04-01 08:22:16 +02:00
{
return episode.Episodes.Any(e => e.AirDate >= DateTime.Today.AddDays(-7));
2013-04-01 08:22:16 +02:00
}
}
}