From f1204fab4746e4867319a14405e984c035f85485 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Tue, 25 Apr 2023 20:35:52 +0800 Subject: [PATCH] Add telegram and server-chan notification --- src/main.py | 1 - src/module/manager/renamer.py | 3 +++ src/module/network/notification.py | 15 ++++++++------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main.py b/src/main.py index c5688170..c951d767 100644 --- a/src/main.py +++ b/src/main.py @@ -31,7 +31,6 @@ async def restart(): logger.info("Restarted") return {"status": "success"} - @router.get("/api/v1/stop", tags=["program"]) async def stop(): global main_process diff --git a/src/module/manager/renamer.py b/src/module/manager/renamer.py index 2e8c8c4b..fa50c63b 100644 --- a/src/module/manager/renamer.py +++ b/src/module/manager/renamer.py @@ -7,6 +7,7 @@ from module.core.download_client import DownloadClient from module.conf import settings from module.parser import TitleParser +from module.network import PostNotification logger = logging.getLogger(__name__) @@ -15,6 +16,7 @@ class Renamer: def __init__(self, download_client: DownloadClient): self.client = download_client self._renamer = TitleParser() + self.notification = PostNotification() @staticmethod def print_result(torrent_count, rename_count): @@ -50,6 +52,7 @@ class Renamer: if compare_name != new_path: try: self.client.rename_torrent_file(_hash=info.hash, old_path=media_path, new_path=new_path) + self.notification.send_msg(folder_name, "update") except Exception as e: logger.warning(f"{old_name} rename failed") logger.warning(f"Folder name: {folder_name}, Season: {season}, Suffix: {suffix}") diff --git a/src/module/network/notification.py b/src/module/network/notification.py index c6dca0ba..edb7103a 100644 --- a/src/module/network/notification.py +++ b/src/module/network/notification.py @@ -17,7 +17,7 @@ class PostNotification: def getClient(): if settings.notification.type.lower() == "telegram": return TelegramNotification() - elif settings.notification.type.lower() == "serverchan": + elif settings.notification.type.lower() == "server-chan": return ServerChanNotification() else: return None @@ -32,16 +32,19 @@ class PostNotification: class TelegramNotification: def __init__(self): - self.token = settings.notification_token + self.token = settings.notification.token + self.chat_id = settings.notification.chat_id self.notification_url = f"https://api.telegram.org/bot{self.token}/sendMessage" - def send_msg(self, title: str, desp: str) -> bool: - if not settings.notification_enable: - return False + def send_msg(self, title: str, desp: str): data = { "chat_id": settings.notification_chat_id, "text": f"{title}\n{desp}", + "disable_notification": True, } + with RequestContent() as req: + resp = req.post_data(self.notification_url, data) + return resp.status_code == 200 class ServerChanNotification: @@ -51,8 +54,6 @@ class ServerChanNotification: self.notification_url = f"https://sctapi.ftqq.com/{self.token}.send" def send_msg(self, title: str, desp: str) -> bool: - if not settings.notification.enable: - return False data = { "title": title, "desp": desp,