From f00e4b8b6e023446deac4512447486395ad8528f Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Sun, 21 Jul 2024 17:31:57 +0200 Subject: [PATCH] UI - Adding "Download latest HTML snapshot" from Edit Watch > Stats page for easier debugging --- changedetectionio/flask_app.py | 24 ++++++++++++++++++++++++ changedetectionio/templates/edit.html | 6 ++++++ changedetectionio/tests/test_backend.py | 5 +++++ 3 files changed, 35 insertions(+) diff --git a/changedetectionio/flask_app.py b/changedetectionio/flask_app.py index 19799450..6fddc361 100644 --- a/changedetectionio/flask_app.py +++ b/changedetectionio/flask_app.py @@ -1369,6 +1369,30 @@ def changedetection_app(config=None, datastore_o=None): except FileNotFoundError: abort(404) + @app.route("/edit//get-html", methods=['GET']) + @login_optionally_required + def watch_get_latest_html(uuid): + from io import BytesIO + from flask import send_file + import brotli + + watch = datastore.data['watching'].get(uuid) + if watch and os.path.isdir(watch.watch_data_dir): + latest_filename = list(watch.history.keys())[0] + html_fname = os.path.join(watch.watch_data_dir, f"{latest_filename}.html.br") + if html_fname.endswith('.br'): + # Read and decompress the Brotli file + with open(html_fname, 'rb') as f: + decompressed_data = brotli.decompress(f.read()) + + buffer = BytesIO(decompressed_data) + + return send_file(buffer, as_attachment=True, download_name=f"{latest_filename}.html", mimetype='text/html') + + + # Return a 500 error + abort(500) + @app.route("/form/add/quickwatch", methods=['POST']) @login_optionally_required def form_quick_watch_add(): diff --git a/changedetectionio/templates/edit.html b/changedetectionio/templates/edit.html index 4b9a7fc1..e81035e8 100644 --- a/changedetectionio/templates/edit.html +++ b/changedetectionio/templates/edit.html @@ -479,6 +479,12 @@ Unavailable") }} + {% if watch.history_n %} +

+ Download latest HTML snapshot +

+ {% endif %} +
diff --git a/changedetectionio/tests/test_backend.py b/changedetectionio/tests/test_backend.py index f7cea925..f63e6712 100644 --- a/changedetectionio/tests/test_backend.py +++ b/changedetectionio/tests/test_backend.py @@ -150,6 +150,11 @@ def test_check_basic_change_detection_functionality(client, live_server, measure res = client.get(url_for("index")) assert b'preview/' in res.data + + # Check the 'get latest snapshot works' + res = client.get(url_for("watch_get_latest_html", uuid=uuid)) + assert b'head title' in res.data + # # Cleanup everything res = client.get(url_for("form_delete", uuid="all"), follow_redirects=True)