From 2ea2b0b1def70fee1aa8550751bf3882a7ff9eab Mon Sep 17 00:00:00 2001 From: mikeoscar2006 <89641725+mikeoscar2006@users.noreply.github.com> Date: Mon, 6 Sep 2021 09:12:15 +0530 Subject: [PATCH] [Cardigann] Add infohash feature for download block (#12258) --- .../Indexers/CardigannIndexer.cs | 73 +++++++++++++++++-- .../Models/IndexerDefinition.cs | 8 ++ 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/Jackett.Common/Indexers/CardigannIndexer.cs b/src/Jackett.Common/Indexers/CardigannIndexer.cs index 46adb22dc..69f862d32 100644 --- a/src/Jackett.Common/Indexers/CardigannIndexer.cs +++ b/src/Jackett.Common/Indexers/CardigannIndexer.cs @@ -1715,9 +1715,8 @@ namespace Jackett.Common.Indexers if (queryCollection.Count > 0) { if (!requestLinkStr.Contains("?")) - requestLinkStr += "?" + queryCollection.GetQueryString(Encoding, separator: request.Queryseparator).Substring(1); - else - requestLinkStr += queryCollection.GetQueryString(Encoding, separator: request.Queryseparator); + requestLinkStr += "?"; + requestLinkStr += queryCollection.GetQueryString(Encoding, separator: request.Queryseparator); } var response = await RequestWithCookiesAndRetryAsync(requestLinkStr, null, method, referer, pairs); @@ -1751,13 +1750,75 @@ namespace Jackett.Common.Indexers var Download = Definition.Download; var variables = GetBaseTemplateVariables(); AddTemplateVariablesFromUri(variables, link, ".DownloadUri"); + + WebResult response = null; if (Download.Before != null) - await handleRequest(Download.Before, variables, link.ToString()); + response = await handleRequest(Download.Before, variables, link.ToString()); if (Download.Method == "post") method = RequestType.POST; + if (Download.Infohash != null) + { + var hashBlock = Download.Infohash.Hash; + var hashSelector = applyGoTemplateText(hashBlock.Selector, variables); + var titleBlock = Download.Infohash.Title; + var titleSelector = applyGoTemplateText(titleBlock.Selector, variables); - if (Download.Selectors != null) + try + { + var headers = ParseCustomHeaders(Definition.Search?.Headers, variables); + var results = ""; + var searchResultParser = new HtmlParser(); + + if (!Download.Infohash.Before || Download.Before == null || response == null) + { + response = await RequestWithCookiesAsync(link.ToString(), headers: headers); + if (response.IsRedirect) + response = await RequestWithCookiesAsync(response.RedirectingTo, headers: headers); + } + results = response.ContentString; + var searchResultDocument = searchResultParser.ParseDocument(results); + var hashElement = searchResultDocument.QuerySelector(hashSelector); + if (hashElement == null) + { + logger.Debug( + $"CardigannIndexer ({Id}): Hash selector {hashSelector} could not match any elements."); + throw new Exception($"InfoHash selectors didn't match"); + } + var hash = string.Empty; + if (hashBlock.Attribute != null) + hash = hashElement.GetAttribute(hashBlock.Attribute); + else + hash = hashElement.TextContent; + hash = applyFilters(hash, hashBlock.Filters, variables); + + var titleElement = searchResultDocument.QuerySelector(titleSelector); + if (titleElement == null) + { + logger.Debug( + $"CardigannIndexer ({Id}): Title selector {titleSelector} could not match any elements."); + throw new Exception($"InfoHash selectors didn't match"); + } + var title = string.Empty; + if (titleBlock.Attribute != null) + title = titleElement.GetAttribute(titleBlock.Attribute); + else + title = titleElement.TextContent; + title = applyFilters(title, titleBlock.Filters, variables); + + var magnet = MagnetUtil.InfoHashToPublicMagnet(hash, title); + var torrentLink = resolvePath(magnet.AbsoluteUri, link); + return await base.Download(torrentLink, method, torrentLink.ToString()); + } + catch (Exception e) + { + logger.Error(e, + $"CardigannIndexer ({Id}): An exception occurred while trying infohash block with hashSelector {hashSelector} and titleSelector {titleSelector}" + ); + } + + } + else if (Download.Selectors != null) { var headers = ParseCustomHeaders(Definition.Search?.Headers, variables); var results = ""; @@ -1769,7 +1830,7 @@ namespace Jackett.Common.Indexers try { - var response = await RequestWithCookiesAsync(link.ToString(), headers: headers); + response = await RequestWithCookiesAsync(link.ToString(), headers: headers); if (response.IsRedirect) response = await RequestWithCookiesAsync(response.RedirectingTo, headers: headers); results = response.ContentString; diff --git a/src/Jackett.Common/Models/IndexerDefinition.cs b/src/Jackett.Common/Models/IndexerDefinition.cs index 119388e11..168e37564 100644 --- a/src/Jackett.Common/Models/IndexerDefinition.cs +++ b/src/Jackett.Common/Models/IndexerDefinition.cs @@ -165,11 +165,19 @@ namespace Jackett.Common.Models public string Queryseparator { get; set; } = "&"; } + public class infohashBlock + { + public downloadsField Hash { get; set; } + public downloadsField Title { get; set; } + public bool Before { get; set; } = false; + } + public class downloadBlock { public List Selectors { get; set; } public string Method { get; set; } public requestBlock Before { get; set; } + public infohashBlock Infohash { get; set; } } public class downloadsField