Skip to content

Commit

Permalink
allow writing all images to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
cdzombak committed Jun 13, 2024
1 parent bfcdddd commit 62bbb79
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", {})
Expand Down
10 changes: 10 additions & 0 deletions ntfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 62bbb79

Please sign in to comment.