From ac6bf052a733625383dacc0054b555c52ab11678 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Wed, 12 Oct 2022 09:10:25 +0200 Subject: [PATCH] jq import fix --- changedetectionio/forms.py | 10 +++++++--- changedetectionio/html_tools.py | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/changedetectionio/forms.py b/changedetectionio/forms.py index 7fa17f90..51e02884 100644 --- a/changedetectionio/forms.py +++ b/changedetectionio/forms.py @@ -303,12 +303,16 @@ class ValidateCSSJSONXPATHInput(object): # Re #265 - maybe in the future fetch the page and offer a # warning/notice that its possible the rule doesnt yet match anything? - - if 'jq:' in line: if not self.allow_json: raise ValidationError("jq not permitted in this field!") - import jq + if 'jq:' in line: + try: + import jq + except ModuleNotFoundError: + # `jq` requires full compilation in windows and so isn't generally available + raise ValidationError("jq not support not found") + input = line.replace('jq:', '') try: diff --git a/changedetectionio/html_tools.py b/changedetectionio/html_tools.py index 6cc8e20a..167d0f77 100644 --- a/changedetectionio/html_tools.py +++ b/changedetectionio/html_tools.py @@ -1,12 +1,11 @@ -import json -from typing import List from bs4 import BeautifulSoup -from jsonpath_ng.ext import parse -import jq -import re from inscriptis import get_text from inscriptis.model.config import ParserConfig +from jsonpath_ng.ext import parse +from typing import List +import json +import re class FilterNotFoundInResponse(ValueError): def __init__(self, msg): @@ -85,9 +84,18 @@ def _parse_json(json_data, json_filter): jsonpath_expression = parse(json_filter.replace('json:', '')) match = jsonpath_expression.find(json_data) return _get_stripped_text_from_json_match(match) + if 'jq:' in json_filter: + + try: + import jq + except ModuleNotFoundError: + # `jq` requires full compilation in windows and so isn't generally available + raise Exception("jq not support not found") + jq_expression = jq.compile(json_filter.replace('jq:', '')) match = jq_expression.input(json_data).all() + return _get_stripped_text_from_json_match(match) def _get_stripped_text_from_json_match(match):