diff --git a/changedetectionio/__init__.py b/changedetectionio/__init__.py
index 08b3b5e49..b81309d77 100644
--- a/changedetectionio/__init__.py
+++ b/changedetectionio/__init__.py
@@ -340,8 +340,6 @@ def changedetection_app(config=None, datastore_o=None):
if len(dates) < 2:
continue
- prev_fname = watch.history[dates[-2]]
-
if not watch.viewed:
# Re #239 - GUID needs to be individual for each event
# @todo In the future make this a configurable link back (see work on BASE_URL https://github.com/dgtlmoon/changedetection.io/pull/228)
@@ -362,9 +360,12 @@ def changedetection_app(config=None, datastore_o=None):
watch_title = watch.get('title') if watch.get('title') else watch.get('url')
fe.title(title=watch_title)
- latest_fname = watch.history[dates[-1]]
- html_diff = diff.render_diff(prev_fname, latest_fname, include_equal=False, line_feed_sep="
")
+ html_diff = diff.render_diff(previous_version_file_contents=watch.get_history_snapshot(dates[-2]),
+ newest_version_file_contents=watch.get_history_snapshot(dates[-1]),
+ include_equal=False,
+ line_feed_sep="
")
+
fe.content(content="
{}
{}".format(watch_title, html_diff),
type='CDATA')
diff --git a/changedetectionio/api/api_v1.py b/changedetectionio/api/api_v1.py
index e28972d64..b30c0d63b 100644
--- a/changedetectionio/api/api_v1.py
+++ b/changedetectionio/api/api_v1.py
@@ -179,9 +179,7 @@ class WatchSingleHistory(Resource):
if timestamp == 'latest':
timestamp = list(watch.history.keys())[-1]
- # @todo - Check for UTF-8 compatability
- with open(watch.history[timestamp], 'r') as f:
- content = f.read()
+ content = watch.get_history_snapshot(timestamp)
response = make_response(content, 200)
response.mimetype = "text/plain"
diff --git a/changedetectionio/diff.py b/changedetectionio/diff.py
index e21bfa697..2b566ffc3 100644
--- a/changedetectionio/diff.py
+++ b/changedetectionio/diff.py
@@ -31,14 +31,11 @@ def customSequenceMatcher(before, after, include_equal=False, include_removed=Tr
# only_differences - only return info about the differences, no context
# line_feed_sep could be "
" or "" or "\n" etc
-def render_diff(previous_file, newest_file, include_equal=False, include_removed=True, include_added=True, line_feed_sep="\n"):
- with open(newest_file, 'r') as f:
- newest_version_file_contents = f.read()
- newest_version_file_contents = [line.rstrip() for line in newest_version_file_contents.splitlines()]
+def render_diff(previous_version_file_contents, newest_version_file_contents, include_equal=False, include_removed=True, include_added=True, line_feed_sep="\n"):
- if previous_file:
- with open(previous_file, 'r') as f:
- previous_version_file_contents = f.read()
+ newest_version_file_contents = [line.rstrip() for line in newest_version_file_contents.splitlines()]
+
+ if previous_version_file_contents:
previous_version_file_contents = [line.rstrip() for line in previous_version_file_contents.splitlines()]
else:
previous_version_file_contents = ""
diff --git a/changedetectionio/model/Watch.py b/changedetectionio/model/Watch.py
index 6b78227e4..cbbce8eec 100644
--- a/changedetectionio/model/Watch.py
+++ b/changedetectionio/model/Watch.py
@@ -412,8 +412,8 @@ class model(dict):
# self.history will be keyed with the full path
for k, fname in self.history.items():
if os.path.isfile(fname):
- with open(fname, "r") as f:
- contents = f.read()
+ if True:
+ contents = self.get_history_snapshot(k)
res = re.findall(regex, contents, re.MULTILINE)
if res:
if not csv_writer:
diff --git a/changedetectionio/tests/unit/test_notification_diff.py b/changedetectionio/tests/unit/test_notification_diff.py
index 80f383a16..a6b7067fb 100755
--- a/changedetectionio/tests/unit/test_notification_diff.py
+++ b/changedetectionio/tests/unit/test_notification_diff.py
@@ -13,21 +13,33 @@ class TestDiffBuilder(unittest.TestCase):
def test_expected_diff_output(self):
base_dir = os.path.dirname(__file__)
- output = diff.render_diff(previous_file=base_dir + "/test-content/before.txt", newest_file=base_dir + "/test-content/after.txt")
+ with open(base_dir + "/test-content/before.txt", 'r') as f:
+ previous_version_file_contents = f.read()
+
+ with open(base_dir + "/test-content/after.txt", 'r') as f:
+ newest_version_file_contents = f.read()
+
+ output = diff.render_diff(previous_version_file_contents, newest_version_file_contents)
output = output.split("\n")
self.assertIn('(changed) ok', output)
self.assertIn('(into) xok', output)
self.assertIn('(into) next-x-ok', output)
self.assertIn('(added) and something new', output)
-
- output = diff.render_diff(previous_file=base_dir + "/test-content/before.txt", newest_file=base_dir + "/test-content/after-2.txt")
+ with open(base_dir + "/test-content/after-2.txt", 'r') as f:
+ newest_version_file_contents = f.read()
+ output = diff.render_diff(previous_version_file_contents, newest_version_file_contents)
output = output.split("\n")
self.assertIn('(removed) for having learned computerese,', output)
self.assertIn('(removed) I continue to examine bits, bytes and words', output)
#diff_removed
- output = diff.render_diff(previous_file=base_dir + "/test-content/before.txt", newest_file=base_dir + "/test-content/after.txt", include_equal=False, include_removed=True, include_added=False)
+ with open(base_dir + "/test-content/before.txt", 'r') as f:
+ previous_version_file_contents = f.read()
+
+ with open(base_dir + "/test-content/after.txt", 'r') as f:
+ newest_version_file_contents = f.read()
+ output = diff.render_diff(previous_version_file_contents, newest_version_file_contents, include_equal=False, include_removed=True, include_added=False)
output = output.split("\n")
self.assertIn('(changed) ok', output)
self.assertIn('(into) xok', output)
@@ -35,7 +47,9 @@ class TestDiffBuilder(unittest.TestCase):
self.assertNotIn('(added) and something new', output)
#diff_removed
- output = diff.render_diff(previous_file=base_dir + "/test-content/before.txt", newest_file=base_dir + "/test-content/after-2.txt", include_equal=False, include_removed=True, include_added=False)
+ with open(base_dir + "/test-content/after-2.txt", 'r') as f:
+ newest_version_file_contents = f.read()
+ output = diff.render_diff(previous_version_file_contents, newest_version_file_contents, include_equal=False, include_removed=True, include_added=False)
output = output.split("\n")
self.assertIn('(removed) for having learned computerese,', output)
self.assertIn('(removed) I continue to examine bits, bytes and words', output)
diff --git a/changedetectionio/update_worker.py b/changedetectionio/update_worker.py
index 2a7479dd3..279705467 100644
--- a/changedetectionio/update_worker.py
+++ b/changedetectionio/update_worker.py
@@ -69,18 +69,17 @@ class update_worker(threading.Thread):
else:
line_feed_sep = "\n"
- with open(watch_history[dates[-1]], 'rb') as f:
- snapshot_contents = f.read()
+ snapshot_contents = watch.get_history_snapshot(dates[-1])
n_object.update({
'watch_url': watch['url'],
'uuid': watch_uuid,
'screenshot': watch.get_screenshot() if watch.get('notification_screenshot') else None,
'current_snapshot': snapshot_contents.decode('utf-8'),
- 'diff': diff.render_diff(watch_history[dates[-2]], watch_history[dates[-1]], line_feed_sep=line_feed_sep),
- 'diff_added': diff.render_diff(watch_history[dates[-2]], watch_history[dates[-1]], include_removed=False, line_feed_sep=line_feed_sep),
- 'diff_removed': diff.render_diff(watch_history[dates[-2]], watch_history[dates[-1]], include_added=False, line_feed_sep=line_feed_sep),
- 'diff_full': diff.render_diff(watch_history[dates[-2]], watch_history[dates[-1]], include_equal=True, line_feed_sep=line_feed_sep)
+ 'diff': diff.render_diff(watch.get_history_snapshot(dates[-2]), watch.get_history_snapshot(dates[-1]), line_feed_sep=line_feed_sep),
+ 'diff_added': diff.render_diff(watch.get_history_snapshot(dates[-2]), watch.get_history_snapshot(dates[-1]), include_removed=False, line_feed_sep=line_feed_sep),
+ 'diff_removed': diff.render_diff(watch.get_history_snapshot(dates[-2]), watch.get_history_snapshot(dates[-1]), include_added=False, line_feed_sep=line_feed_sep),
+ 'diff_full': diff.render_diff(watch.get_history_snapshot(dates[-2]), watch.get_history_snapshot(dates[-1]), include_equal=True, line_feed_sep=line_feed_sep)
})
logging.info (">> SENDING NOTIFICATION")
self.notification_q.put(n_object)