using System.ServiceModel.Syndication;
using System.Xml;
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers.Core;
namespace NzbDrone.Core.Providers.Feed
{
public abstract class FeedProviderBase
{
protected readonly ISeriesProvider _seriesProvider;
protected readonly ISeasonProvider _seasonProvider;
protected readonly IEpisodeProvider _episodeProvider;
protected readonly IConfigProvider _configProvider;
private readonly HttpProvider _httpProvider;
protected static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public FeedProviderBase(ISeriesProvider seriesProvider, ISeasonProvider seasonProvider,
IEpisodeProvider episodeProvider, IConfigProvider configProvider, HttpProvider httpProvider)
{
_seriesProvider = seriesProvider;
_seasonProvider = seasonProvider;
_episodeProvider = episodeProvider;
_configProvider = configProvider;
_httpProvider = httpProvider;
}
///
/// Gets the source URL for the feed
///
protected abstract string[] URL { get; }
///
/// Gets the name for this feed
///
protected abstract string Name { get; }
///
/// Generates direct link to download an NZB
///
/// RSS Feed item to generate the link for
/// Download link URL
protected abstract string NzbDownloadUrl(SyndicationItem item);
///
/// Parses the RSS feed item and.
///
/// RSS feed item to parse
/// Detailed episode info
protected EpisodeParseResult ParseFeed(SyndicationItem item)
{
var episodeParseResult = Parser.ParseEpisodeInfo(item.Title.Text);
if (episodeParseResult == null) return null;
var seriesInfo = _seriesProvider.FindSeries(episodeParseResult.SeriesTitle);
if (seriesInfo != null)
{
episodeParseResult.SeriesId = seriesInfo.SeriesId;
episodeParseResult.SeriesTitle = seriesInfo.Title;
return episodeParseResult;
}
Logger.Debug("Unable to map {0} to any of series in database", episodeParseResult.SeriesTitle);
return null;
}
///
/// Fetches RSS feed and process each news item.
///
public void Fetch()
{
Logger.Info("Fetching feeds from " + Name);
foreach (var url in URL)
{
Logger.Debug("Downloading RSS " + url);
var feed = SyndicationFeed.Load(_httpProvider.DownloadXml(url)).Items;
foreach (var item in feed)
{
ProcessItem(item);
}
}
Logger.Info("Finished processing feeds from " + Name);
}
private void ProcessItem(SyndicationItem feedItem)
{
Logger.Info("Processing RSS feed item " + feedItem.Title.Text);
var parseResult = ParseFeed(feedItem);
if (parseResult != null)
{
if (!_seriesProvider.IsMonitored(parseResult.SeriesId))
{
Logger.Debug("{0} is present in the DB but not tracked. skipping.", parseResult.SeriesTitle);
}
if (!_seriesProvider.QualityWanted(parseResult.SeriesId, parseResult.Quality))
{
Logger.Debug("Post doesn't meet the quality requirements [{0}]. skipping.", parseResult.Quality);
}
if (_seasonProvider.IsIgnored(parseResult.SeriesId, parseResult.SeasonNumber))
{
Logger.Debug("Season {0} is currently set to ignore. skipping.", parseResult.SeasonNumber);
}
if (!_episodeProvider.IsNeeded(parseResult))
{
Logger.Debug("Episode {0} is not needed. skipping.", parseResult);
}
}
}
}
}