From 58206c3ba753feb6af86428551eb0fcbcdc1555d Mon Sep 17 00:00:00 2001 From: mikeoscar2006 <89641725+mikeoscar2006@users.noreply.github.com> Date: Fri, 10 Sep 2021 08:44:57 +0530 Subject: [PATCH] [Cardigann] Add optional pathselector in the BEFORE block and few fixes (#12276) --- .../Indexers/CardigannIndexer.cs | 67 +++++++++++++++---- .../Models/IndexerDefinition.cs | 15 +++-- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/src/Jackett.Common/Indexers/CardigannIndexer.cs b/src/Jackett.Common/Indexers/CardigannIndexer.cs index 69f862d32..53caa5bb2 100644 --- a/src/Jackett.Common/Indexers/CardigannIndexer.cs +++ b/src/Jackett.Common/Indexers/CardigannIndexer.cs @@ -1703,13 +1703,16 @@ namespace Jackett.Common.Indexers pairs = new Dictionary(); } - foreach (var Input in request.Inputs) + if (request.Inputs != null) { - var value = applyGoTemplateText(Input.Value, variables); - if (method == RequestType.GET) - queryCollection.Add(Input.Key, value); - else if (method == RequestType.POST) - pairs.Add(Input.Key, value); + foreach (var Input in request.Inputs) + { + var value = applyGoTemplateText(Input.Value, variables); + if (method == RequestType.GET) + queryCollection.Add(Input.Key, value); + else if (method == RequestType.POST) + pairs.Add(Input.Key, value); + } } if (queryCollection.Count > 0) @@ -1751,9 +1754,45 @@ namespace Jackett.Common.Indexers var variables = GetBaseTemplateVariables(); AddTemplateVariablesFromUri(variables, link, ".DownloadUri"); + var headers = ParseCustomHeaders(Definition.Search?.Headers, variables); WebResult response = null; - if (Download.Before != null) - response = await handleRequest(Download.Before, variables, link.ToString()); + HtmlParser searchResultParser = null; + string results = string.Empty; + + var beforeBlock = Download.Before; + if (beforeBlock != null) + { + if (beforeBlock.Pathselector != null) + { + response = await RequestWithCookiesAsync(link.ToString(), headers: headers); + if (response.IsRedirect) + response = await RequestWithCookiesAsync(response.RedirectingTo, headers: headers); + + var beforeSelector = applyGoTemplateText(beforeBlock.Pathselector.Selector, variables); + searchResultParser = new HtmlParser(); + + results = response.ContentString; + var searchResultDocument = searchResultParser.ParseDocument(results); + + var beforeElement = searchResultDocument.QuerySelector(beforeSelector); + if (beforeElement == null) + { + logger.Debug( + $"CardigannIndexer ({Id}): Before path selector {beforeSelector} could not match any elements."); + throw new Exception($"Before selectors didn't match"); + } + + string path; + if (beforeBlock.Pathselector.Attribute != null) + path = beforeElement.GetAttribute(beforeBlock.Pathselector.Attribute); + else + path = beforeElement.TextContent; + path = applyFilters(path, beforeBlock.Pathselector.Filters, variables); + beforeBlock.Path = path; + } + + response = await handleRequest(beforeBlock, variables, link.ToString()); + } if (Download.Method == "post") method = RequestType.POST; @@ -1766,9 +1805,9 @@ namespace Jackett.Common.Indexers try { - var headers = ParseCustomHeaders(Definition.Search?.Headers, variables); - var results = ""; - var searchResultParser = new HtmlParser(); + headers = ParseCustomHeaders(Definition.Search?.Headers, variables); + results = ""; + searchResultParser = new HtmlParser(); if (!Download.Infohash.Before || Download.Before == null || response == null) { @@ -1820,9 +1859,9 @@ namespace Jackett.Common.Indexers } else if (Download.Selectors != null) { - var headers = ParseCustomHeaders(Definition.Search?.Headers, variables); - var results = ""; - var searchResultParser = new HtmlParser(); + headers = ParseCustomHeaders(Definition.Search?.Headers, variables); + results = ""; + searchResultParser = new HtmlParser(); foreach (var selector in Download.Selectors) { diff --git a/src/Jackett.Common/Models/IndexerDefinition.cs b/src/Jackett.Common/Models/IndexerDefinition.cs index 168e37564..52c260ac7 100644 --- a/src/Jackett.Common/Models/IndexerDefinition.cs +++ b/src/Jackett.Common/Models/IndexerDefinition.cs @@ -165,22 +165,27 @@ namespace Jackett.Common.Models public string Queryseparator { get; set; } = "&"; } + public class beforeBlock : requestBlock + { + public selectorField Pathselector { get; set; } + } + public class infohashBlock { - public downloadsField Hash { get; set; } - public downloadsField Title { get; set; } + public selectorField Hash { get; set; } + public selectorField Title { get; set; } public bool Before { get; set; } = false; } public class downloadBlock { - public List Selectors { get; set; } + public List Selectors { get; set; } public string Method { get; set; } - public requestBlock Before { get; set; } + public beforeBlock Before { get; set; } public infohashBlock Infohash { get; set; } } - public class downloadsField + public class selectorField { public string Selector { get; set; } public string Attribute { get; set; }