From 424370a1c3434a0bdce19900e66e2912658c7443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Franco?= Date: Mon, 6 Jan 2020 02:13:21 +0000 Subject: [PATCH] Added Cardigann support for functions "and" and "or" in templates (#6787) resolves #6780 Fix soundpark search for lidarr --- src/Jackett.Common/Definitions/soundpark.yml | 4 +- .../Indexers/CardigannIndexer.cs | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/Jackett.Common/Definitions/soundpark.yml b/src/Jackett.Common/Definitions/soundpark.yml index 50e4ef3a4..a973fa296 100644 --- a/src/Jackett.Common/Definitions/soundpark.yml +++ b/src/Jackett.Common/Definitions/soundpark.yml @@ -38,9 +38,9 @@ search: paths: - - path: "{{if .Keywords}}search{{else}}music{{end}}" + - path: "{{if or (.Query.Album) (.Query.Artist) (.Keywords) }}search{{else}}music{{end}}" inputs: - q: "{{if .Query.Artist}}{{ .Query.Artist }}{{else}}{{ .Keywords }}{{end}}" + q: "{{if or (.Query.Album) (.Query.Artist) }}{{ or (.Query.Album) (.Query.Artist) }}{{else}}{{ .Keywords }}{{end}}" num: 50 rows: diff --git a/src/Jackett.Common/Indexers/CardigannIndexer.cs b/src/Jackett.Common/Indexers/CardigannIndexer.cs index 8501f5992..91ae93d23 100644 --- a/src/Jackett.Common/Indexers/CardigannIndexer.cs +++ b/src/Jackett.Common/Indexers/CardigannIndexer.cs @@ -273,6 +273,65 @@ namespace Jackett.Common.Indexers JoinMatches = JoinMatches.NextMatch(); } + // handle or, and functions + Regex AndOrRegex = new Regex(@"(and|or)\s+\((\..+?)\)\s+\((\..+?)\)(\s+\((\..+?)\)){0,1}"); + var AndOrRegexMatches = AndOrRegex.Match(template); + + while (AndOrRegexMatches.Success) + { + string functionResult = ""; + string all = AndOrRegexMatches.Groups[0].Value; + string op = AndOrRegexMatches.Groups[1].Value; + string first = AndOrRegexMatches.Groups[2].Value; + string second = AndOrRegexMatches.Groups[3].Value; + string third = ""; + if (AndOrRegexMatches.Groups.Count > 5) + { + third = AndOrRegexMatches.Groups[5].Value; + } + + var value = variables[first]; + if (op == "and") + { + functionResult = second; + if (value == null || (value is string && string.IsNullOrWhiteSpace((string)value))) + { + functionResult = first; + } + else + { + if (!string.IsNullOrWhiteSpace(third)) + { + functionResult = third; + value = variables[second]; + if (value == null || (value is string && string.IsNullOrWhiteSpace((string)value))) + { + functionResult = second; + } + } + } + } + if (op == "or") + { + functionResult = first; + if (value == null || (value is string && string.IsNullOrWhiteSpace((string)value))) + { + functionResult = second; + if (!string.IsNullOrWhiteSpace(third)) + { + value = variables[second]; + if (value == null || (value is string && string.IsNullOrWhiteSpace((string)value))) + { + functionResult = third; + } + } + } + + } + template = template.Replace(all, functionResult); + AndOrRegexMatches = AndOrRegexMatches.NextMatch(); + } + // handle if ... else ... expression Regex IfElseRegex = new Regex(@"{{\s*if\s*(.+?)\s*}}(.*?){{\s*else\s*}}(.*?){{\s*end\s*}}"); var IfElseRegexMatches = IfElseRegex.Match(template);