From a9003d574e828c83c385e29b6d211214e5b2feb7 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Wed, 9 Apr 2025 09:04:36 +0200 Subject: [PATCH 1/2] Requests fetcher - remove old screenshot when watch was in a different fetcher type --- changedetectionio/content_fetchers/base.py | 2 +- changedetectionio/content_fetchers/requests.py | 11 +++++++++++ .../content_fetchers/webdriver_selenium.py | 2 +- changedetectionio/processors/__init__.py | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/changedetectionio/content_fetchers/base.py b/changedetectionio/content_fetchers/base.py index c764f77e0a5..a482fbdc92c 100644 --- a/changedetectionio/content_fetchers/base.py +++ b/changedetectionio/content_fetchers/base.py @@ -87,7 +87,7 @@ def run(self, pass @abstractmethod - def quit(self): + def quit(self, watch=None): return @abstractmethod diff --git a/changedetectionio/content_fetchers/requests.py b/changedetectionio/content_fetchers/requests.py index 2519aa9567a..702283dfda1 100644 --- a/changedetectionio/content_fetchers/requests.py +++ b/changedetectionio/content_fetchers/requests.py @@ -96,3 +96,14 @@ def run(self, self.raw_content = r.content + + def quit(self, watch=None): + screenshot = watch.get_screenshot() + # In case they switched to `requests` fetcher from something else + # Then the screenshot could be old, in any case, it's not used here. + if screenshot: + try: + os.unlink(screenshot) + except Exception as e: + logger.warning(f"Failed to unlink screenshot: {screenshot} - {e}") + diff --git a/changedetectionio/content_fetchers/webdriver_selenium.py b/changedetectionio/content_fetchers/webdriver_selenium.py index 72e80b1592a..eb5cec56f71 100644 --- a/changedetectionio/content_fetchers/webdriver_selenium.py +++ b/changedetectionio/content_fetchers/webdriver_selenium.py @@ -112,7 +112,7 @@ def is_ready(self): self.quit() return True - def quit(self): + def quit(self, watch=None): if self.driver: try: self.driver.quit() diff --git a/changedetectionio/processors/__init__.py b/changedetectionio/processors/__init__.py index adf08a18a9f..1dd703baa2c 100644 --- a/changedetectionio/processors/__init__.py +++ b/changedetectionio/processors/__init__.py @@ -159,7 +159,7 @@ def call_browser(self, preferred_proxy_id=None): ) #@todo .quit here could go on close object, so we can run JS if change-detected - self.fetcher.quit() + self.fetcher.quit(watch=self.watch) # After init, call run_changedetection() which will do the actual change-detection From 09b32d4ebffad458e09bbd358e6f05c99e5d5cb8 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Wed, 9 Apr 2025 09:27:39 +0200 Subject: [PATCH 2/2] Skip removing old screenshots for notification tests --- changedetectionio/content_fetchers/requests.py | 15 +++++++++------ changedetectionio/run_basic_tests.sh | 7 ++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/changedetectionio/content_fetchers/requests.py b/changedetectionio/content_fetchers/requests.py index 702283dfda1..fe66d869fc6 100644 --- a/changedetectionio/content_fetchers/requests.py +++ b/changedetectionio/content_fetchers/requests.py @@ -98,12 +98,15 @@ def run(self, self.raw_content = r.content def quit(self, watch=None): - screenshot = watch.get_screenshot() + # In case they switched to `requests` fetcher from something else # Then the screenshot could be old, in any case, it's not used here. - if screenshot: - try: - os.unlink(screenshot) - except Exception as e: - logger.warning(f"Failed to unlink screenshot: {screenshot} - {e}") + # REMOVE_REQUESTS_OLD_SCREENSHOTS - Mainly used for testing + if strtobool(os.getenv("REMOVE_REQUESTS_OLD_SCREENSHOTS", 'true')): + screenshot = watch.get_screenshot() + if screenshot: + try: + os.unlink(screenshot) + except Exception as e: + logger.warning(f"Failed to unlink screenshot: {screenshot} - {e}") diff --git a/changedetectionio/run_basic_tests.sh b/changedetectionio/run_basic_tests.sh index 38bcd6032d1..7e13b76620e 100755 --- a/changedetectionio/run_basic_tests.sh +++ b/changedetectionio/run_basic_tests.sh @@ -14,7 +14,8 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) find tests/test_*py -type f|while read test_name do echo "TEST RUNNING $test_name" - pytest $test_name + # REMOVE_REQUESTS_OLD_SCREENSHOTS disabled so that we can write a screenshot and send it in test_notifications.py without a real browser + REMOVE_REQUESTS_OLD_SCREENSHOTS=false pytest $test_name done echo "RUNNING WITH BASE_URL SET" @@ -22,7 +23,7 @@ echo "RUNNING WITH BASE_URL SET" # Now re-run some tests with BASE_URL enabled # Re #65 - Ability to include a link back to the installation, in the notification. export BASE_URL="https://really-unique-domain.io" -pytest tests/test_notification.py +REMOVE_REQUESTS_OLD_SCREENSHOTS=false pytest tests/test_notification.py # Re-run with HIDE_REFERER set - could affect login @@ -32,7 +33,7 @@ pytest tests/test_access_control.py # Re-run a few tests that will trigger brotli based storage export SNAPSHOT_BROTLI_COMPRESSION_THRESHOLD=5 pytest tests/test_access_control.py -pytest tests/test_notification.py +REMOVE_REQUESTS_OLD_SCREENSHOTS=false pytest tests/test_notification.py pytest tests/test_backend.py pytest tests/test_rss.py pytest tests/test_unique_lines.py