2011-05-20 05:52:05 +00:00
|
|
|
|
using System;
|
2012-02-07 05:08:07 +00:00
|
|
|
|
using System.Collections.Generic;
|
2012-10-18 00:06:14 +00:00
|
|
|
|
using System.Linq;
|
2011-05-20 05:52:05 +00:00
|
|
|
|
using NLog;
|
2013-02-19 06:01:03 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2011-05-20 05:52:05 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
2012-01-28 23:53:14 +00:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2012-02-07 05:08:07 +00:00
|
|
|
|
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;
|
2011-05-24 04:12:54 +00:00
|
|
|
|
private readonly HistoryProvider _historyProvider;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
private readonly EpisodeService _episodeService;
|
2011-07-28 07:21:49 +00:00
|
|
|
|
private readonly ExternalNotificationProvider _externalNotificationProvider;
|
2012-01-28 23:53:14 +00:00
|
|
|
|
private readonly ConfigProvider _configProvider;
|
|
|
|
|
private readonly BlackholeProvider _blackholeProvider;
|
2012-02-12 09:52:51 +00:00
|
|
|
|
private readonly SignalRProvider _signalRProvider;
|
2012-08-30 00:32:27 +00:00
|
|
|
|
private readonly PneumaticProvider _pneumaticProvider;
|
2013-01-24 07:31:41 +00:00
|
|
|
|
private readonly NzbgetProvider _nzbgetProvider;
|
2011-07-28 07:21:49 +00:00
|
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2011-05-20 05:52:05 +00:00
|
|
|
|
|
2011-07-28 07:21:49 +00:00
|
|
|
|
public DownloadProvider(SabProvider sabProvider, HistoryProvider historyProvider,
|
2013-02-20 02:05:15 +00:00
|
|
|
|
EpisodeService episodeService, ExternalNotificationProvider externalNotificationProvider,
|
2012-02-12 09:52:51 +00:00
|
|
|
|
ConfigProvider configProvider, BlackholeProvider blackholeProvider,
|
2013-01-24 07:31:41 +00:00
|
|
|
|
SignalRProvider signalRProvider, PneumaticProvider pneumaticProvider,
|
|
|
|
|
NzbgetProvider nzbgetProvider)
|
2011-05-20 05:52:05 +00:00
|
|
|
|
{
|
|
|
|
|
_sabProvider = sabProvider;
|
2011-05-24 04:12:54 +00:00
|
|
|
|
_historyProvider = historyProvider;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
_episodeService = episodeService;
|
2011-07-28 07:21:49 +00:00
|
|
|
|
_externalNotificationProvider = externalNotificationProvider;
|
2012-01-28 23:53:14 +00:00
|
|
|
|
_configProvider = configProvider;
|
|
|
|
|
_blackholeProvider = blackholeProvider;
|
2012-02-12 09:52:51 +00:00
|
|
|
|
_signalRProvider = signalRProvider;
|
2012-08-30 00:32:27 +00:00
|
|
|
|
_pneumaticProvider = pneumaticProvider;
|
2013-01-24 07:31:41 +00:00
|
|
|
|
_nzbgetProvider = nzbgetProvider;
|
2011-05-20 05:52:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DownloadProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool DownloadReport(EpisodeParseResult parseResult)
|
|
|
|
|
{
|
2012-02-07 05:08:07 +00:00
|
|
|
|
var downloadTitle = GetDownloadTitle(parseResult);
|
|
|
|
|
var provider = GetActiveDownloadClient();
|
2013-01-24 23:33:32 +00:00
|
|
|
|
var recentEpisode = ContainsRecentEpisode(parseResult);
|
2012-01-28 23:53:14 +00:00
|
|
|
|
|
2012-11-23 02:56:27 +00:00
|
|
|
|
bool success = provider.DownloadNzb(parseResult.NzbUrl, downloadTitle, recentEpisode);
|
2011-05-24 04:12:54 +00:00
|
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
|
if (success)
|
2011-05-24 04:12:54 +00:00
|
|
|
|
{
|
2012-02-07 05:08:07 +00:00
|
|
|
|
logger.Trace("Download added to Queue: {0}", downloadTitle);
|
|
|
|
|
|
2012-10-17 07:39:06 +00:00
|
|
|
|
foreach (var episode in parseResult.Episodes)
|
2011-05-24 04:12:54 +00:00
|
|
|
|
{
|
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,
|
2013-02-20 02:05:15 +00:00
|
|
|
|
EpisodeId = episode.OID,
|
2012-08-30 00:32:27 +00:00
|
|
|
|
SeriesId = episode.SeriesId,
|
|
|
|
|
NzbInfoUrl = parseResult.NzbInfoUrl,
|
|
|
|
|
ReleaseGroup = parseResult.ReleaseGroup,
|
|
|
|
|
};
|
2011-05-24 04:12:54 +00:00
|
|
|
|
|
|
|
|
|
_historyProvider.Add(history);
|
2013-02-20 02:05:15 +00:00
|
|
|
|
_episodeService.MarkEpisodeAsFetched(episode.OID);
|
2012-02-12 09:52:51 +00:00
|
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
|
_signalRProvider.UpdateEpisodeStatus(episode.OID, EpisodeStatusType.Downloading, null);
|
2011-05-24 04:12:54 +00:00
|
|
|
|
}
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
|
|
_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;
|
|
|
|
|
|
2013-01-24 07:31:41 +00:00
|
|
|
|
case DownloadClientType.Nzbget:
|
|
|
|
|
return _nzbgetProvider;
|
|
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
|
default:
|
|
|
|
|
return _sabProvider;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual String GetDownloadTitle(EpisodeParseResult parseResult)
|
|
|
|
|
{
|
2013-01-23 05:08:21 +00:00
|
|
|
|
if(_configProvider.DownloadClientUseSceneName)
|
|
|
|
|
{
|
|
|
|
|
logger.Trace("Using scene name: {0}", parseResult.OriginalString);
|
|
|
|
|
return parseResult.OriginalString;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
|
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);
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
|
|
if (parseResult.Quality.Proper)
|
|
|
|
|
seasonResult += " [Proper]";
|
|
|
|
|
|
|
|
|
|
return seasonResult;
|
2011-05-24 04:12:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
|
if (parseResult.Series.SeriesType == SeriesType.Daily)
|
2012-02-07 05:08:07 +00:00
|
|
|
|
{
|
|
|
|
|
var dailyResult = String.Format("{0} - {1:yyyy-MM-dd} - {2} [{3}]", seriesTitle,
|
2012-10-20 23:07:04 +00:00
|
|
|
|
parseResult.AirDate, parseResult.Episodes.First().Title, parseResult.Quality.Quality);
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
|
|
if (parseResult.Quality.Proper)
|
|
|
|
|
dailyResult += " [Proper]";
|
|
|
|
|
|
|
|
|
|
return dailyResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Show Name - 1x01-1x02 - Episode Name
|
|
|
|
|
//Show Name - 1x01 - Episode Name
|
|
|
|
|
var episodeString = new List<String>();
|
2012-10-18 00:06:14 +00:00
|
|
|
|
var episodeNames = new List<String>();
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
2012-10-17 07:39:06 +00:00
|
|
|
|
foreach (var episode in parseResult.Episodes)
|
2012-02-07 05:08:07 +00:00
|
|
|
|
{
|
2012-10-18 00:06:14 +00:00
|
|
|
|
episodeString.Add(String.Format("{0}x{1:00}", episode.SeasonNumber, episode.EpisodeNumber));
|
|
|
|
|
episodeNames.Add(Parser.CleanupEpisodeTitle(episode.Title));
|
2012-02-07 05:08:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var epNumberString = String.Join("-", episodeString);
|
2012-10-18 00:06:14 +00:00
|
|
|
|
string episodeName;
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
2012-10-18 00:06:14 +00:00
|
|
|
|
|
|
|
|
|
if (episodeNames.Distinct().Count() == 1)
|
|
|
|
|
episodeName = episodeNames.First();
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
episodeName = String.Join(" + ", episodeNames.Distinct());
|
|
|
|
|
|
2012-10-20 23:07:04 +00:00
|
|
|
|
var result = String.Format("{0} - {1} - {2} [{3}]", seriesTitle, epNumberString, episodeName, parseResult.Quality.Quality);
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
|
|
if (parseResult.Quality.Proper)
|
|
|
|
|
{
|
|
|
|
|
result += " [Proper]";
|
|
|
|
|
}
|
2011-07-28 07:21:49 +00:00
|
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
|
return result;
|
2011-05-20 05:52:05 +00:00
|
|
|
|
}
|
2012-11-23 02:56:27 +00:00
|
|
|
|
|
2013-01-24 23:33:32 +00:00
|
|
|
|
public virtual bool ContainsRecentEpisode(EpisodeParseResult parseResult)
|
2012-11-23 02:56:27 +00:00
|
|
|
|
{
|
2013-01-24 23:33:32 +00:00
|
|
|
|
return parseResult.Episodes.Any(e => e.AirDate >= DateTime.Today.AddDays(-7));
|
2012-11-23 02:56:27 +00:00
|
|
|
|
}
|
2011-05-20 05:52:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|