2011-04-22 02:23:31 +00:00
|
|
|
using System;
|
2011-04-24 22:32:08 +00:00
|
|
|
using System.Collections.Generic;
|
2011-04-23 20:53:13 +00:00
|
|
|
using System.IO;
|
2011-04-25 20:21:52 +00:00
|
|
|
using System.Net;
|
2011-04-21 01:29:41 +00:00
|
|
|
using System.ServiceModel.Syndication;
|
2011-04-29 06:06:13 +00:00
|
|
|
using System.Linq;
|
2011-04-04 03:50:12 +00:00
|
|
|
using NLog;
|
2011-04-26 05:54:12 +00:00
|
|
|
using NzbDrone.Core.Helpers;
|
2011-04-04 03:50:12 +00:00
|
|
|
using NzbDrone.Core.Model;
|
2011-04-04 07:21:07 +00:00
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-04-29 06:06:13 +00:00
|
|
|
using NzbDrone.Core.Providers.ExternalNotification;
|
2011-04-19 00:12:06 +00:00
|
|
|
using NzbDrone.Core.Repository;
|
2011-04-04 03:50:12 +00:00
|
|
|
|
2011-04-19 00:12:06 +00:00
|
|
|
namespace NzbDrone.Core.Providers.Indexer
|
2011-04-04 03:50:12 +00:00
|
|
|
{
|
2011-04-19 00:12:06 +00:00
|
|
|
public abstract class IndexerProviderBase
|
2011-04-04 03:50:12 +00:00
|
|
|
{
|
2011-04-22 06:23:29 +00:00
|
|
|
protected readonly Logger _logger;
|
2011-04-10 01:34:36 +00:00
|
|
|
protected readonly ConfigProvider _configProvider;
|
2011-04-10 02:44:01 +00:00
|
|
|
protected readonly EpisodeProvider _episodeProvider;
|
2011-04-07 02:25:52 +00:00
|
|
|
private readonly HttpProvider _httpProvider;
|
2011-04-19 00:12:06 +00:00
|
|
|
private readonly IndexerProvider _indexerProvider;
|
2011-04-22 02:23:31 +00:00
|
|
|
private readonly HistoryProvider _historyProvider;
|
2011-04-10 02:44:01 +00:00
|
|
|
protected readonly SeasonProvider _seasonProvider;
|
|
|
|
protected readonly SeriesProvider _seriesProvider;
|
2011-04-24 22:32:08 +00:00
|
|
|
protected readonly SabProvider _sabProvider;
|
2011-04-29 06:06:13 +00:00
|
|
|
protected readonly IEnumerable<ExternalNotificationProviderBase> _externalNotificationProvider;
|
2011-04-19 00:12:06 +00:00
|
|
|
|
2011-04-22 02:23:31 +00:00
|
|
|
protected IndexerProviderBase(SeriesProvider seriesProvider, SeasonProvider seasonProvider,
|
2011-04-10 02:44:01 +00:00
|
|
|
EpisodeProvider episodeProvider, ConfigProvider configProvider,
|
2011-04-24 22:32:08 +00:00
|
|
|
HttpProvider httpProvider, IndexerProvider indexerProvider,
|
2011-04-29 06:06:13 +00:00
|
|
|
HistoryProvider historyProvider, SabProvider sabProvider,
|
|
|
|
IEnumerable<ExternalNotificationProviderBase> externalNotificationProvider)
|
2011-04-04 06:53:22 +00:00
|
|
|
{
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
_seasonProvider = seasonProvider;
|
|
|
|
_episodeProvider = episodeProvider;
|
2011-04-04 07:21:07 +00:00
|
|
|
_configProvider = configProvider;
|
2011-04-05 05:30:13 +00:00
|
|
|
_httpProvider = httpProvider;
|
2011-04-19 00:12:06 +00:00
|
|
|
_indexerProvider = indexerProvider;
|
2011-04-22 02:23:31 +00:00
|
|
|
_historyProvider = historyProvider;
|
2011-04-24 22:32:08 +00:00
|
|
|
_sabProvider = sabProvider;
|
2011-05-01 08:04:44 +00:00
|
|
|
|
|
|
|
//Todo: IEnumerable yields no results for some reason, yet yields results in other classes
|
2011-04-29 06:06:13 +00:00
|
|
|
_externalNotificationProvider = externalNotificationProvider;
|
2011-04-29 06:32:51 +00:00
|
|
|
_logger = LogManager.GetLogger(GetType().ToString());
|
2011-04-04 06:53:22 +00:00
|
|
|
}
|
|
|
|
|
2011-04-04 03:50:12 +00:00
|
|
|
/// <summary>
|
2011-04-19 00:12:06 +00:00
|
|
|
/// Gets the name for the feed
|
2011-04-04 03:50:12 +00:00
|
|
|
/// </summary>
|
2011-04-19 00:12:06 +00:00
|
|
|
public abstract string Name { get; }
|
2011-04-04 03:50:12 +00:00
|
|
|
|
2011-05-01 08:04:44 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a bool to determine if Backlog Searching is Supported
|
|
|
|
/// </summary>
|
|
|
|
public abstract bool SupportsBacklog { get; }
|
|
|
|
|
2011-04-04 06:53:22 +00:00
|
|
|
/// <summary>
|
2011-04-21 01:26:13 +00:00
|
|
|
/// Gets the source URL for the feed
|
2011-04-04 06:53:22 +00:00
|
|
|
/// </summary>
|
2011-04-21 01:26:13 +00:00
|
|
|
protected abstract string[] Urls { get; }
|
2011-04-04 06:53:22 +00:00
|
|
|
|
2011-04-25 20:21:52 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the credential.
|
|
|
|
/// </summary>
|
|
|
|
protected virtual NetworkCredential Credentials
|
|
|
|
{
|
|
|
|
get { return null; }
|
|
|
|
}
|
|
|
|
|
2011-04-22 17:09:06 +00:00
|
|
|
public IndexerSetting Settings
|
2011-04-04 06:53:22 +00:00
|
|
|
{
|
2011-04-21 01:26:13 +00:00
|
|
|
get
|
2011-04-04 06:53:22 +00:00
|
|
|
{
|
2011-04-21 01:26:13 +00:00
|
|
|
return _indexerProvider.GetSettings(GetType());
|
2011-04-04 06:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2011-04-10 02:44:01 +00:00
|
|
|
/// Fetches RSS feed and process each news item.
|
2011-04-04 06:53:22 +00:00
|
|
|
/// </summary>
|
2011-04-25 17:48:16 +00:00
|
|
|
public List<Exception> Fetch()
|
2011-04-04 03:50:12 +00:00
|
|
|
{
|
2011-04-25 04:15:23 +00:00
|
|
|
_logger.Debug("Fetching feeds from " + Settings.Name);
|
2011-04-25 17:48:16 +00:00
|
|
|
var exeptions = new List<Exception>();
|
2011-04-04 03:50:12 +00:00
|
|
|
|
2011-04-21 01:26:13 +00:00
|
|
|
foreach (var url in Urls)
|
2011-04-04 03:50:12 +00:00
|
|
|
{
|
2011-04-22 06:23:29 +00:00
|
|
|
try
|
|
|
|
{
|
2011-04-25 04:15:23 +00:00
|
|
|
_logger.Trace("Downloading RSS " + url);
|
2011-04-25 18:16:38 +00:00
|
|
|
|
2011-04-25 20:21:52 +00:00
|
|
|
var reader = new SyndicationFeedXmlReader(_httpProvider.DownloadStream(url, Credentials));
|
2011-04-25 18:16:38 +00:00
|
|
|
var feed = SyndicationFeed.Load(reader).Items;
|
2011-04-04 03:50:12 +00:00
|
|
|
|
2011-04-22 06:23:29 +00:00
|
|
|
foreach (var item in feed)
|
|
|
|
{
|
2011-04-22 20:14:02 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ProcessItem(item);
|
|
|
|
}
|
|
|
|
catch (Exception itemEx)
|
|
|
|
{
|
2011-04-25 17:48:16 +00:00
|
|
|
exeptions.Add(itemEx);
|
2011-04-22 20:14:02 +00:00
|
|
|
_logger.ErrorException("An error occurred while processing feed item", itemEx);
|
|
|
|
}
|
|
|
|
|
2011-04-22 06:23:29 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-22 20:14:02 +00:00
|
|
|
catch (Exception feedEx)
|
2011-04-04 03:50:12 +00:00
|
|
|
{
|
2011-04-25 17:48:16 +00:00
|
|
|
exeptions.Add(feedEx);
|
2011-04-22 20:14:02 +00:00
|
|
|
_logger.ErrorException("An error occurred while processing feed", feedEx);
|
2011-04-04 03:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-22 06:23:29 +00:00
|
|
|
_logger.Info("Finished processing feeds from " + Settings.Name);
|
2011-04-25 17:48:16 +00:00
|
|
|
return exeptions;
|
2011-04-04 03:50:12 +00:00
|
|
|
}
|
|
|
|
|
2011-04-22 19:16:52 +00:00
|
|
|
internal void ProcessItem(SyndicationItem feedItem)
|
2011-04-04 03:50:12 +00:00
|
|
|
{
|
2011-04-22 20:14:02 +00:00
|
|
|
_logger.Debug("Processing RSS feed item " + feedItem.Title.Text);
|
2011-04-04 07:21:07 +00:00
|
|
|
|
2011-04-04 06:53:22 +00:00
|
|
|
var parseResult = ParseFeed(feedItem);
|
2011-04-04 03:50:12 +00:00
|
|
|
|
2011-04-22 19:16:52 +00:00
|
|
|
if (parseResult != null && parseResult.SeriesId != 0)
|
2011-04-04 03:50:12 +00:00
|
|
|
{
|
2011-04-05 06:33:24 +00:00
|
|
|
if (!_seriesProvider.IsMonitored(parseResult.SeriesId))
|
|
|
|
{
|
2011-04-22 19:16:52 +00:00
|
|
|
_logger.Debug("{0} is present in the DB but not tracked. skipping.", parseResult.CleanTitle);
|
2011-04-19 06:37:34 +00:00
|
|
|
return;
|
2011-04-05 06:33:24 +00:00
|
|
|
}
|
2011-04-04 03:50:12 +00:00
|
|
|
|
2011-04-05 06:33:24 +00:00
|
|
|
if (!_seriesProvider.QualityWanted(parseResult.SeriesId, parseResult.Quality))
|
|
|
|
{
|
2011-04-22 06:23:29 +00:00
|
|
|
_logger.Debug("Post doesn't meet the quality requirements [{0}]. skipping.", parseResult.Quality);
|
2011-04-19 06:37:34 +00:00
|
|
|
return;
|
2011-04-05 06:33:24 +00:00
|
|
|
}
|
2011-04-04 03:50:12 +00:00
|
|
|
|
2011-04-05 06:33:24 +00:00
|
|
|
if (_seasonProvider.IsIgnored(parseResult.SeriesId, parseResult.SeasonNumber))
|
|
|
|
{
|
2011-04-22 06:23:29 +00:00
|
|
|
_logger.Debug("Season {0} is currently set to ignore. skipping.", parseResult.SeasonNumber);
|
2011-04-19 06:37:34 +00:00
|
|
|
return;
|
2011-04-05 06:33:24 +00:00
|
|
|
}
|
2011-04-04 03:50:12 +00:00
|
|
|
|
2011-04-26 15:39:03 +00:00
|
|
|
//Todo: How to handle full season files? Currently the episode list is completely empty for these releases
|
|
|
|
//Todo: Should we assume that the release contains all the episodes that belong to this season and add them from the DB?
|
|
|
|
|
2011-04-05 06:33:24 +00:00
|
|
|
if (!_episodeProvider.IsNeeded(parseResult))
|
|
|
|
{
|
2011-04-22 06:23:29 +00:00
|
|
|
_logger.Debug("Episode {0} is not needed. skipping.", parseResult);
|
2011-04-19 06:37:34 +00:00
|
|
|
return;
|
2011-04-05 06:33:24 +00:00
|
|
|
}
|
2011-04-19 06:37:34 +00:00
|
|
|
|
2011-04-22 02:23:31 +00:00
|
|
|
var episodes = _episodeProvider.GetEpisodeByParseResult(parseResult);
|
|
|
|
|
2011-04-24 22:32:08 +00:00
|
|
|
if (InHistory(episodes, parseResult, feedItem))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-04-25 04:15:23 +00:00
|
|
|
|
2011-04-26 06:32:10 +00:00
|
|
|
parseResult.EpisodeTitle = episodes[0].Title;
|
2011-04-26 05:54:12 +00:00
|
|
|
var sabTitle = _sabProvider.GetSabTitle(parseResult);
|
2011-04-24 22:32:08 +00:00
|
|
|
|
2011-05-18 00:19:05 +00:00
|
|
|
if (_sabProvider.IsInQueue(sabTitle))
|
2011-04-26 05:54:12 +00:00
|
|
|
{
|
2011-05-18 00:19:05 +00:00
|
|
|
return;
|
2011-04-26 05:54:12 +00:00
|
|
|
}
|
2011-04-24 22:32:08 +00:00
|
|
|
|
2011-05-18 00:19:05 +00:00
|
|
|
if (!_sabProvider.AddByUrl(NzbDownloadUrl(feedItem), sabTitle))
|
2011-04-26 05:54:12 +00:00
|
|
|
{
|
2011-05-18 00:19:05 +00:00
|
|
|
_logger.Warn("Unable to add item to SAB queue. {0} {1}", NzbDownloadUrl(feedItem), sabTitle);
|
|
|
|
return;
|
2011-04-26 05:54:12 +00:00
|
|
|
}
|
2011-04-24 22:32:08 +00:00
|
|
|
|
2011-04-22 02:23:31 +00:00
|
|
|
foreach (var episode in episodes)
|
|
|
|
{
|
2011-04-24 22:32:08 +00:00
|
|
|
_historyProvider.Add(new History
|
2011-04-22 02:23:31 +00:00
|
|
|
{
|
2011-04-24 22:32:08 +00:00
|
|
|
Date = DateTime.Now,
|
|
|
|
EpisodeId = episode.EpisodeId,
|
|
|
|
IsProper = parseResult.Proper,
|
|
|
|
NzbTitle = feedItem.Title.Text,
|
2011-04-28 00:11:08 +00:00
|
|
|
Quality = parseResult.Quality,
|
2011-05-19 03:58:42 +00:00
|
|
|
Indexer = Name
|
2011-04-24 22:32:08 +00:00
|
|
|
});
|
2011-04-22 02:23:31 +00:00
|
|
|
}
|
2011-04-29 06:06:13 +00:00
|
|
|
|
|
|
|
//Notify!
|
|
|
|
foreach (var notification in _externalNotificationProvider.Where(n => n.Settings.Enabled))
|
|
|
|
{
|
|
|
|
notification.OnGrab(sabTitle);
|
|
|
|
}
|
2011-04-04 06:53:22 +00:00
|
|
|
}
|
2011-04-04 03:50:12 +00:00
|
|
|
}
|
2011-04-19 00:12:06 +00:00
|
|
|
|
2011-04-21 01:26:13 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Parses the RSS feed item and.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name = "item">RSS feed item to parse</param>
|
|
|
|
/// <returns>Detailed episode info</returns>
|
2011-04-25 20:21:52 +00:00
|
|
|
public EpisodeParseResult ParseFeed(SyndicationItem item)
|
2011-04-19 00:12:06 +00:00
|
|
|
{
|
2011-04-21 01:26:13 +00:00
|
|
|
var episodeParseResult = Parser.ParseEpisodeInfo(item.Title.Text);
|
2011-04-25 20:21:52 +00:00
|
|
|
if (episodeParseResult == null) return null;
|
2011-04-21 01:26:13 +00:00
|
|
|
|
2011-04-22 19:16:52 +00:00
|
|
|
var seriesInfo = _seriesProvider.FindSeries(episodeParseResult.CleanTitle);
|
2011-04-21 01:26:13 +00:00
|
|
|
|
2011-04-26 05:54:12 +00:00
|
|
|
if (seriesInfo == null)
|
|
|
|
{
|
|
|
|
var seriesId = SceneNameHelper.FindByName(episodeParseResult.CleanTitle);
|
|
|
|
|
|
|
|
if (seriesId != 0)
|
|
|
|
seriesInfo = _seriesProvider.GetSeries(seriesId);
|
|
|
|
}
|
|
|
|
|
2011-04-21 01:26:13 +00:00
|
|
|
if (seriesInfo != null)
|
2011-04-19 00:12:06 +00:00
|
|
|
{
|
2011-04-21 01:26:13 +00:00
|
|
|
episodeParseResult.SeriesId = seriesInfo.SeriesId;
|
2011-04-23 20:53:13 +00:00
|
|
|
episodeParseResult.FolderName = new DirectoryInfo(seriesInfo.Path).Name; ;
|
|
|
|
|
2011-04-22 19:16:52 +00:00
|
|
|
episodeParseResult.CleanTitle = seriesInfo.Title;
|
2011-04-21 01:26:13 +00:00
|
|
|
return CustomParser(item, episodeParseResult);
|
2011-04-19 00:12:06 +00:00
|
|
|
}
|
|
|
|
|
2011-04-22 19:16:52 +00:00
|
|
|
_logger.Debug("Unable to map {0} to any of series in database", episodeParseResult.CleanTitle);
|
2011-04-25 20:21:52 +00:00
|
|
|
return null;
|
2011-04-21 01:26:13 +00:00
|
|
|
}
|
2011-04-19 00:12:06 +00:00
|
|
|
|
2011-04-21 01:26:13 +00:00
|
|
|
/// <summary>
|
|
|
|
/// This method can be overwritten to provide indexer specific info parsing
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">RSS item that needs to be parsed</param>
|
|
|
|
/// <param name="currentResult">Result of the built in parse function.</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
protected virtual EpisodeParseResult CustomParser(SyndicationItem item, EpisodeParseResult currentResult)
|
|
|
|
{
|
|
|
|
return currentResult;
|
|
|
|
}
|
2011-04-19 00:12:06 +00:00
|
|
|
|
2011-04-21 01:26:13 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Generates direct link to download an NZB
|
|
|
|
/// </summary>
|
|
|
|
/// <param name = "item">RSS Feed item to generate the link for</param>
|
|
|
|
/// <returns>Download link URL</returns>
|
|
|
|
protected abstract string NzbDownloadUrl(SyndicationItem item);
|
2011-04-24 22:32:08 +00:00
|
|
|
|
|
|
|
private bool InHistory(IList<Episode> episodes, EpisodeParseResult parseResult, SyndicationItem feedItem)
|
|
|
|
{
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
{
|
|
|
|
if (_historyProvider.Exists(episode.EpisodeId, parseResult.Quality, parseResult.Proper))
|
|
|
|
{
|
|
|
|
_logger.Debug("Episode in history: {0}", feedItem.Title.Text);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2011-04-24 22:53:01 +00:00
|
|
|
return false;
|
2011-04-24 22:32:08 +00:00
|
|
|
}
|
2011-04-04 03:50:12 +00:00
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
}
|