From f07ff9b55e5a4714ce9324d6ef0ba99997d0e486 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Tue, 9 Jul 2024 15:35:19 +0200 Subject: [PATCH] UI - Visual Selector should still update when elements were not found (#2476) --- .../content_fetchers/exceptions/__init__.py | 3 ++- changedetectionio/processors/text_json_diff.py | 10 ++++++---- changedetectionio/update_worker.py | 10 +++++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/changedetectionio/content_fetchers/exceptions/__init__.py b/changedetectionio/content_fetchers/exceptions/__init__.py index 00752a3e..9552b838 100644 --- a/changedetectionio/content_fetchers/exceptions/__init__.py +++ b/changedetectionio/content_fetchers/exceptions/__init__.py @@ -87,11 +87,12 @@ class ScreenshotUnavailable(Exception): class ReplyWithContentButNoText(Exception): - def __init__(self, status_code, url, screenshot=None, has_filters=False, html_content=''): + def __init__(self, status_code, url, screenshot=None, has_filters=False, html_content='', xpath_data=None): # Set this so we can use it in other parts of the app self.status_code = status_code self.url = url self.screenshot = screenshot self.has_filters = has_filters self.html_content = html_content + self.xpath_data = xpath_data return diff --git a/changedetectionio/processors/text_json_diff.py b/changedetectionio/processors/text_json_diff.py index e793de89..797b6c2b 100644 --- a/changedetectionio/processors/text_json_diff.py +++ b/changedetectionio/processors/text_json_diff.py @@ -19,8 +19,9 @@ description = 'Detects all text changes where possible' json_filter_prefixes = ['json:', 'jq:', 'jqraw:'] class FilterNotFoundInResponse(ValueError): - def __init__(self, msg, screenshot=None): + def __init__(self, msg, screenshot=None, xpath_data=None): self.screenshot = screenshot + self.xpath_data = xpath_data ValueError.__init__(self, msg) @@ -185,7 +186,7 @@ class perform_site_check(difference_detection_processor): append_pretty_line_formatting=not watch.is_source_type_url) if not html_content.strip(): - raise FilterNotFoundInResponse(msg=include_filters_rule, screenshot=self.fetcher.screenshot) + raise FilterNotFoundInResponse(msg=include_filters_rule, screenshot=self.fetcher.screenshot, xpath_data=self.fetcher.xpath_data) if has_subtractive_selectors: html_content = html_tools.element_removal(subtractive_selectors, html_content) @@ -243,9 +244,10 @@ class perform_site_check(difference_detection_processor): if not is_json and not empty_pages_are_a_change and len(stripped_text_from_html.strip()) == 0: raise content_fetchers.exceptions.ReplyWithContentButNoText(url=url, status_code=self.fetcher.get_last_status_code(), - screenshot=screenshot, + screenshot=self.fetcher.screenshot, has_filters=has_filter_rule, - html_content=html_content + html_content=html_content, + xpath_data=self.fetcher.xpath_data ) # We rely on the actual text in the html output.. many sites have random script vars etc, diff --git a/changedetectionio/update_worker.py b/changedetectionio/update_worker.py index daa0f788..91453c47 100644 --- a/changedetectionio/update_worker.py +++ b/changedetectionio/update_worker.py @@ -314,6 +314,9 @@ class update_worker(threading.Thread): if e.screenshot: watch.save_screenshot(screenshot=e.screenshot, as_error=True) + if e.xpath_data: + watch.save_xpath_data(data=e.xpath_data) + process_changedetection_results = False except content_fetchers.exceptions.Non200ErrorCodeReceived as e: @@ -344,8 +347,13 @@ class update_worker(threading.Thread): err_text = "Warning, no filters were found, no change detection ran - Did the page change layout? update your Visual Filter if necessary." self.datastore.update_watch(uuid=uuid, update_obj={'last_error': err_text}) + + # Filter wasnt found, but we should still update the visual selector so that they can have a chance to set it up again if e.screenshot: - watch.save_screenshot(screenshot=e.screenshot, as_error=True) + watch.save_screenshot(screenshot=e.screenshot) + + if e.xpath_data: + watch.save_xpath_data(data=e.xpath_data) # Only when enabled, send the notification if watch.get('filter_failure_notification_send', False):