1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-31 16:02:29 +01:00
Sonarr/NzbDrone.Core/Providers/Indexer/IndexerProviderBase.cs

189 lines
7.0 KiB
C#
Raw Normal View History

using System;
using System.ServiceModel.Syndication;
2011-04-04 05:50:12 +02:00
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
2011-04-04 05:50:12 +02:00
namespace NzbDrone.Core.Providers.Indexer
2011-04-04 05:50:12 +02:00
{
public abstract class IndexerProviderBase
2011-04-04 05:50:12 +02:00
{
2011-04-22 08:23:29 +02:00
protected readonly Logger _logger;
protected readonly ConfigProvider _configProvider;
2011-04-10 04:44:01 +02:00
protected readonly EpisodeProvider _episodeProvider;
2011-04-07 04:25:52 +02:00
private readonly HttpProvider _httpProvider;
private readonly IndexerProvider _indexerProvider;
private readonly HistoryProvider _historyProvider;
2011-04-10 04:44:01 +02:00
protected readonly SeasonProvider _seasonProvider;
protected readonly SeriesProvider _seriesProvider;
2011-04-04 05:50:12 +02:00
protected IndexerProviderBase(SeriesProvider seriesProvider, SeasonProvider seasonProvider,
2011-04-10 04:44:01 +02:00
EpisodeProvider episodeProvider, ConfigProvider configProvider,
HttpProvider httpProvider, IndexerProvider indexerProvider, HistoryProvider historyProvider)
2011-04-04 08:53:22 +02:00
{
_seriesProvider = seriesProvider;
_seasonProvider = seasonProvider;
_episodeProvider = episodeProvider;
_configProvider = configProvider;
2011-04-05 07:30:13 +02:00
_httpProvider = httpProvider;
_indexerProvider = indexerProvider;
_historyProvider = historyProvider;
2011-04-22 08:23:29 +02:00
_logger = LogManager.GetLogger(GetType().ToString());
2011-04-04 08:53:22 +02:00
}
2011-04-04 05:50:12 +02:00
/// <summary>
/// Gets the name for the feed
2011-04-04 05:50:12 +02:00
/// </summary>
public abstract string Name { get; }
2011-04-04 05:50:12 +02:00
2011-04-04 08:53:22 +02:00
/// <summary>
/// Gets the source URL for the feed
2011-04-04 08:53:22 +02:00
/// </summary>
protected abstract string[] Urls { get; }
2011-04-04 08:53:22 +02:00
public IndexerSetting Settings
2011-04-04 08:53:22 +02:00
{
get
2011-04-04 08:53:22 +02:00
{
return _indexerProvider.GetSettings(GetType());
2011-04-04 08:53:22 +02:00
}
}
/// <summary>
2011-04-10 04:44:01 +02:00
/// Fetches RSS feed and process each news item.
2011-04-04 08:53:22 +02:00
/// </summary>
2011-04-04 05:50:12 +02:00
public void Fetch()
{
2011-04-22 08:23:29 +02:00
_logger.Info("Fetching feeds from " + Settings.Name);
2011-04-04 05:50:12 +02:00
foreach (var url in Urls)
2011-04-04 05:50:12 +02:00
{
2011-04-22 08:23:29 +02:00
try
{
_logger.Debug("Downloading RSS " + url);
var feed = SyndicationFeed.Load(_httpProvider.DownloadXml(url)).Items;
2011-04-04 05:50:12 +02:00
2011-04-22 08:23:29 +02:00
foreach (var item in feed)
{
2011-04-22 22:14:02 +02:00
try
{
ProcessItem(item);
}
catch (Exception itemEx)
{
_logger.ErrorException("An error occurred while processing feed item", itemEx);
}
2011-04-22 08:23:29 +02:00
}
}
2011-04-22 22:14:02 +02:00
catch (Exception feedEx)
2011-04-04 05:50:12 +02:00
{
2011-04-22 22:14:02 +02:00
_logger.ErrorException("An error occurred while processing feed", feedEx);
2011-04-04 05:50:12 +02:00
}
}
2011-04-22 08:23:29 +02:00
_logger.Info("Finished processing feeds from " + Settings.Name);
2011-04-04 05:50:12 +02:00
}
2011-04-22 21:16:52 +02:00
internal void ProcessItem(SyndicationItem feedItem)
2011-04-04 05:50:12 +02:00
{
2011-04-22 22:14:02 +02:00
_logger.Debug("Processing RSS feed item " + feedItem.Title.Text);
2011-04-04 08:53:22 +02:00
var parseResult = ParseFeed(feedItem);
2011-04-04 05:50:12 +02:00
2011-04-22 21:16:52 +02:00
if (parseResult != null && parseResult.SeriesId != 0)
2011-04-04 05:50:12 +02:00
{
if (!_seriesProvider.IsMonitored(parseResult.SeriesId))
{
2011-04-22 21:16:52 +02:00
_logger.Debug("{0} is present in the DB but not tracked. skipping.", parseResult.CleanTitle);
2011-04-19 08:37:34 +02:00
return;
}
2011-04-04 05:50:12 +02:00
if (!_seriesProvider.QualityWanted(parseResult.SeriesId, parseResult.Quality))
{
2011-04-22 08:23:29 +02:00
_logger.Debug("Post doesn't meet the quality requirements [{0}]. skipping.", parseResult.Quality);
2011-04-19 08:37:34 +02:00
return;
}
2011-04-04 05:50:12 +02:00
if (_seasonProvider.IsIgnored(parseResult.SeriesId, parseResult.SeasonNumber))
{
2011-04-22 08:23:29 +02:00
_logger.Debug("Season {0} is currently set to ignore. skipping.", parseResult.SeasonNumber);
2011-04-19 08:37:34 +02:00
return;
}
2011-04-04 05:50:12 +02:00
if (!_episodeProvider.IsNeeded(parseResult))
{
2011-04-22 08:23:29 +02:00
_logger.Debug("Episode {0} is not needed. skipping.", parseResult);
2011-04-19 08:37:34 +02:00
return;
}
2011-04-19 08:37:34 +02:00
var episodes = _episodeProvider.GetEpisodeByParseResult(parseResult);
foreach (var episode in episodes)
{
if (_historyProvider.Exists(episode.EpisodeId, parseResult.Quality, parseResult.Proper))
{
2011-04-22 08:23:29 +02:00
_logger.Debug("Episode in history: {0}", episode.ToString());
continue;
}
//TODO: Add episode to sab
2011-04-22 22:14:02 +02:00
_historyProvider.Add(new History
{
Date = DateTime.Now,
EpisodeId = episode.EpisodeId,
IsProper = parseResult.Proper,
NzbTitle = feedItem.Title.Text,
Quality = parseResult.Quality
});
}
2011-04-04 08:53:22 +02:00
}
2011-04-04 05:50:12 +02:00
}
/// <summary>
/// Parses the RSS feed item and.
/// </summary>
/// <param name = "item">RSS feed item to parse</param>
/// <returns>Detailed episode info</returns>
protected EpisodeParseResult ParseFeed(SyndicationItem item)
{
var episodeParseResult = Parser.ParseEpisodeInfo(item.Title.Text);
if (episodeParseResult == null) return CustomParser(item, null);
2011-04-22 21:16:52 +02:00
var seriesInfo = _seriesProvider.FindSeries(episodeParseResult.CleanTitle);
if (seriesInfo != null)
{
episodeParseResult.SeriesId = seriesInfo.SeriesId;
2011-04-22 21:16:52 +02:00
episodeParseResult.CleanTitle = seriesInfo.Title;
return CustomParser(item, episodeParseResult);
}
2011-04-22 21:16:52 +02:00
_logger.Debug("Unable to map {0} to any of series in database", episodeParseResult.CleanTitle);
return CustomParser(item, episodeParseResult);
}
/// <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;
}
/// <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-04 05:50:12 +02:00
}
2011-04-10 04:44:01 +02:00
}