diff --git a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs index efd4b0acd..2f8323079 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs @@ -139,20 +139,13 @@ namespace NzbDrone.Core.Indexers.Definitions.Cardigann { var selectorSelector = ApplyGoTemplateText(selector.Selector, variables); - if (dom.Matches(selectorSelector)) - { - selection = dom; - } - else - { - selection = QuerySelector(dom, selectorSelector); - } + selection = dom.Matches(selectorSelector) ? dom : QuerySelector(dom, selectorSelector); if (selection == null) { if (required) { - throw new Exception(string.Format("Selector \"{0}\" didn't match {1}", selectorSelector, dom.ToHtmlPretty())); + throw new Exception($"Selector \"{selectorSelector}\" didn't match {dom.ToHtmlPretty()}"); } return null; @@ -195,7 +188,7 @@ namespace NzbDrone.Core.Indexers.Definitions.Cardigann { if (required) { - throw new Exception(string.Format("Attribute \"{0}\" is not set for element {1}", selector.Attribute, selection.ToHtmlPretty())); + throw new Exception($"Attribute \"{selector.Attribute}\" is not set for element {selection.ToHtmlPretty()}"); } return null; diff --git a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannRequestGenerator.cs b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannRequestGenerator.cs index 45019d590..da9d7b415 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannRequestGenerator.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannRequestGenerator.cs @@ -332,37 +332,47 @@ namespace NzbDrone.Core.Indexers.Definitions.Cardigann } // selector inputs - if (login.Selectorinputs != null) + if (login.Selectorinputs != null && login.Selectorinputs.Any()) { - foreach (var selectorinput in login.Selectorinputs) + foreach (var selectorInput in login.Selectorinputs) { - string value = null; try { - value = HandleSelector(selectorinput.Value, landingResultDocument.FirstElementChild); - pairs[selectorinput.Key] = value; + var value = HandleSelector(selectorInput.Value, landingResultDocument.FirstElementChild, required: !selectorInput.Value.Optional); + + if (selectorInput.Value.Optional && value == null) + { + continue; + } + + pairs[selectorInput.Key] = value; } catch (Exception ex) { - throw new CardigannException(string.Format("Error while parsing selector input={0}, selector={1}, value={2}: {3}", selectorinput.Key, selectorinput.Value.Selector, value, ex.Message)); + throw new CardigannException($"Error while parsing selector input={selectorInput.Key}, selector={selectorInput.Value.Selector}: {ex.Message}", ex); } } } // getselector inputs - if (login.Getselectorinputs != null) + if (login.Getselectorinputs != null && login.Getselectorinputs.Any()) { - foreach (var selectorinput in login.Getselectorinputs) + foreach (var selectorInput in login.Getselectorinputs) { - string value = null; try { - value = HandleSelector(selectorinput.Value, landingResultDocument.FirstElementChild); - queryCollection[selectorinput.Key] = value; + var value = HandleSelector(selectorInput.Value, landingResultDocument.FirstElementChild, required: !selectorInput.Value.Optional); + + if (selectorInput.Value.Optional && value == null) + { + continue; + } + + queryCollection[selectorInput.Key] = value; } catch (Exception ex) { - throw new CardigannException(string.Format("Error while parsing get selector input={0}, selector={1}, value={2}: {3}", selectorinput.Key, selectorinput.Value.Selector, value, ex.Message)); + throw new CardigannException($"Error while parsing get selector input={selectorInput.Key}, selector={selectorInput.Value.Selector}: {ex.Message}", ex); } } }