-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from khanxmetu/feature-ntfy
Added support for ntfy notification service
- Loading branch information
Showing
8 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Optional, TypedDict | ||
|
||
from moodle_dl.types import Course, File | ||
|
||
|
||
class NtfyMessage(TypedDict): | ||
title: str | ||
message: str | ||
source_url: Optional[str] | ||
|
||
|
||
def _get_change_type(file: File) -> str: | ||
if file.modified: | ||
return "modified" | ||
elif file.deleted: | ||
return "deleted" | ||
elif file.moved: | ||
return "moved" | ||
else: | ||
return "new" | ||
|
||
|
||
def create_full_moodle_diff_messages(changes: list[Course]) -> list[NtfyMessage]: | ||
messages = [] | ||
for course in changes: | ||
files_generic_msg = NtfyMessage() | ||
misc_generic_msg = NtfyMessage() | ||
files_generic_msg["message"] = f"{course.fullname}\n" | ||
misc_generic_msg["message"] = f"{course.fullname}\n" | ||
files_generic_cnt = 0 | ||
misc_generic_cnt = 0 | ||
for file in course.files: | ||
change_type = _get_change_type(file) | ||
if file.content_type == "description": | ||
msg = NtfyMessage( | ||
title=" ".join(file.content_filepath.split()[1:]), | ||
message=f"{course.fullname}\n{file.content_filename}", | ||
source_url=file.content_fileurl, | ||
) | ||
if change_type != "new": | ||
msg["message"] += f"\nMessage {change_type}" | ||
messages.append(msg) | ||
elif file.content_type in ("file", "assignfile"): | ||
msg_str = f"* {file.content_filename}" | ||
if file.content_type == "assignfile": | ||
msg_str += " | Assignment File" | ||
if change_type != "new": | ||
msg_str += f" | File {change_type}" | ||
msg_str += "\n" | ||
files_generic_msg["message"] += msg_str | ||
files_generic_cnt += 1 | ||
else: | ||
misc_generic_msg["message"] += f"* {file.content_filename}\n" | ||
misc_generic_cnt += 1 | ||
files_generic_msg["title"] = f"{files_generic_cnt} File Changes" | ||
misc_generic_msg["title"] = f"{misc_generic_cnt} Misc Changes" | ||
if files_generic_cnt: | ||
messages.append(files_generic_msg) | ||
if misc_generic_cnt: | ||
messages.append(misc_generic_msg) | ||
return messages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import logging | ||
import traceback | ||
from typing import List | ||
|
||
import moodle_dl.notifications.ntfy.ntfy_formatter as NF | ||
from moodle_dl.downloader.task import Task | ||
from moodle_dl.notifications.notification_service import NotificationService | ||
from moodle_dl.notifications.ntfy.ntfy_shooter import NtfyShooter | ||
from moodle_dl.types import Course | ||
|
||
|
||
class NtfyService(NotificationService): | ||
def _is_configured(self) -> bool: | ||
# Checks if the sending of ntfy messages has been configured. | ||
try: | ||
self.config.get_property("ntfy") | ||
return True | ||
except ValueError: | ||
logging.debug("ntfy-Notifications not configured, skipping.") | ||
return False | ||
|
||
def _send_messages(self, messages: List[str]): | ||
""" | ||
Sends an message | ||
""" | ||
if not self._is_configured() or messages is None or len(messages) == 0: | ||
return | ||
|
||
ntfy_cfg = self.config.get_property("ntfy") | ||
|
||
logging.info("Sending Notification via ntfy...") | ||
ntfy_shooter = NtfyShooter(ntfy_cfg["topic"], ntfy_cfg.get("server")) | ||
|
||
for message in messages: | ||
try: | ||
ntfy_shooter.send(**message) | ||
except BaseException as e: | ||
logging.error( | ||
"While sending notification:\n%s", | ||
traceback.format_exc(), | ||
extra={"exception": e}, | ||
) | ||
raise e # to be properly notified via Sentry | ||
|
||
def notify_about_changes_in_moodle(self, changes: List[Course]) -> None: | ||
""" | ||
Sends out a notification about the downloaded changes. | ||
@param changes: A list of changed courses with changed files. | ||
""" | ||
if not self._is_configured(): | ||
return | ||
|
||
messages = NF.create_full_moodle_diff_messages(changes) | ||
|
||
self._send_messages(messages) | ||
|
||
def notify_about_error(self, error_description: str): | ||
""" | ||
Sends out an error message if configured to do so. | ||
@param error_description: The error object. | ||
""" | ||
pass | ||
|
||
def notify_about_failed_downloads(self, failed_downloads: List[Task]): | ||
""" | ||
Sends out an message about failed download if configured to send out error messages. | ||
@param failed_downloads: A list of failed Tasks. | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import json | ||
from typing import Optional | ||
|
||
import requests | ||
|
||
|
||
class NtfyShooter: | ||
def __init__(self, topic: str, server: Optional[str] = None): | ||
self.topic = topic | ||
self.server = server or "https://ntfy.sh/" | ||
|
||
def send(self, title: str, message: str, source_url: Optional[str] = None): | ||
data = {"topic": self.topic, "title": title, "message": message} | ||
if source_url: | ||
data["click"] = source_url | ||
view_action = {"action": "view", "label": "View", "url": source_url} | ||
data.setdefault("actions", []).append(view_action) | ||
|
||
resp = requests.post(self.server, data=json.dumps(data)) | ||
resp.raise_for_status() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters