This commit is contained in:
Boris Gerretzen 2024-05-12 01:39:21 +01:00 committed by GitHub
commit adb8c47268
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 254 additions and 15 deletions

View File

@ -0,0 +1,9 @@
using Newtonsoft.Json.Serialization;
namespace Jackett.Common.Helpers
{
public class LowerCaseNamingStrategy : NamingStrategy
{
protected override string ResolvePropertyName(string name) => name.ToLowerInvariant();
}
}

View File

@ -0,0 +1,71 @@
using System.Linq;
using System.Xml.Linq;
using Jackett.Common.Extensions;
using Newtonsoft.Json.Linq;
namespace Jackett.Common.Helpers
{
public static class XmlToJsonConverter
{
/// <summary>
/// Converts an XML element to a JSON object.
/// Attributes are stored in an @attributes object.
/// </summary>
public static JToken XmlToJson(XElement element)
{
var obj = new JObject();
// Build @attributes object from element attributes
if (element.Attributes().Any())
{
var attributes = element.Attributes()
.ToDictionary(
attribute => GetName(attribute.Name, attribute.Parent?.Document),
attribute => (JToken)attribute.Value
);
obj.Add("@attributes", JObject.FromObject(attributes));
}
foreach (var childElement in element.Elements())
{
var childObj = XmlToJson(childElement);
var childName = GetName(childElement.Name, element.Document);
// If the child name already exists, convert it to an array
if (obj.ContainsKey(childName))
{
if (obj[childName] is JArray existingArray)
{
existingArray.Add(childObj);
}
else
{
obj[childName] = new JArray(obj[childName]!, childObj);
}
}
else
{
obj.Add(childName, childObj);
}
}
if (!element.Elements().Any() && !element.Value.IsNullOrWhiteSpace())
{
return element.Value;
}
return obj;
}
private static string GetName(XName name, XDocument document)
{
if (document == null)
{
return name.LocalName;
}
var prefix = document.Root?.GetPrefixOfNamespace(name.Namespace);
return prefix != null ? $"{prefix}:{name.LocalName}" : name.LocalName;
}
}
}

View File

@ -10,6 +10,7 @@ namespace Jackett.Common.Models.DTO
public string imdbid { get; set; }
public string ep { get; set; }
public string t { get; set; }
public string o { get; set; }
public string extended { get; set; }
public string limit { get; set; }
public string offset { get; set; }

View File

@ -5,6 +5,8 @@ using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml.Linq;
using Jackett.Common.Helpers;
using Newtonsoft.Json;
namespace Jackett.Common.Models
{
@ -52,7 +54,7 @@ namespace Jackett.Common.Models
return new XElement(_TorznabNs + "attr", new XAttribute("name", name), new XAttribute("value", value));
}
public string ToXml(Uri selfAtom)
private XDocument GetXDocument(Uri selfAtom)
{
// IMPORTANT: We can't use Uri.ToString(), because it generates URLs without URL encode (links with unicode
// characters are broken). We must use Uri.AbsoluteUri instead that handles encoding correctly
@ -127,7 +129,19 @@ namespace Jackett.Common.Models
)
);
return xdoc;
}
public string ToXml(Uri selfAtom)
{
var xdoc = GetXDocument(selfAtom);
return xdoc.Declaration + Environment.NewLine + xdoc;
}
public string ToJson(Uri selfAtom, JsonSerializerSettings serializerSettings = null)
{
var jsonObject = XmlToJsonConverter.XmlToJson(GetXDocument(selfAtom).Root);
return JsonConvert.SerializeObject(jsonObject, serializerSettings);
}
}
}

View File

@ -2,6 +2,8 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Jackett.Common.Helpers;
using Newtonsoft.Json;
namespace Jackett.Common.Models
{
@ -352,6 +354,12 @@ namespace Jackett.Common.Models
public string ToXml() =>
GetXDocument().Declaration + Environment.NewLine + GetXDocument();
public string ToJson(JsonSerializerSettings serializerSettings = null)
{
var jsonObject = XmlToJsonConverter.XmlToJson(GetXDocument().Root);
return JsonConvert.SerializeObject(jsonObject, serializerSettings);
}
public static TorznabCapabilities Concat(TorznabCapabilities lhs, TorznabCapabilities rhs)
{
lhs.SearchAvailable = lhs.SearchAvailable || rhs.SearchAvailable;

View File

@ -48,6 +48,8 @@ namespace Jackett.Common.Models
public bool IsTest { get; set; }
public bool IsJson { get; set; }
public string ImdbIDShort => ImdbID?.TrimStart('t');
protected string[] QueryStringParts;

View File

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using System.Xml.Linq;
using Jackett.Common;
using Jackett.Common.Exceptions;
using Jackett.Common.Helpers;
using Jackett.Common.Indexers;
using Jackett.Common.Indexers.Meta;
using Jackett.Common.Models;
@ -20,6 +21,9 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using NLog;
namespace Jackett.Server.Controllers
@ -175,6 +179,17 @@ namespace Jackett.Server.Controllers
private readonly IServerService serverService;
private readonly ICacheService cacheService;
private readonly Common.Models.Config.ServerConfig serverConfig;
private static readonly JsonSerializerSettings _JsonSerializerSettings = new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
new StringEnumConverter
{
NamingStrategy = new LowerCaseNamingStrategy()
}
},
NullValueHandling = NullValueHandling.Ignore
};
public ResultsController(IIndexerManagerService indexerManagerService, IServerService ss, ICacheService c, Logger logger, Common.Models.Config.ServerConfig sConfig)
{
@ -339,9 +354,16 @@ namespace Jackett.Server.Controllers
[HttpGet]
public async Task<IActionResult> Torznab([FromQuery] TorznabRequest request)
{
if (string.Equals(request.o, "json", StringComparison.InvariantCultureIgnoreCase))
{
CurrentQuery.IsJson = true;
}
if (string.Equals(CurrentQuery.QueryType, "caps", StringComparison.InvariantCultureIgnoreCase))
{
return Content(CurrentIndexer.TorznabCaps.ToXml(), "application/rss+xml", Encoding.UTF8);
return CurrentQuery.IsJson ?
Content(CurrentIndexer.TorznabCaps.ToJson(_JsonSerializerSettings), "application/json", Encoding.UTF8) :
Content(CurrentIndexer.TorznabCaps.ToXml(), "application/rss+xml", Encoding.UTF8);
}
// indexers - returns a list of all included indexers (meta indexers only)
@ -350,7 +372,7 @@ namespace Jackett.Server.Controllers
if (!(CurrentIndexer is BaseMetaIndexer)) // shouldn't be needed because CanHandleQuery should return false
{
logger.Warn($"A search request with t=indexers from {Request.HttpContext.Connection.RemoteIpAddress} was made but the indexer {CurrentIndexer.Name} isn't a meta indexer.");
return GetErrorXML(203, "Function Not Available: this isn't a meta indexer");
return GetError(203, "Function Not Available: this isn't a meta indexer");
}
var CurrentBaseMetaIndexer = (BaseMetaIndexer)CurrentIndexer;
var indexers = CurrentBaseMetaIndexer.Indexers;
@ -376,6 +398,11 @@ namespace Jackett.Server.Controllers
)
);
if (CurrentQuery.IsJson)
{
return Json(XmlToJsonConverter.XmlToJson(xdoc.Root), _JsonSerializerSettings);
}
return Content(xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString(), "application/xml", Encoding.UTF8);
}
@ -395,19 +422,19 @@ namespace Jackett.Server.Controllers
if (CurrentQuery.ImdbID == null)
{
logger.Warn($"A search request from {Request.HttpContext.Connection.RemoteIpAddress} was made with an invalid imdbid.");
return GetErrorXML(201, "Incorrect parameter: invalid imdbid format");
return GetError(201, "Incorrect parameter: invalid imdbid format");
}
if (CurrentQuery.IsMovieSearch && !CurrentIndexer.TorznabCaps.MovieSearchImdbAvailable)
{
logger.Warn($"A search request with imdbid from {Request.HttpContext.Connection.RemoteIpAddress} was made but the indexer {CurrentIndexer.Name} doesn't support it.");
return GetErrorXML(203, "Function Not Available: imdbid is not supported for movie search by this indexer");
return GetError(203, "Function Not Available: imdbid is not supported for movie search by this indexer");
}
if (CurrentQuery.IsTVSearch && !CurrentIndexer.TorznabCaps.TvSearchImdbAvailable)
{
logger.Warn($"A search request with imdbid from {Request.HttpContext.Connection.RemoteIpAddress} was made but the indexer {CurrentIndexer.Name} doesn't support it.");
return GetErrorXML(203, "Function Not Available: imdbid is not supported for TV search by this indexer");
return GetError(203, "Function Not Available: imdbid is not supported for TV search by this indexer");
}
}
@ -416,13 +443,13 @@ namespace Jackett.Server.Controllers
if (CurrentQuery.IsMovieSearch && !CurrentIndexer.TorznabCaps.MovieSearchTmdbAvailable)
{
logger.Warn($"A search request with tmdbid from {Request.HttpContext.Connection.RemoteIpAddress} was made but the indexer {CurrentIndexer.Name} doesn't support it.");
return GetErrorXML(203, "Function Not Available: tmdbid is not supported for movie search by this indexer");
return GetError(203, "Function Not Available: tmdbid is not supported for movie search by this indexer");
}
if (CurrentQuery.IsTVSearch && !CurrentIndexer.TorznabCaps.TvSearchTmdbAvailable)
{
logger.Warn($"A search request with tmdbid from {Request.HttpContext.Connection.RemoteIpAddress} was made but the indexer {CurrentIndexer.Name} doesn't support it.");
return GetErrorXML(203, "Function Not Available: tmdbid is not supported for TV search by this indexer");
return GetError(203, "Function Not Available: tmdbid is not supported for TV search by this indexer");
}
}
@ -431,7 +458,7 @@ namespace Jackett.Server.Controllers
if (CurrentQuery.IsTVSearch && !CurrentIndexer.TorznabCaps.TvSearchAvailable)
{
logger.Warn($"A search request with tvdbid from {Request.HttpContext.Connection.RemoteIpAddress} was made but the indexer {CurrentIndexer.Name} doesn't support it.");
return GetErrorXML(203, "Function Not Available: tvdbid is not supported for movie search by this indexer");
return GetError(203, "Function Not Available: tvdbid is not supported for movie search by this indexer");
}
}
@ -465,7 +492,14 @@ namespace Jackett.Server.Controllers
else
logger.Info($"Torznab search in {CurrentIndexer.Name} for {CurrentQuery.GetQueryString()} => Found {result.Releases.Count()} releases{cacheStr} [{stopwatch.ElapsedMilliseconds:0}ms]");
var xml = resultPage.ToXml(new Uri(serverUrl));
var serverUri = new Uri(serverUrl);
if (CurrentQuery.IsJson)
{
var json = resultPage.ToJson(serverUri);
return Content(json, "application/json", Encoding.UTF8);
}
var xml = resultPage.ToXml(serverUri);
// Force the return as XML
return Content(xml, "application/rss+xml", Encoding.UTF8);
@ -485,25 +519,29 @@ namespace Jackett.Server.Controllers
}
}
return GetErrorXML(900, ex.Message, StatusCodes.Status429TooManyRequests);
return GetError(900, ex.Message, StatusCodes.Status429TooManyRequests);
}
catch (Exception e)
{
logger.Error(e);
return GetErrorXML(900, e.ToString());
return GetError(900, e.ToString());
}
}
[Route("[action]/{ignored?}")]
public IActionResult GetErrorXML(int code, string description, int statusCode = StatusCodes.Status400BadRequest)
public IActionResult GetError(int code, string description, int statusCode = StatusCodes.Status400BadRequest)
{
var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse("application/xml");
var mediaTypeHeaderValue = CurrentQuery.IsJson ?
MediaTypeHeaderValue.Parse("application/json") :
MediaTypeHeaderValue.Parse("application/xml");
mediaTypeHeaderValue.Encoding = Encoding.UTF8;
return new ContentResult
{
StatusCode = statusCode,
Content = CreateErrorXML(code, description),
Content = CurrentQuery.IsJson ?
CreateErrorJson(code, description) :
CreateErrorXML(code, description),
ContentType = mediaTypeHeaderValue.ToString()
};
}
@ -520,6 +558,19 @@ namespace Jackett.Server.Controllers
return xdoc.Declaration + Environment.NewLine + xdoc;
}
public static string CreateErrorJson(int code, string description)
{
return new JObject
{
{ "error", new JObject
{
{ "code", code },
{ "description", description }
}
}
}.ToString();
}
public static IActionResult GetErrorActionResult(RouteData routeData, HttpStatusCode status, int torznabCode, string description)
{
var isTorznab = routeData.Values["action"].ToString().Equals("torznab", StringComparison.OrdinalIgnoreCase);

View File

@ -0,0 +1,83 @@
using System.Xml.Linq;
using Jackett.Common.Helpers;
using Newtonsoft.Json;
using NUnit.Framework;
namespace Jackett.Test.Common.Helpers
{
[TestFixture]
public class XmlToJsonConverterTests
{
[Test]
public void XmlToJsonConverter_XmlToJson_Empty()
{
var element = new XElement("test");
var json = XmlToJsonConverter.XmlToJson(element);
Assert.AreEqual("{}", json.ToString(Formatting.None));
}
[Test]
public void XmlToJsonConverter_XmlToJson_WithAttributes()
{
var element = new XElement("test", new XAttribute("attr1", "value1"), new XAttribute("attr2", "value2"));
var json = XmlToJsonConverter.XmlToJson(element);
Assert.AreEqual("{\"@attributes\":{\"attr1\":\"value1\",\"attr2\":\"value2\"}}", json.ToString(Formatting.None));
}
[Test]
public void XmlToJsonConverter_XmlToJson_WithChildElement()
{
var element = new XElement("test", new XElement("child"));
var json = XmlToJsonConverter.XmlToJson(element);
Assert.AreEqual("{\"child\":{}}", json.ToString(Formatting.None));
}
[Test]
public void XmlToJsonConverter_XmlToJson_WithChildElement_WithAttributes()
{
var element = new XElement("test", new XElement("child", new XAttribute("attr1", "value1"), new XAttribute("attr2", "value2")));
var json = XmlToJsonConverter.XmlToJson(element);
Assert.AreEqual("{\"child\":{\"@attributes\":{\"attr1\":\"value1\",\"attr2\":\"value2\"}}}", json.ToString(Formatting.None));
}
[Test]
public void XmlToJsonConverter_XmlToJson_WithChildElement_WithText()
{
var element = new XElement("test", new XElement("child", new XElement("subchild", "text")));
var json = XmlToJsonConverter.XmlToJson(element);
Assert.AreEqual("{\"child\":{\"subchild\":\"text\"}}", json.ToString(Formatting.None));
}
[Test]
public void XmlToJsonConverter_XmlToJson_WithChildElementArray()
{
var element = new XElement("test", new XElement("child"), new XElement("child"));
var json = XmlToJsonConverter.XmlToJson(element);
Assert.AreEqual("{\"child\":[{},{}]}", json.ToString(Formatting.None));
}
[Test]
public void XmlToJsonConverter_XmlToJson_WithChildElementArray_WithText()
{
var element = new XElement("test", new XElement("child", "text1"), new XElement("child", "text2"));
var json = XmlToJsonConverter.XmlToJson(element);
Assert.AreEqual("{\"child\":[\"text1\",\"text2\"]}", json.ToString(Formatting.None));
}
[Test]
public void XmlToJsonConverter_XmlToJson_HandlesNamespaces()
{
var document = new XDocument(
new XElement("root",
new XAttribute(XNamespace.Xmlns + "ns1", "http://example.com/ns1"),
new XAttribute(XNamespace.Xmlns + "ns2", "http://example.com/ns2"),
new XElement("{http://example.com/ns1}child1"),
new XElement("{http://example.com/ns2}child2")
)
);
var json = XmlToJsonConverter.XmlToJson(document.Root);
Assert.AreEqual("{\"@attributes\":{\"xmlns:ns1\":\"http://example.com/ns1\",\"xmlns:ns2\":\"http://example.com/ns2\"},\"ns1:child1\":{},\"ns2:child2\":{}}", json.ToString(Formatting.None));
}
}
}