From 85897e0bf9315d4a5af2d5a4aa17deec4df33821 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 17 Oct 2022 17:40:28 +0200 Subject: [PATCH] Windows - diff file handling improvements (#1031) --- changedetectionio/__init__.py | 10 ++++++---- changedetectionio/model/Watch.py | 11 ++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/changedetectionio/__init__.py b/changedetectionio/__init__.py index c8d8c52f..096070ef 100644 --- a/changedetectionio/__init__.py +++ b/changedetectionio/__init__.py @@ -816,9 +816,11 @@ def changedetection_app(config=None, datastore_o=None): newest_file = history[dates[-1]] + # Read as binary and force decode as UTF-8 + # Windows may fail decode in python if we just use 'r' mode (chardet decode exception) try: - with open(newest_file, 'r') as f: - newest_version_file_contents = f.read() + with open(newest_file, 'rb') as f: + newest_version_file_contents = f.read().decode('utf-8') except Exception as e: newest_version_file_contents = "Unable to read {}.\n".format(newest_file) @@ -830,8 +832,8 @@ def changedetection_app(config=None, datastore_o=None): previous_file = history[dates[-2]] try: - with open(previous_file, 'r') as f: - previous_version_file_contents = f.read() + with open(previous_file, 'rb') as f: + previous_version_file_contents = f.read().decode('utf-8') except Exception as e: previous_version_file_contents = "Unable to read {}.\n".format(previous_file) diff --git a/changedetectionio/model/Watch.py b/changedetectionio/model/Watch.py index b7aaca86..9a87ad71 100644 --- a/changedetectionio/model/Watch.py +++ b/changedetectionio/model/Watch.py @@ -151,28 +151,29 @@ class model(dict): import uuid import logging - output_path = "{}/{}".format(self.__datastore_path, self['uuid']) + output_path = os.path.join(self.__datastore_path, self['uuid']) self.ensure_data_dir_exists() + snapshot_fname = os.path.join(output_path, str(uuid.uuid4())) - snapshot_fname = "{}/{}.stripped.txt".format(output_path, uuid.uuid4()) logging.debug("Saving history text {}".format(snapshot_fname)) + # in /diff/ we are going to assume for now that it's UTF-8 when reading with open(snapshot_fname, 'wb') as f: f.write(contents) f.close() # Append to index # @todo check last char was \n - index_fname = "{}/history.txt".format(output_path) + index_fname = os.path.join(output_path, "history.txt") with open(index_fname, 'a') as f: f.write("{},{}\n".format(timestamp, snapshot_fname)) f.close() self.__newest_history_key = timestamp - self.__history_n+=1 + self.__history_n += 1 - #@todo bump static cache of the last timestamp so we dont need to examine the file to set a proper ''viewed'' status + # @todo bump static cache of the last timestamp so we dont need to examine the file to set a proper ''viewed'' status return snapshot_fname @property