From 0fa443c3f24d0fc2693b166dc86f74b83c5a31e0 Mon Sep 17 00:00:00 2001 From: Leigh Morresi <275001+dgtlmoon@users.noreply.github.com> Date: Mon, 15 Mar 2021 22:26:50 +0100 Subject: [PATCH] Proof of concept, view as a stream --- backend/__init__.py | 63 ++++++++++++++++++++++- backend/static/css/styles.css | 5 ++ backend/store.py | 6 +++ backend/templates/watch-diff-stream.html | 12 +++++ backend/templates/watch-overview.html | 62 +---------------------- backend/templates/watch-table.html | 64 ++++++++++++++++++++++++ 6 files changed, 150 insertions(+), 62 deletions(-) create mode 100644 backend/templates/watch-diff-stream.html create mode 100644 backend/templates/watch-table.html diff --git a/backend/__init__.py b/backend/__init__.py index 00b0ba81f..720838ca9 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -99,6 +99,8 @@ def changedetection_app(config=None, datastore_o=None): global messages limit_tag = request.args.get('tag') + rss = request.args.get('rss') + mode = request.args.get('mode') # Sort by last_changed and add the uuid which is usually the key.. sorted_watches = [] @@ -119,7 +121,63 @@ def changedetection_app(config=None, datastore_o=None): sorted_watches.sort(key=lambda x: x['last_changed'], reverse=True) existing_tags = datastore.get_all_tags() - rss = request.args.get('rss') + + if mode == 'stream': + import difflib + + import pprint + streams = [] + + extra_stylesheets = ['/static/css/diff.css'] + for watch in sorted_watches: + if not watch['viewed']: + + # get last two date keys + dates = list(watch['history'].keys()) + # Convert to int, sort and back to str again + dates = [int(i) for i in dates] + dates.sort(reverse=True) + dates = [str(i) for i in dates] + print ("OK", watch['uuid']) + + if len(dates) < 2: + print ("Skipping", watch['url']) + continue + else: + try: + path = datastore.data['watching'][watch['uuid']]['history'][str(dates[1])] + with open(path, + encoding='utf-8') as file: + txt1=[line.rstrip() for line in file.readlines()] + + path = datastore.data['watching'][watch['uuid']]['history'][str(dates[0])] + with open(path, + encoding='utf-8') as file: + txt2 = [line.rstrip() for line in file.readlines()] + except FileNotFoundError: + print ("Skipping", watch['url']) + continue + + df = list(difflib.unified_diff(txt1, txt2,n=1)) + diff_entry=[] + for line in df: + if line[0] == '-' or line[0] == '+': + diff_entry.append(line) + + + # pprint(df) + #s = pprint.pformat(df) + streams.append(diff_entry) + + + print ("###########", len(streams)) + + output = render_template("watch-diff-stream.html", + streams=streams, + extra_stylesheets=extra_stylesheets + ) + return output + if rss: fg = FeedGenerator() @@ -143,7 +201,8 @@ def changedetection_app(config=None, datastore_o=None): return response else: - output = render_template("watch-overview.html", + #table = render_template('watch-table.html', watches=sorted_watches) + output = render_template("watch-table.html", watches=sorted_watches, messages=messages, tags=existing_tags, diff --git a/backend/static/css/styles.css b/backend/static/css/styles.css index da504eef5..2f5e61533 100644 --- a/backend/static/css/styles.css +++ b/backend/static/css/styles.css @@ -266,3 +266,8 @@ footer { #new-version-text a{ color: #e07171; } + +#diff-stream { + font-size: 10px; + white-space: pre-wrap; +} diff --git a/backend/store.py b/backend/store.py index c44a40705..2e467b7e1 100644 --- a/backend/store.py +++ b/backend/store.py @@ -228,6 +228,12 @@ class ChangeDetectionStore: # result_obj from fetch_site_status.run() def save_history_text(self, uuid, result_obj, contents): + output_path = "{}/{}".format(self.datastore_path, uuid) + try: + os.mkdir(output_path) + except FileExistsError: + pass + output_path = "{}/{}".format(self.datastore_path, uuid) fname = "{}/{}-{}.stripped.txt".format(output_path, result_obj['previous_md5'], str(time.time())) with open(fname, 'w') as f: diff --git a/backend/templates/watch-diff-stream.html b/backend/templates/watch-diff-stream.html new file mode 100644 index 000000000..7dca200d5 --- /dev/null +++ b/backend/templates/watch-diff-stream.html @@ -0,0 +1,12 @@ +{% extends 'watch-overview.html' %} +{% block innercontent %} +Entries: {{ streams|length }} + +
+ {% for item in streams %} + {{ loop.index }} +{% for diff in item %}{% if diff[0] =='+' %}{{ diff }}{% endif %}{% if diff[0] =='-' %}{{ diff }}{% endif %} + {% endfor %} + {% endfor %} +
+{% endblock %} \ No newline at end of file diff --git a/backend/templates/watch-overview.html b/backend/templates/watch-overview.html index e17c4ffab..50f5ccd2a 100644 --- a/backend/templates/watch-overview.html +++ b/backend/templates/watch-overview.html @@ -24,67 +24,9 @@
- - - - - - - - - - - + {% block innercontent %} - - {% for watch in watches %} - - - - - - - - {% endfor %} - -
#Last CheckedLast Changed
{{ loop.index }}{{watch.title if watch.title is not none else watch.url}} - - {% if watch.last_error is defined and watch.last_error != False %} -
{{ watch.last_error }}
- {% endif %} - {% if not active_tag %} - {{ watch.tag}} - {% endif %} -
{{watch|format_last_checked_time}}{% if watch.history|length >= 2 and watch.last_changed %} - {{watch.last_changed|format_timestamp_timeago}} - {% else %} - Not yet - {% endif %} - - Recheck - Edit - {% if watch.history|length >= 2 %} - Diff - {% endif %} -
- + {% endblock %}
{% endblock %} \ No newline at end of file diff --git a/backend/templates/watch-table.html b/backend/templates/watch-table.html new file mode 100644 index 000000000..05bb8fdb5 --- /dev/null +++ b/backend/templates/watch-table.html @@ -0,0 +1,64 @@ +{% extends 'watch-overview.html' %} +{% block innercontent %} + + + + + + + + + + + + + + {% for watch in watches %} + + + + + + + + {% endfor %} + +
#Last CheckedLast Changed
{{ loop.index }}{{watch.title if watch.title is not none else watch.url}} + + {% if watch.last_error is defined and watch.last_error != False %} +
{{ watch.last_error }}
+ {% endif %} + {% if not active_tag %} + {{ watch.tag}} + {% endif %} +
{{watch|format_last_checked_time}}{% if watch.history|length >= 2 and watch.last_changed %} + {{watch.last_changed|format_timestamp_timeago}} + {% else %} + Not yet + {% endif %} + + Recheck + Edit + {% if watch.history|length >= 2 %} + Diff + {% endif %} +
+ +{% endblock %} \ No newline at end of file