1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 08:22:35 +01:00
Sonarr/NzbDrone.Core/Indexers/IndexerBase.cs

238 lines
8.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text.RegularExpressions;
2011-04-04 05:50:12 +02:00
using NLog;
2012-02-11 01:48:20 +01:00
using NzbDrone.Common;
2013-02-24 07:48:52 +01:00
using NzbDrone.Core.Configuration;
2011-04-04 05:50:12 +02:00
using NzbDrone.Core.Model;
2013-02-23 05:37:23 +01:00
namespace NzbDrone.Core.Indexers
2011-04-04 05:50:12 +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;
2012-12-21 09:35:20 +01:00
protected readonly HttpProvider _httpProvider;
2013-02-24 07:48:52 +01:00
protected readonly IConfigService _configService;
protected static readonly Regex TitleSearchRegex = new Regex(@"[\W]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
protected static readonly Regex RemoveThe = new Regex(@"^the\s", RegexOptions.IgnoreCase | RegexOptions.Compiled);
2013-02-24 07:48:52 +01:00
protected IndexerBase(HttpProvider httpProvider, IConfigService configService)
2011-04-04 08:53:22 +02:00
{
2011-04-05 07:30:13 +02:00
_httpProvider = httpProvider;
2013-02-24 07:48:52 +01:00
_configService = configService;
_logger = LogManager.GetLogger(GetType().ToString());
2011-04-04 08:53:22 +02:00
}
2011-05-27 05:54:28 +02:00
public IndexerBase()
{
2011-05-27 08:03:57 +02:00
2011-05-27 05:54:28 +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 abstract bool IsConfigured { get; }
/// <summary>
/// Should the indexer be enabled by default?
/// </summary>
public virtual bool EnabledByDefault
{
get { return false; }
}
/// <summary>
/// Gets the credential.
/// </summary>
protected virtual NetworkCredential Credentials
{
get { return null; }
}
2013-03-27 04:44:52 +01:00
protected abstract IEnumerable<string> GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber);
protected abstract IEnumerable<string> GetDailyEpisodeSearchUrls(string seriesTitle, DateTime date);
protected abstract IEnumerable<string> GetSeasonSearchUrls(string seriesTitle, int seasonNumber);
protected abstract IEnumerable<string> GetPartialSeasonSearchUrls(string seriesTitle, int seasonNumber, int episodeWildcard);
protected virtual EpisodeParseResult CustomParser(SyndicationItem item, EpisodeParseResult currentResult)
{
return currentResult;
}
protected virtual string TitlePreParser(SyndicationItem item)
{
return item.Title.Text;
}
protected abstract string NzbDownloadUrl(SyndicationItem item);
2011-04-04 08:53:22 +02:00
2012-05-02 21:02:39 +02:00
protected abstract string NzbInfoUrl(SyndicationItem item);
2011-05-27 05:54:28 +02:00
public virtual IList<EpisodeParseResult> FetchRss()
2011-04-04 05:50:12 +02:00
{
_logger.Debug("Fetching feeds from " + Name);
var result = new List<EpisodeParseResult>();
2011-04-04 05:50:12 +02:00
result = Fetch(Urls);
2012-02-22 05:43:19 +01:00
_logger.Debug("Finished processing feeds from " + Name);
return result;
}
public virtual IList<EpisodeParseResult> FetchSeason(string seriesTitle, int seasonNumber)
{
_logger.Debug("Searching {0} for {1} Season {2}", Name, seriesTitle, seasonNumber);
var searchUrls = GetSeasonSearchUrls(GetQueryTitle(seriesTitle), seasonNumber);
var result = Fetch(searchUrls);
_logger.Info("Finished searching {0} for {1} Season {2}, Found {3}", Name, seriesTitle, seasonNumber, result.Count);
return result;
}
public virtual IList<EpisodeParseResult> FetchPartialSeason(string seriesTitle, int seasonNumber, int episodePrefix)
{
_logger.Debug("Searching {0} for {1} Season {2}, Prefix: {3}", Name, seriesTitle, seasonNumber, episodePrefix);
var searchUrls = GetPartialSeasonSearchUrls(GetQueryTitle(seriesTitle), seasonNumber, episodePrefix);
var result = Fetch(searchUrls);
_logger.Info("Finished searching {0} for {1} Season {2}, Found {3}", Name, seriesTitle, seasonNumber, result.Count);
return result;
}
2011-05-27 05:54:28 +02:00
public virtual IList<EpisodeParseResult> FetchEpisode(string seriesTitle, int seasonNumber, int episodeNumber)
{
2011-05-27 08:03:57 +02:00
_logger.Debug("Searching {0} for {1}-S{2:00}E{3:00}", Name, seriesTitle, seasonNumber, episodeNumber);
var searchUrls = GetEpisodeSearchUrls(GetQueryTitle(seriesTitle), seasonNumber, episodeNumber);
var result = Fetch(searchUrls);
_logger.Info("Finished searching {0} for {1} S{2:00}E{3:00}, Found {4}", Name, seriesTitle, seasonNumber, episodeNumber, result.Count);
return result;
2011-04-25 20:16:38 +02:00
}
2011-04-04 05:50:12 +02:00
public virtual IList<EpisodeParseResult> FetchDailyEpisode(string seriesTitle, DateTime airDate)
{
_logger.Debug("Searching {0} for {1}-{2}", Name, seriesTitle, airDate.ToShortDateString());
var searchUrls = GetDailyEpisodeSearchUrls(GetQueryTitle(seriesTitle), airDate);
var result = Fetch(searchUrls);
_logger.Info("Finished searching {0} for {1}-{2}, Found {3}", Name, seriesTitle, airDate.ToShortDateString(), result.Count);
return result;
}
2013-03-27 04:44:52 +01:00
private List<EpisodeParseResult> Fetch(IEnumerable<string> urls)
{
var result = new List<EpisodeParseResult>();
if (!IsConfigured)
{
_logger.Warn("Indexer '{0}' isn't configured correctly. please reconfigure the indexer in settings page.", Name);
return result;
}
foreach (var url in urls)
{
try
{
_logger.Trace("Downloading RSS " + url);
var reader = new SyndicationFeedXmlReader(_httpProvider.DownloadStream(url, Credentials));
var feed = SyndicationFeed.Load(reader).Items;
foreach (var item in feed)
2011-04-22 08:23:29 +02:00
{
try
2011-04-22 22:14:02 +02:00
{
var parsedEpisode = ParseFeed(item);
if (parsedEpisode != null)
{
parsedEpisode.NzbUrl = NzbDownloadUrl(item);
2012-05-02 21:02:39 +02:00
parsedEpisode.NzbInfoUrl = NzbInfoUrl(item);
parsedEpisode.Indexer = String.IsNullOrWhiteSpace(parsedEpisode.Indexer) ? Name : parsedEpisode.Indexer;
result.Add(parsedEpisode);
}
}
catch (Exception itemEx)
{
2012-01-19 03:08:17 +01:00
itemEx.Data.Add("FeedUrl", url);
itemEx.Data.Add("Item", item.Title);
_logger.ErrorException("An error occurred while processing feed item", itemEx);
2011-04-22 22:14:02 +02:00
}
}
}
catch (WebException webException)
{
if (webException.Message.Contains("503"))
{
2013-03-27 04:44:52 +01:00
_logger.Warn("{0} server is currently unavailable.{1} {2}", Name, url, webException.Message);
}
else
{
webException.Data.Add("FeedUrl", url);
2012-02-28 03:35:25 +01:00
_logger.ErrorException("An error occurred while processing feed. " + url, webException);
}
}
catch (Exception feedEx)
{
2012-01-19 03:08:17 +01:00
feedEx.Data.Add("FeedUrl", url);
2012-02-28 03:35:25 +01:00
_logger.ErrorException("An error occurred while processing feed. " + url, feedEx);
2011-04-22 08:23:29 +02:00
}
2011-04-04 05:50:12 +02:00
}
return result;
2011-04-04 05:50:12 +02:00
}
public EpisodeParseResult ParseFeed(SyndicationItem item)
{
var title = TitlePreParser(item);
var episodeParseResult = Parser.ParseTitle(title);
if (episodeParseResult != null)
{
episodeParseResult.Age = DateTime.Now.Date.Subtract(item.PublishDate.Date).Days;
episodeParseResult.OriginalString = title;
episodeParseResult.SceneSource = true;
}
_logger.Trace("Parsed: {0} from: {1}", episodeParseResult, item.Title.Text);
return CustomParser(item, episodeParseResult);
}
2011-05-27 08:03:57 +02:00
public virtual string GetQueryTitle(string title)
2011-05-27 08:03:57 +02:00
{
title = RemoveThe.Replace(title, string.Empty);
var cleanTitle = TitleSearchRegex.Replace(title, "+").Trim('+', ' ');
//remove any repeating +s
cleanTitle = Regex.Replace(cleanTitle, @"\+{1,100}", "+");
return cleanTitle;
2011-05-27 08:03:57 +02:00
}
2011-04-04 05:50:12 +02:00
}
2011-04-10 04:44:01 +02:00
}