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