1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-27 10:19:41 +00:00
Radarr/NzbDrone.Core/Providers/DownloadProvider.cs

167 lines
6.3 KiB
C#
Raw Normal View History

2011-05-20 05:52:05 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Ninject;
2011-05-20 05:52:05 +00:00
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.DownloadClients;
2011-05-20 05:52:05 +00:00
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers
{
public class DownloadProvider
{
private readonly SabProvider _sabProvider;
private readonly HistoryProvider _historyProvider;
private readonly EpisodeProvider _episodeProvider;
private readonly ExternalNotificationProvider _externalNotificationProvider;
private readonly ConfigProvider _configProvider;
private readonly BlackholeProvider _blackholeProvider;
private readonly SignalRProvider _signalRProvider;
2012-08-30 00:32:27 +00:00
private readonly PneumaticProvider _pneumaticProvider;
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2011-05-20 05:52:05 +00:00
[Inject]
public DownloadProvider(SabProvider sabProvider, HistoryProvider historyProvider,
EpisodeProvider episodeProvider, ExternalNotificationProvider externalNotificationProvider,
ConfigProvider configProvider, BlackholeProvider blackholeProvider,
2012-08-30 00:32:27 +00:00
SignalRProvider signalRProvider, PneumaticProvider pneumaticProvider)
2011-05-20 05:52:05 +00:00
{
_sabProvider = sabProvider;
_historyProvider = historyProvider;
_episodeProvider = episodeProvider;
_externalNotificationProvider = externalNotificationProvider;
_configProvider = configProvider;
_blackholeProvider = blackholeProvider;
_signalRProvider = signalRProvider;
2012-08-30 00:32:27 +00:00
_pneumaticProvider = pneumaticProvider;
2011-05-20 05:52:05 +00:00
}
public DownloadProvider()
{
}
public virtual bool DownloadReport(EpisodeParseResult parseResult)
{
var downloadTitle = GetDownloadTitle(parseResult);
var provider = GetActiveDownloadClient();
var recentEpisode = CheckIfRecentEpisode(parseResult);
bool success = provider.DownloadNzb(parseResult.NzbUrl, downloadTitle, recentEpisode);
if (success)
{
logger.Trace("Download added to Queue: {0}", downloadTitle);
foreach (var episode in parseResult.Episodes)
{
2012-08-30 00:32:27 +00:00
var history = new History
{
Date = DateTime.Now,
Indexer = parseResult.Indexer,
IsProper = parseResult.Quality.Proper,
2012-10-14 00:36:16 +00:00
Quality = parseResult.Quality.Quality,
2012-08-30 00:32:27 +00:00
NzbTitle = parseResult.OriginalString,
EpisodeId = episode.EpisodeId,
SeriesId = episode.SeriesId,
NzbInfoUrl = parseResult.NzbInfoUrl,
ReleaseGroup = parseResult.ReleaseGroup,
};
_historyProvider.Add(history);
_episodeProvider.MarkEpisodeAsFetched(episode.EpisodeId);
_signalRProvider.UpdateEpisodeStatus(episode.EpisodeId, EpisodeStatusType.Downloading, null);
}
_externalNotificationProvider.OnGrab(downloadTitle);
}
return success;
}
public virtual IDownloadClient GetActiveDownloadClient()
{
switch (_configProvider.DownloadClient)
{
case DownloadClientType.Blackhole:
return _blackholeProvider;
2012-08-30 00:32:27 +00:00
case DownloadClientType.Pneumatic:
return _pneumaticProvider;
default:
return _sabProvider;
}
}
public virtual String GetDownloadTitle(EpisodeParseResult parseResult)
{
var seriesTitle = MediaFileProvider.CleanFilename(parseResult.Series.Title);
//Handle Full Naming
if (parseResult.FullSeason)
{
var seasonResult = String.Format("{0} - Season {1} [{2}]", seriesTitle,
2012-10-14 00:36:16 +00:00
parseResult.SeasonNumber, parseResult.Quality.Quality);
if (parseResult.Quality.Proper)
seasonResult += " [Proper]";
return seasonResult;
}
if (parseResult.Series.IsDaily)
{
var dailyResult = String.Format("{0} - {1:yyyy-MM-dd} - {2} [{3}]", seriesTitle,
parseResult.AirDate, parseResult.Episodes.First().Title, parseResult.Quality.Quality);
if (parseResult.Quality.Proper)
dailyResult += " [Proper]";
return dailyResult;
}
//Show Name - 1x01-1x02 - Episode Name
//Show Name - 1x01 - Episode Name
var episodeString = new List<String>();
var episodeNames = new List<String>();
foreach (var episode in parseResult.Episodes)
{
episodeString.Add(String.Format("{0}x{1:00}", episode.SeasonNumber, episode.EpisodeNumber));
episodeNames.Add(Parser.CleanupEpisodeTitle(episode.Title));
}
var epNumberString = String.Join("-", episodeString);
string episodeName;
if (episodeNames.Distinct().Count() == 1)
episodeName = episodeNames.First();
else
episodeName = String.Join(" + ", episodeNames.Distinct());
var result = String.Format("{0} - {1} - {2} [{3}]", seriesTitle, epNumberString, episodeName, parseResult.Quality.Quality);
if (parseResult.Quality.Proper)
{
result += " [Proper]";
}
return result;
2011-05-20 05:52:05 +00:00
}
public virtual bool CheckIfRecentEpisode(EpisodeParseResult parseResult)
{
return parseResult.Episodes
.Where(e => e.AirDate.HasValue)
.Select(e => e.AirDate)
.OrderBy(e => e.Value)
.First().Value <= DateTime.Today.AddDays(7);
}
2011-05-20 05:52:05 +00:00
}
}