Now checking for errors before parsing newznab feeds

This commit is contained in:
Mark McDowall
2013-09-26 21:41:08 -07:00
parent ca429cf5de
commit 9fa4cedb71
11 changed files with 107 additions and 16 deletions
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NzbDrone.Common.Exceptions;
namespace NzbDrone.Core.Indexers.Newznab
{
public class NewznabException : NzbDroneException
{
public NewznabException(string message, params object[] args) : base(message, args)
{
}
public NewznabException(string message) : base(message)
{
}
}
}
@@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using NzbDrone.Core.Indexers.Exceptions;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.Indexers.Newznab
@@ -46,5 +48,10 @@ namespace NzbDrone.Core.Indexers.Newznab
return currentResult;
}
protected override void PreProcess(string source, string url)
{
NewznabPreProcessor.Process(source, url);
}
}
}
@@ -0,0 +1,24 @@
using System;
using System.Linq;
using System.Xml.Linq;
using NzbDrone.Core.Indexers.Exceptions;
namespace NzbDrone.Core.Indexers.Newznab
{
public static class NewznabPreProcessor
{
public static void Process(string source, string url)
{
var xdoc = XDocument.Parse(source);
var error = xdoc.Descendants("error").FirstOrDefault();
if (error == null) return;
var code = Convert.ToInt32(error.Attribute("code").Value);
if (code >= 100 && code <= 199) throw new ApiKeyException("Invalid API key: {0}");
throw new NewznabException("Newznab error detected: {0}", error.Attribute("description").Value);
}
}
}