2011-03-18 15:39:19 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-03-22 03:51:03 +00:00
|
|
|
|
using System.IO;
|
2011-03-18 15:39:19 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Helpers;
|
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class RssItemProcessingProvider : IRssItemProcessingProvider
|
|
|
|
|
{
|
|
|
|
|
private ISeriesProvider _seriesProvider;
|
|
|
|
|
private ISeasonProvider _seasonProvider;
|
|
|
|
|
private IEpisodeProvider _episodeProvider;
|
|
|
|
|
private IHistoryProvider _historyProvider;
|
|
|
|
|
private IDownloadProvider _sabProvider;
|
|
|
|
|
private IConfigProvider _configProvider;
|
2011-03-22 03:51:03 +00:00
|
|
|
|
private IHttpProvider _httpProvider;
|
|
|
|
|
private IDiskProvider _diskProvider;
|
2011-03-18 15:39:19 +00:00
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
public RssItemProcessingProvider(ISeriesProvider seriesProvider, ISeasonProvider seasonProvider,
|
|
|
|
|
IEpisodeProvider episodeProvider, IHistoryProvider historyProvider,
|
2011-03-22 03:51:03 +00:00
|
|
|
|
IDownloadProvider sabProvider, IConfigProvider configProvider,
|
|
|
|
|
IHttpProvider httpProvider, IDiskProvider diskProvider)
|
2011-03-18 15:39:19 +00:00
|
|
|
|
{
|
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
|
_seasonProvider = seasonProvider;
|
|
|
|
|
_episodeProvider = episodeProvider;
|
|
|
|
|
_historyProvider = historyProvider;
|
|
|
|
|
_sabProvider = sabProvider;
|
|
|
|
|
_configProvider = configProvider;
|
2011-03-22 03:51:03 +00:00
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
|
_diskProvider = diskProvider;
|
2011-03-18 15:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IRssItemProcessingProvider Members
|
|
|
|
|
|
2011-03-22 03:51:03 +00:00
|
|
|
|
public bool DownloadIfWanted(NzbInfoModel nzb, Indexer indexer)
|
2011-03-18 15:39:19 +00:00
|
|
|
|
{
|
|
|
|
|
//Do we want this item?
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (nzb.IsPassworded())
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Skipping Passworded Report {0}", nzb.Title);
|
2011-03-22 03:51:03 +00:00
|
|
|
|
return false;
|
2011-03-18 15:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var episodeParseResults = Parser.ParseEpisodeInfo(nzb.Title);
|
|
|
|
|
|
2011-03-23 05:19:23 +00:00
|
|
|
|
if (episodeParseResults.Count() > 0)
|
2011-03-18 15:39:19 +00:00
|
|
|
|
{
|
|
|
|
|
ProcessStandardItem(nzb, indexer, episodeParseResults);
|
2011-03-22 03:51:03 +00:00
|
|
|
|
return false;
|
2011-03-18 15:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-22 03:51:03 +00:00
|
|
|
|
//Todo: Try to handle Season X style naming
|
2011-03-18 15:39:19 +00:00
|
|
|
|
|
|
|
|
|
if (episodeParseResults.Count() < 1)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Unsupported Title: {0}", nzb.Title);
|
2011-03-22 03:51:03 +00:00
|
|
|
|
return false;
|
2011-03-18 15:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2011-03-23 05:19:23 +00:00
|
|
|
|
Logger.Error("Unsupported Title: {0}", nzb.Title);
|
2011-03-18 15:39:19 +00:00
|
|
|
|
Logger.ErrorException("Error Parsing NZB: " + ex.Message, ex);
|
|
|
|
|
}
|
2011-03-22 03:51:03 +00:00
|
|
|
|
return false;
|
2011-03-18 15:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetTitleFix(List<EpisodeParseResult> episodes, int seriesId)
|
|
|
|
|
{
|
|
|
|
|
var series = _seriesProvider.GetSeries(seriesId);
|
|
|
|
|
|
|
|
|
|
int seasonNumber = 0;
|
|
|
|
|
string episodeNumbers = String.Empty;
|
|
|
|
|
string episodeTitles = String.Empty;
|
|
|
|
|
|
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
|
{
|
|
|
|
|
var episodeInDb = _episodeProvider.GetEpisode(seriesId, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
|
|
|
|
|
if (episodeInDb == null)
|
|
|
|
|
{
|
2011-03-23 05:19:23 +00:00
|
|
|
|
//Todo: Handle this some other way?
|
|
|
|
|
Logger.Debug("Episode Not found in Database, Fake it...");
|
|
|
|
|
//return String.Format("{0} - {1:00}x{2}", series.Title, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
episodeInDb = new Episode { EpisodeNumber = episode.EpisodeNumber, Title = "TBA" };
|
2011-03-18 15:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
seasonNumber = episodeInDb.SeasonNumber;
|
|
|
|
|
episodeNumbers = String.Format("{0}x{1:00}", episodeNumbers, episodeInDb.EpisodeNumber);
|
|
|
|
|
episodeTitles = String.Format("{0} + {1}", episodeTitles, episodeInDb.Title);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
episodeTitles = episodeTitles.Trim(' ', '+');
|
|
|
|
|
|
|
|
|
|
return String.Format("{0} - {1}{2} - {3}", series.Title, seasonNumber, episodeNumbers, episodeTitles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private void ProcessStandardItem(NzbInfoModel nzb, Indexer indexer, List<EpisodeParseResult> episodeParseResults)
|
|
|
|
|
{
|
|
|
|
|
//Will try to match via NormalizeTitle, if that fails it will look for a scene name and do a lookup for your shows
|
|
|
|
|
var series = _seriesProvider.FindSeries(episodeParseResults[0].SeriesTitle);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
//If we weren't able to find a title using the clean name, lets try again looking for a scene name
|
2011-03-24 07:19:54 +00:00
|
|
|
|
|
|
|
|
|
var sceneId = SceneNameHelper.FindByName(episodeParseResults[0].SeriesTitle);
|
|
|
|
|
|
|
|
|
|
if (sceneId != 0)
|
|
|
|
|
series = _seriesProvider.GetSeries(sceneId);
|
2011-03-18 15:39:19 +00:00
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Show is not being watched: {0}", episodeParseResults[0].SeriesTitle);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Debug("Show is being watched: {0}", series.Title);
|
|
|
|
|
|
|
|
|
|
nzb.Proper = Parser.ParseProper(nzb.Title);
|
|
|
|
|
nzb.Quality = Parser.ParseQuality(nzb.Title);
|
|
|
|
|
|
|
|
|
|
//Loop through the list of the episodeParseResults to ensure that all the episodes are needed
|
|
|
|
|
foreach (var episode in episodeParseResults)
|
|
|
|
|
{
|
|
|
|
|
//IsEpisodeWanted?
|
|
|
|
|
var episodeModel = new EpisodeModel();
|
|
|
|
|
episodeModel.Proper = nzb.Proper;
|
|
|
|
|
episodeModel.SeriesId = series.SeriesId;
|
|
|
|
|
episodeModel.SeriesTitle = series.Title;
|
|
|
|
|
episodeModel.Quality = nzb.Quality;
|
|
|
|
|
episodeModel.SeasonNumber = episode.SeasonNumber;
|
|
|
|
|
episodeModel.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
|
|
|
|
|
|
if (!_episodeProvider.IsNeeded(episodeModel))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var titleFix = GetTitleFix(new List<EpisodeParseResult> { episode }, episodeModel.SeriesId);
|
|
|
|
|
titleFix = String.Format("{0} [{1}]", titleFix, nzb.Quality); //Add Quality to the titleFix
|
|
|
|
|
|
2011-03-23 05:19:23 +00:00
|
|
|
|
if (!Convert.ToBoolean(_configProvider.GetValue("UseBlackhole", true, true)))
|
2011-03-22 03:51:03 +00:00
|
|
|
|
if (_sabProvider.IsInQueue(titleFix))
|
|
|
|
|
return;
|
2011-03-18 15:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//If their is more than one episode in this NZB check to see if it has been added as a single NZB
|
2011-03-22 03:51:03 +00:00
|
|
|
|
|
|
|
|
|
//Do we want to download the NZB Directly or Send to SABnzbd?
|
|
|
|
|
|
2011-03-24 07:19:54 +00:00
|
|
|
|
nzb.TitleFix = GetTitleFix(episodeParseResults, series.SeriesId); //Get the TitleFix so we can use it later
|
|
|
|
|
nzb.TitleFix = String.Format("{0} [{1}]", nzb.TitleFix, nzb.Quality); //Add Quality to the titleFix
|
|
|
|
|
|
2011-03-22 03:51:03 +00:00
|
|
|
|
if (Convert.ToBoolean(_configProvider.GetValue("UseBlackHole", true, true)))
|
2011-03-18 15:39:19 +00:00
|
|
|
|
{
|
2011-03-22 03:51:03 +00:00
|
|
|
|
var path = _configProvider.GetValue("BlackholeDirectory", String.Empty, true);
|
2011-03-18 15:39:19 +00:00
|
|
|
|
|
2011-03-22 03:51:03 +00:00
|
|
|
|
if (String.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
//Use the NZBDrone root Directory + /NZBs
|
2011-03-23 05:19:23 +00:00
|
|
|
|
//path = CentralDispatch.StartupPath + "NZBs";
|
|
|
|
|
path = @"C:\Test\NZBs";
|
2011-03-22 03:51:03 +00:00
|
|
|
|
}
|
2011-03-18 15:39:19 +00:00
|
|
|
|
|
2011-03-22 03:51:03 +00:00
|
|
|
|
if (_diskProvider.FolderExists(path))
|
2011-03-23 05:19:23 +00:00
|
|
|
|
{
|
|
|
|
|
var filename = path + Path.DirectorySeparatorChar + nzb.TitleFix;
|
|
|
|
|
|
|
|
|
|
if (_httpProvider.DownloadFile(nzb.Link.ToString(), filename))
|
|
|
|
|
AddToHistory(episodeParseResults, series, nzb, indexer);
|
|
|
|
|
}
|
2011-03-22 03:51:03 +00:00
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
Logger.Error("Blackhole Directory doesn't exist, not saving NZB: '{0}'", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Send it to SABnzbd
|
2011-03-18 15:39:19 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2011-03-22 03:51:03 +00:00
|
|
|
|
if (episodeParseResults.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
if (_sabProvider.IsInQueue(nzb.TitleFix))
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (indexer.IndexerName != "Newzbin")
|
2011-03-23 05:19:23 +00:00
|
|
|
|
{
|
|
|
|
|
if (AddByUrl(nzb))
|
|
|
|
|
{
|
|
|
|
|
AddToHistory(episodeParseResults, series, nzb, indexer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2011-03-22 03:51:03 +00:00
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//AddById(episodeParseResults, series, nzb, indexer);
|
|
|
|
|
}
|
2011-03-18 15:39:19 +00:00
|
|
|
|
}
|
2011-03-22 03:51:03 +00:00
|
|
|
|
|
2011-03-18 15:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-23 05:19:23 +00:00
|
|
|
|
private bool AddByUrl(NzbInfoModel nzb)
|
2011-03-18 15:39:19 +00:00
|
|
|
|
{
|
2011-03-23 05:19:23 +00:00
|
|
|
|
return _sabProvider.AddByUrl(nzb.Link.ToString(), nzb.TitleFix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddToHistory(List<EpisodeParseResult> episodeParseResults, Series series, NzbInfoModel nzb, Indexer indexer)
|
|
|
|
|
{
|
|
|
|
|
//We need to loop through the episodeParseResults so each episode in the NZB is properly handled
|
|
|
|
|
foreach (var epr in episodeParseResults)
|
2011-03-18 15:39:19 +00:00
|
|
|
|
{
|
2011-03-23 05:19:23 +00:00
|
|
|
|
var episode = _episodeProvider.GetEpisode(series.SeriesId, epr.SeasonNumber, epr.EpisodeNumber);
|
2011-03-18 15:39:19 +00:00
|
|
|
|
|
2011-03-23 05:19:23 +00:00
|
|
|
|
if (episode == null)
|
|
|
|
|
{
|
|
|
|
|
//Not sure how we got this far, so lets throw an exception
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
}
|
2011-03-18 15:39:19 +00:00
|
|
|
|
|
2011-03-23 05:19:23 +00:00
|
|
|
|
//Set episode status to grabbed
|
|
|
|
|
episode.Status = EpisodeStatusType.Grabbed;
|
2011-03-18 15:39:19 +00:00
|
|
|
|
|
2011-03-23 05:19:23 +00:00
|
|
|
|
//Add to History
|
|
|
|
|
var history = new History();
|
|
|
|
|
history.Date = DateTime.Now;
|
|
|
|
|
history.EpisodeId = episode.EpisodeId;
|
|
|
|
|
history.IndexerId = indexer.IndexerId;
|
|
|
|
|
history.IsProper = nzb.Proper;
|
|
|
|
|
history.Quality = nzb.Quality;
|
|
|
|
|
history.NzbTitle = nzb.Title;
|
2011-03-18 15:39:19 +00:00
|
|
|
|
|
2011-03-23 05:19:23 +00:00
|
|
|
|
_historyProvider.Insert(history);
|
2011-03-18 15:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|