Compare commits

...

8 Commits

Author SHA1 Message Date
Boris Gerretzen 79a5804cbe
Merge 8d3dfd43d4 into a7efe1b6da 2024-04-26 05:33:44 +01:00
Garfield69 a7efe1b6da lilleskyorg: -> 8.0.2 resolves #15104 2024-04-26 15:33:03 +12:00
Boris 8d3dfd43d4 Redid xml to json conversion 2024-03-10 22:36:17 +01:00
Boris Gerretzen 4eaacaeb94
Merge branch 'Jackett:master' into master 2024-03-10 22:09:13 +01:00
Bogdan 86f5e5abce
Merge branch 'master' into master 2024-02-08 20:56:00 +02:00
Boris e4911054e3 Better formatting of caps as json 2024-02-08 18:48:02 +01:00
Boris 533a25da54 Fix linter error 2024-02-03 20:42:30 +01:00
Boris 23b62c2fa6 [enhancement]: support Torznab o=JSON 2024-02-03 20:33:09 +01:00
9 changed files with 267 additions and 29 deletions

View File

@ -11,21 +11,15 @@ links:
caps:
categorymappings:
- {id: 1, cat: Movies, desc: "MOVIES"}
- {id: 20, cat: Movies/Foreign, desc: "MOVIES-FOREIGN"}
- {id: 2, cat: TV, desc: "TV"}
- {id: 19, cat: TV/Foreign, desc: "TV-FOREIGN"}
- {id: 4, cat: PC, desc: "APPS"}
- {id: 5, cat: Audio, desc: "MUSIC"}
- {id: 6, cat: XXX, desc: "XXX"}
- {id: 7, cat: Console, desc: "GAMES"}
- {id: 8, cat: Books, desc: "EBOOKS"}
- {id: 3, cat: Audio, desc: "MUSIC"}
modes:
search: [q]
tv-search: [q, season, ep, imdbid, tvdbid, tmdbid]
movie-search: [q, imdbid, tmdbid]
music-search: [q]
book-search: [q]
# book-search: [q]
settings:
- name: apikey
@ -39,6 +33,10 @@ settings:
type: checkbox
label: Search freeleech only
default: false
- name: single_file_release_use_filename
type: checkbox
label: Use filename as title for single file releases
default: true
- name: sort
type: select
label: Sort requested from site
@ -100,7 +98,7 @@ search:
fields:
category:
selector: category_id
title:
title_optional:
selector: name
details:
selector: details_link
@ -122,10 +120,6 @@ search:
genre:
selector: meta.genres
filters:
- name: re_replace
args: ["(?i)^None$", ""]
- name: re_replace
args: ["(?i)(Science Fiction)", "Science_Fiction"]
- name: replace
args: [" & ", "_&_"]
description:
@ -170,4 +164,9 @@ search:
minimumseedtime:
# 7 days (as seconds = 7 x 24 x 60 x 60)
text: 604800
# json UNIT3D 7.2.5
title_filename:
selector: "files[0].name"
optional: true
title:
text: "{{ if and (.Config.single_file_release_use_filename) (eq .Result.files \"1\") (.Result.title_filename) }}{{ .Result.title_filename }}{{ else }}{{ .Result.title_optional }}{{ end }}"
# json UNIT3D 8.0.2

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));
}
}
}