modify notification module config

This commit is contained in:
maxDorninger
2025-07-10 23:48:29 +02:00
parent 8a89a24f25
commit d986f91e5e
6 changed files with 52 additions and 22 deletions

View File

@@ -1,3 +1,4 @@
import os
from pathlib import Path
from pydantic import AnyHttpUrl
@@ -27,7 +28,7 @@ class BasicConfig(BaseSettings):
class AllEncompassingConfig(BaseSettings):
model_config = SettingsConfigDict(toml_file="config.toml")
model_config = SettingsConfigDict(toml_file=os.getenv("CONFIG_FILE", "./config.toml"))
"""
This class is used to load all configurations from the environment variables.
It combines the BasicConfig with any additional configurations needed.

View File

@@ -1,5 +1,10 @@
from pydantic_settings import BaseSettings
from media_manager.notification.service_providers.email import EmailNotificationsConfig
from media_manager.notification.service_providers.gotify import GotifyConfig
from media_manager.notification.service_providers.ntfy import NtfyConfig
from media_manager.notification.service_providers.pushover import PushoverConfig
class EmailConfig(BaseSettings):
smtp_host: str = ""
@@ -12,16 +17,10 @@ class EmailConfig(BaseSettings):
class NotificationConfig(BaseSettings):
smtp_config: EmailConfig = EmailConfig()
email: str | None = None # the email address to send notifications to
email_notifications: EmailNotificationsConfig = EmailNotificationsConfig()
gotify: GotifyConfig = GotifyConfig()
ntfy: NtfyConfig = NtfyConfig()
pushover: PushoverConfig = PushoverConfig()
ntfy_url: str | None = (
None # e.g. https://ntfy.sh/your-topic (note lack of trailing slash)
)
pushover_api_key: str | None = None
pushover_user: str | None = None
gotify_api_key: str | None = None
gotify_url: str | None = (
None # e.g. https://gotify.example.com (note lack of trailing slash)
)

View File

@@ -1,3 +1,5 @@
from pydantic_settings import BaseSettings
import media_manager.notification.utils
from media_manager.notification.schemas import MessageNotification
from media_manager.notification.service_providers.abstractNotificationServiceProvider import (
@@ -5,10 +7,14 @@ from media_manager.notification.service_providers.abstractNotificationServicePro
)
from media_manager.config import AllEncompassingConfig
class EmailNotificationsConfig(BaseSettings):
enabled: bool = False
emails: list[str] = [] # the email addresses to send notifications to
class EmailNotificationServiceProvider(AbstractNotificationServiceProvider):
def __init__(self):
self.config = AllEncompassingConfig().notifications
self.config = AllEncompassingConfig().notifications.email_notifications
def send_notification(self, message: MessageNotification) -> bool:
subject = "MediaManager - " + message.title
@@ -23,7 +29,10 @@ class EmailNotificationServiceProvider(AbstractNotificationServiceProvider):
</body>
</html>
"""
media_manager.notification.utils.send_email(
subject=subject, html=html, addressee=self.config.email
)
for email in self.config.emails:
media_manager.notification.utils.send_email(
subject=subject, html=html, addressee=email
)
return True

View File

@@ -1,10 +1,18 @@
import requests
from pydantic_settings import BaseSettings
from media_manager.config import AllEncompassingConfig
from media_manager.notification.schemas import MessageNotification
from media_manager.notification.service_providers.abstractNotificationServiceProvider import (
AbstractNotificationServiceProvider,
)
class GotifyConfig(BaseSettings):
enabled: bool = False
api_key: str | None = None
url: str | None = (
None # e.g. https://gotify.example.com (note lack of trailing slash)
)
class GotifyNotificationServiceProvider(AbstractNotificationServiceProvider):
"""
@@ -12,11 +20,11 @@ class GotifyNotificationServiceProvider(AbstractNotificationServiceProvider):
"""
def __init__(self):
self.config = AllEncompassingConfig().notifications
self.config = AllEncompassingConfig().notifications.gotify
def send_notification(self, message: MessageNotification) -> bool:
response = requests.post(
url=f"{self.config.gotify_url}/message?token={self.config.gotify_api_key}",
url=f"{self.config.url}/message?token={self.config.api_key}",
json={
"message": message.message,
"title": message.title,

View File

@@ -1,10 +1,17 @@
import requests
from pydantic_settings import BaseSettings
from media_manager.config import AllEncompassingConfig
from media_manager.notification.schemas import MessageNotification
from media_manager.notification.service_providers.abstractNotificationServiceProvider import (
AbstractNotificationServiceProvider,
)
class NtfyConfig(BaseSettings):
enabled: bool = False
url: str | None = (
None # e.g. https://ntfy.sh/your-topic (note lack of trailing slash)
)
class NtfyNotificationServiceProvider(AbstractNotificationServiceProvider):
"""
@@ -12,11 +19,11 @@ class NtfyNotificationServiceProvider(AbstractNotificationServiceProvider):
"""
def __init__(self):
self.config = AllEncompassingConfig().notifications
self.config = AllEncompassingConfig().notifications.ntfy
def send_notification(self, message: MessageNotification) -> bool:
response = requests.post(
url=self.config.ntfy_url,
url=self.config.url,
data=message.message.encode(encoding="utf-8"),
headers={
"Title": "MediaManager - " + message.title,

View File

@@ -1,21 +1,27 @@
import requests
from pydantic_settings import BaseSettings
from media_manager.config import AllEncompassingConfig
from media_manager.notification.schemas import MessageNotification
from media_manager.notification.service_providers.abstractNotificationServiceProvider import (
AbstractNotificationServiceProvider,
)
class PushoverConfig(BaseSettings):
enabled: bool = False
api_key: str | None = None
user: str | None = None
class PushoverNotificationServiceProvider(AbstractNotificationServiceProvider):
def __init__(self):
self.config = AllEncompassingConfig().notifications
self.config = AllEncompassingConfig().notifications.pushover
def send_notification(self, message: MessageNotification) -> bool:
response = requests.post(
url="https://api.pushover.net/1/messages.json",
params={
"token": self.config.pushover_api_key,
"user": self.config.pushover_user,
"token": self.config.api_key,
"user": self.config.user,
"message": message.message,
"title": "MediaManager - " + message.title,
},