From 0855017dcaa632a452e468d3631083fe07f724ea Mon Sep 17 00:00:00 2001 From: Leigh Morresi <275001+dgtlmoon@users.noreply.github.com> Date: Fri, 26 Feb 2021 16:52:14 +0100 Subject: [PATCH 01/28] Validation of added headers, should contain key/val (2 parts) --- backend/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/__init__.py b/backend/__init__.py index 101e0094..530e5570 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -162,13 +162,15 @@ def changedetection_app(config=None, datastore_o=None): url = request.form.get('url').strip() tag = request.form.get('tag').strip() + # Extra headers form_headers = request.form.get('headers').strip().split("\n") extra_headers = {} if form_headers: for header in form_headers: if len(header): parts = header.split(':', 1) - extra_headers.update({parts[0].strip(): parts[1].strip()}) + if len(parts) == 2: + extra_headers.update({parts[0].strip(): parts[1].strip()}) validators.url(url) # @todo switch to prop/attr/observer datastore.data['watching'][uuid].update({'url': url, From 468184bc3acbd201db75e03af48752c03f13b6f5 Mon Sep 17 00:00:00 2001 From: Leigh Morresi <275001+dgtlmoon@users.noreply.github.com> Date: Fri, 26 Feb 2021 20:07:26 +0100 Subject: [PATCH 02/28] Issue #14 - Tweaks to edit, create ignore text, tests for ignore text, integrate ignore text --- backend/__init__.py | 34 +++++-- backend/fetch_site_status.py | 20 +++- backend/pytest.ini | 4 +- backend/store.py | 3 +- backend/templates/edit.html | 20 +++- backend/templates/watch-overview.html | 5 +- backend/tests/conftest.py | 8 +- backend/tests/test_ignore_text.py | 135 ++++++++++++++++++++++++++ 8 files changed, 213 insertions(+), 16 deletions(-) create mode 100644 backend/tests/test_ignore_text.py diff --git a/backend/__init__.py b/backend/__init__.py index 530e5570..d46a06fb 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -151,13 +151,16 @@ def changedetection_app(config=None, datastore_o=None): return render_template("scrub.html") - @app.route("/edit", methods=['GET', 'POST']) - def edit_page(): + @app.route("/edit/", methods=['GET', 'POST']) + def edit_page(uuid): global messages import validators + # More for testing, possible to return the first/only + if uuid == 'first': + uuid = list(datastore.data['watching'].keys()).pop() + if request.method == 'POST': - uuid = request.args.get('uuid') url = request.form.get('url').strip() tag = request.form.get('tag').strip() @@ -172,10 +175,27 @@ def changedetection_app(config=None, datastore_o=None): if len(parts) == 2: extra_headers.update({parts[0].strip(): parts[1].strip()}) + update_obj = {'url': url, + 'tag': tag, + 'headers': extra_headers + } + + # Ignore text + form_ignore_text = request.form.get('ignore-text').strip() + ignore_text = [] + if form_ignore_text: + for text in form_ignore_text.split("\n"): + text = text.strip() + if len(text): + ignore_text.append(text) + + # Reset the previous_md5 so we process a new snapshot including stripping ignore text. + update_obj['previous_md5'] = "" + + update_obj['ignore_text'] = ignore_text + validators.url(url) # @todo switch to prop/attr/observer - datastore.data['watching'][uuid].update({'url': url, - 'tag': tag, - 'headers': extra_headers}) + datastore.data['watching'][uuid].update(update_obj) datastore.needs_write = True messages.append({'class': 'ok', 'message': 'Updated watch.'}) @@ -183,8 +203,6 @@ def changedetection_app(config=None, datastore_o=None): return redirect(url_for('index')) else: - - uuid = request.args.get('uuid') output = render_template("edit.html", uuid=uuid, watch=datastore.data['watching'][uuid], messages=messages) return output diff --git a/backend/fetch_site_status.py b/backend/fetch_site_status.py index c3e557e0..5c0d98ef 100644 --- a/backend/fetch_site_status.py +++ b/backend/fetch_site_status.py @@ -11,6 +11,15 @@ class perform_site_check(): super().__init__(*args, **kwargs) self.datastore = datastore + def strip_ignore_text(self, content, list_ignore_text): + + output=[] + for line in content.splitlines(): + if not any(skip_text in line for skip_text in list_ignore_text): + output.append(line) + + return "\n".join(output) + def run(self, uuid): timestamp = int(time.time()) # used for storage etc too stripped_text_from_html = False @@ -76,7 +85,16 @@ class perform_site_check(): if not len(r.text): update_obj["last_error"] = "Empty reply" - fetched_md5 = hashlib.md5(stripped_text_from_html.encode('utf-8')).hexdigest() + content = stripped_text_from_html.encode('utf-8') + + # If there's text to skip + # @todo we could abstract out the get_text() to handle this cleaner + if len(self.datastore.data['watching'][uuid]['ignore_text']): + content = self.strip_ignore_text(content, self.datastore.data['watching'][uuid]['ignore_text']) + + fetched_md5 = hashlib.md5(content).hexdigest() + + # could be None or False depending on JSON type if self.datastore.data['watching'][uuid]['previous_md5'] != fetched_md5: diff --git a/backend/pytest.ini b/backend/pytest.ini index 8b9ccf85..7716524b 100644 --- a/backend/pytest.ini +++ b/backend/pytest.ini @@ -1,2 +1,4 @@ [pytest] -addopts = --no-start-live-server --live-server-port=5005 \ No newline at end of file +addopts = --no-start-live-server --live-server-port=5005 +live_server_scope = function + diff --git a/backend/store.py b/backend/store.py index d36f4b25..b5e936ec 100644 --- a/backend/store.py +++ b/backend/store.py @@ -53,7 +53,8 @@ class ChangeDetectionStore: 'previous_md5': "", 'uuid': str(uuid_builder.uuid4()), 'headers': {}, # Extra headers to send - 'history': {} # Dict of timestamp and output stripped filename + 'history': {}, # Dict of timestamp and output stripped filename + 'ignore_text': [] # List of text to ignore when calculating the comparison checksum } if path.isfile('/source.txt'): diff --git a/backend/templates/edit.html b/backend/templates/edit.html index 89c2bfd6..525d3b27 100644 --- a/backend/templates/edit.html +++ b/backend/templates/edit.html @@ -18,10 +18,26 @@ Grouping tags, can be a comma separated list. + +
+ + + + Each line will be processed separately as an ignore rule. + +
+ +
-