From 62bbb79e9b3af7670c42c1207d2e34d89915ea30 Mon Sep 17 00:00:00 2001 From: Chris Dzombak Date: Thu, 13 Jun 2024 15:45:04 -0400 Subject: [PATCH] allow writing all images to disk --- README.md | 1 + config.py | 7 +++++++ ntfy.py | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/README.md b/README.md index d2d664f..3fb2a8d 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,7 @@ The file is a single JSON object containing the following keys, or a subset ther - `debounce_threshold_s`: Specifies the number of seconds to wait after a notification before sending another one for the same type of object. - `default_priority`: Default priority for notifications. ([See Ntfy docs on Message Priority](https://docs.ntfy.sh/publish/#message-priority).) - `image_method`: Method for adding images to notifications. By default, the image URL is added both as a "click" action and as an attachment. Set this to `click` or `attach` to use only one of those methods. + - `images_cc_dir`: Directory to which notification images are written. This is optional; if not set, nothing is saved to disk. - `priorities`: Map of `classification name` → `priority`. Allows customizing notification priority for specific object types. - `req_timeout_s`: Request timeout for sending notifications. - `server`: The Ntfy server to send notifications to. diff --git a/config.py b/config.py index 4eec0ae..a73ec61 100644 --- a/config.py +++ b/config.py @@ -137,6 +137,13 @@ def config_from_file( raise ConfigValidationError( "notifier.image_method must be one of " f"{IMAGE_ATTACH}, {IMAGE_CLICK}" ) + cfg.notifier.images_cc_dir = ntfy_cfg_dict.get( + "images_cc_dir", cfg.notifier.images_cc_dir + ) + if cfg.notifier.images_cc_dir is not None and not isinstance( + cfg.notifier.images_cc_dir, str + ): + raise ConfigValidationError("notifier.images_cc_dir must be a string") # tracker: tracker_cfg_dict = cfg_dict.get("tracker", {}) diff --git a/ntfy.py b/ntfy.py index f97261d..6b7200d 100644 --- a/ntfy.py +++ b/ntfy.py @@ -2,6 +2,7 @@ import datetime import logging import multiprocessing +import os.path from abc import ABC from enum import Enum from typing import Dict, Optional, Final @@ -37,6 +38,7 @@ class NtfyConfig: priorities: Dict[str, str] = dataclasses.field(default_factory=lambda: {}) req_timeout_s: float = 10.0 image_method: Optional[str] = None + images_cc_dir: Optional[str] = None class Notification(ABC): @@ -234,6 +236,14 @@ def _run(self): logger.debug("received notification: " + n.message()) if isinstance(n, ObjectNotification): + if n.jpeg_image and self._config.images_cc_dir: + try: + dst_fname = f"{n.id}.jpg" + dst_path = os.path.join(self._config.images_cc_dir, dst_fname) + with open(dst_path, "wb") as f: + f.write(n.jpeg_image) + except Exception as e: + logger.error(f"error writing image to disk: {e}") if self._suppress(logger, n): continue self._records[n.id] = NtfyRecord(