From 482e2d5d42569e576aed0724327215ed1e4540fe Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 24 Sep 2020 11:46:23 +0200 Subject: [PATCH] Handle ratelimit api response for newznab caps endpoint on certain newznab indexers that have caps behind the apikey --- src/NzbDrone.Core/Indexers/HttpIndexerBase.cs | 8 ++++---- .../Newznab/NewznabCapabilitiesProvider.cs | 12 +++++++---- .../Indexers/Newznab/NewznabRssParser.cs | 20 ++++++++++++++----- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs b/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs index 9c3ad417d..60761fc2e 100644 --- a/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs +++ b/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs @@ -339,15 +339,15 @@ namespace NzbDrone.Core.Indexers return new ValidationFailure(string.Empty, "Query successful, but no results were returned from your indexer. This may be an issue with the indexer or your indexer category settings."); } } - catch (ApiKeyException) + catch (ApiKeyException ex) { - _logger.Warn("Indexer returned result for RSS URL, API Key appears to be invalid"); + _logger.Warn("Indexer returned result for RSS URL, API Key appears to be invalid: " + ex.Message); return new ValidationFailure("ApiKey", "Invalid API Key"); } - catch (RequestLimitReachedException) + catch (RequestLimitReachedException ex) { - _logger.Warn("Request limit reached"); + _logger.Warn("Request limit reached: " + ex.Message); } catch (CloudFlareCaptchaException ex) { diff --git a/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs b/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs index 1c955c75d..ef6f68ce8 100644 --- a/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs +++ b/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs @@ -68,13 +68,15 @@ namespace NzbDrone.Core.Indexers.Newznab } catch (XmlException ex) { + ex.WithData(response, 128 * 1024); + _logger.Trace("Unexpected Response content ({0} bytes): {1}", response.ResponseData.Length, response.Content); _logger.Debug(ex, "Failed to parse newznab api capabilities for {0}", indexerSettings.BaseUrl); - - ex.WithData(response); throw; } catch (Exception ex) { + ex.WithData(response, 128 * 1024); + _logger.Trace("Unexpected Response content ({0} bytes): {1}", response.ResponseData.Length, response.Content); _logger.Error(ex, "Failed to determine newznab api capabilities for {0}, using the defaults instead till Sonarr restarts", indexerSettings.BaseUrl); } @@ -89,14 +91,16 @@ namespace NzbDrone.Core.Indexers.Newznab if (xDoc == null) { - throw new XmlException("Invalid XML"); + throw new XmlException("Invalid XML").WithData(response); } + NewznabRssParser.CheckError(xDoc, new IndexerResponse(new IndexerRequest(response.Request), response)); + var xmlRoot = xDoc.Element("caps"); if (xmlRoot == null) { - throw new XmlException("Unexpected XML"); + throw new XmlException("Unexpected XML").WithData(response); } var xmlLimits = xmlRoot.Element("limits"); diff --git a/src/NzbDrone.Core/Indexers/Newznab/NewznabRssParser.cs b/src/NzbDrone.Core/Indexers/Newznab/NewznabRssParser.cs index 75b244fda..ddf9724e1 100644 --- a/src/NzbDrone.Core/Indexers/Newznab/NewznabRssParser.cs +++ b/src/NzbDrone.Core/Indexers/Newznab/NewznabRssParser.cs @@ -2,7 +2,9 @@ using System.Collections.Generic; using System.Linq; using System.Xml.Linq; +using MonoTorrent; using NzbDrone.Common.Extensions; +using NzbDrone.Common.Http; using NzbDrone.Core.Indexers.Exceptions; using NzbDrone.Core.Parser.Model; @@ -18,20 +20,19 @@ namespace NzbDrone.Core.Indexers.Newznab UseEnclosureUrl = true; } - protected override bool PreProcess(IndexerResponse indexerResponse) + public static void CheckError(XDocument xdoc, IndexerResponse indexerResponse) { - var xdoc = LoadXmlDocument(indexerResponse); var error = xdoc.Descendants("error").FirstOrDefault(); - if (error == null) return true; + if (error == null) + return; var code = Convert.ToInt32(error.Attribute("code").Value); var errorMessage = error.Attribute("description").Value; if (code >= 100 && code <= 199) { - _logger.Warn("Invalid API Key: {0}", errorMessage); - throw new ApiKeyException("Invalid API key"); + throw new ApiKeyException(errorMessage); } if (!indexerResponse.Request.Url.FullUri.Contains("apikey=") && (errorMessage == "Missing parameter" || errorMessage.Contains("apikey"))) @@ -47,6 +48,15 @@ namespace NzbDrone.Core.Indexers.Newznab throw new NewznabException(indexerResponse, errorMessage); } + protected override bool PreProcess(IndexerResponse indexerResponse) + { + var xdoc = LoadXmlDocument(indexerResponse); + + CheckError(xdoc, indexerResponse); + + return true; + } + protected override bool PostProcess(IndexerResponse indexerResponse, List items, List releases) { var enclosureTypes = items.SelectMany(GetEnclosures).Select(v => v.Type).Distinct().ToArray();