1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-27 02:07:41 +00:00
Sonarr/NzbDrone.Core/Providers/Indexer/IndexerBase.cs

132 lines
4.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Net;
using System.ServiceModel.Syndication;
2011-04-04 03:50:12 +00:00
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
2011-04-04 03:50:12 +00:00
namespace NzbDrone.Core.Providers.Indexer
2011-04-04 03:50:12 +00:00
{
public abstract class IndexerBase
2011-04-04 03:50:12 +00:00
{
2011-04-22 06:23:29 +00:00
protected readonly Logger _logger;
2011-04-07 02:25:52 +00:00
private readonly HttpProvider _httpProvider;
protected readonly ConfigProvider _configProvider;
private readonly IndexerProvider _indexerProvider;
protected IndexerBase(HttpProvider httpProvider, ConfigProvider configProvider, IndexerProvider indexerProvider)
2011-04-04 06:53:22 +00:00
{
2011-04-05 05:30:13 +00:00
_httpProvider = httpProvider;
_configProvider = configProvider;
_indexerProvider = indexerProvider;
_logger = LogManager.GetLogger(GetType().ToString());
2011-04-04 06:53:22 +00:00
}
2011-04-04 03:50:12 +00:00
/// <summary>
/// Gets the name for the feed
2011-04-04 03:50:12 +00:00
/// </summary>
public abstract string Name { get; }
2011-04-04 03:50:12 +00:00
2011-04-04 06:53:22 +00:00
/// <summary>
/// Gets the source URL for the feed
2011-04-04 06:53:22 +00:00
/// </summary>
protected abstract string[] Urls { get; }
2011-04-04 06:53:22 +00:00
/// <summary>
/// Gets the credential.
/// </summary>
protected virtual NetworkCredential Credentials
{
get { return null; }
}
public IndexerSetting Settings
2011-04-04 06:53:22 +00:00
{
get
2011-04-04 06:53:22 +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>
public List<EpisodeParseResult> Fetch()
2011-04-04 03:50:12 +00:00
{
2011-04-25 04:15:23 +00:00
_logger.Debug("Fetching feeds from " + Settings.Name);
var result = new List<EpisodeParseResult>();
2011-04-04 03:50:12 +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
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
{
var parsedEpisode = ParseFeed(item);
if (parsedEpisode != null)
{
parsedEpisode.NzbUrl = NzbDownloadUrl(item);
parsedEpisode.Indexer = Name;
parsedEpisode.NzbTitle = item.Title.Text;
result.Add(parsedEpisode);
}
2011-04-22 20:14:02 +00:00
}
catch (Exception itemEx)
{
_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-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);
return result;
2011-04-04 03:50:12 +00:00
}
/// <summary>
/// Parses the RSS feed item
/// </summary>
/// <param name = "item">RSS feed item to parse</param>
/// <returns>Detailed episode info</returns>
public EpisodeParseResult ParseFeed(SyndicationItem item)
{
var episodeParseResult = Parser.ParseEpisodeInfo(item.Title.Text);
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 03:50:12 +00:00
}
2011-04-10 02:44:01 +00:00
}