From 20869a13b3490097ae80f83100cee1aab357fa45 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 12 Sep 2022 12:45:29 +0200 Subject: [PATCH] tweaks to saving --- changedetectionio/fetch_processor/__init__.py | 1 - changedetectionio/fetch_processor/image.py | 4 ++-- changedetectionio/model/Watch.py | 11 ++++++++++- changedetectionio/update_worker.py | 2 +- requirements.txt | 1 + 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/changedetectionio/fetch_processor/__init__.py b/changedetectionio/fetch_processor/__init__.py index 74ae2c87..2b9a7773 100644 --- a/changedetectionio/fetch_processor/__init__.py +++ b/changedetectionio/fetch_processor/__init__.py @@ -3,7 +3,6 @@ available_fetchers = [('json_html_plaintext', 'JSON/HTML/Text'), ('image', 'Stat class fetch_processor(): contents = b'' screenshot = None - history_artifact_suffix = 'txt' """ base class for all fetch processors diff --git a/changedetectionio/fetch_processor/image.py b/changedetectionio/fetch_processor/image.py index ffe1589f..ed006601 100644 --- a/changedetectionio/fetch_processor/image.py +++ b/changedetectionio/fetch_processor/image.py @@ -80,6 +80,8 @@ class perform_site_check(fetch_processor): update_obj["last_check_status"] = fetcher.get_last_status_code() + self.contents = fetcher.raw_content + image = Image.open(io.BytesIO(fetcher.raw_content)) # @todo different choice? @@ -97,6 +99,4 @@ class perform_site_check(fetch_processor): if not watch.get('previous_md5'): watch['previous_md5'] = fetched_hash - #self.contents = fetcher.screenshot - return changed_detected, update_obj diff --git a/changedetectionio/model/Watch.py b/changedetectionio/model/Watch.py index d624bff0..bb227920 100644 --- a/changedetectionio/model/Watch.py +++ b/changedetectionio/model/Watch.py @@ -146,9 +146,18 @@ class model(dict): bump = self.history return self.__newest_history_key - def save_history_artifact(self, contents: bytes, timestamp, suffix='txt'): + def save_history_artifact(self, contents: bytes, timestamp): import uuid import logging + import magic + import re + suffix = 'bin' + # detect extension type + mtype = magic.from_buffer(contents, mime=True) + if mtype: + r = re.search(r'image/(\w+)', mtype, re.IGNORECASE) + if r: + suffix = r.group(1) output_path = "{}/{}".format(self.__datastore_path, self['uuid']) diff --git a/changedetectionio/update_worker.py b/changedetectionio/update_worker.py index 072de2f6..696be7d4 100644 --- a/changedetectionio/update_worker.py +++ b/changedetectionio/update_worker.py @@ -270,7 +270,7 @@ class update_worker(threading.Thread): # For the FIRST time we check a site, or a change detected, save the snapshot. if changed_detected or not watch['last_checked']: # A change was detected - watch.save_history_artifact(contents=update_handler.contents, timestamp=str(round(time.time())), suffix=update_handler.history_artifact_suffix) + watch.save_history_artifact(contents=update_handler.contents, timestamp=str(round(time.time()))) self.datastore.update_watch(uuid=uuid, update_obj=update_obj) diff --git a/requirements.txt b/requirements.txt index c03ae724..55b0947e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -46,3 +46,4 @@ werkzeug ~= 2.0.0 imagehash ~= 4.3.0 pillow +python-magic