From d1694f6cb71dcdc59c9b37d76e397b180244fd30 Mon Sep 17 00:00:00 2001 From: yilee <930617673@qq.com> Date: Wed, 4 Jun 2025 23:45:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:ntfy=E6=B7=BB=E5=8A=A0Token=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E5=92=8C=E7=94=A8=E6=88=B7=E5=8A=A8=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/ntfymsg/__init__.py | 113 +++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 3 deletions(-) diff --git a/plugins/ntfymsg/__init__.py b/plugins/ntfymsg/__init__.py index c005233..3f5638f 100644 --- a/plugins/ntfymsg/__init__.py +++ b/plugins/ntfymsg/__init__.py @@ -1,3 +1,4 @@ +import base64 import json import requests @@ -15,10 +16,20 @@ class NtfyClient: headers = { "Title": title.encode(encoding='utf-8'), "Markdown": "true" if format_as_markdown else "false", + "Icon": "https://movie-pilot.org/images/logo.png", } + if self._token: + headers["Authorization"] = "Bearer " + self._token + elif self._user and self._password: + authStr = self._user + ":" + self._password + headers["Authorization"] = "Basic " + base64.b64encode(authStr.encode('utf-8')).decode('utf-8') + + if self._actions: + headers["Actions"] = self._actions.encode('utf-8') + response = json.loads( - requests.post(url=self.url, data=message.encode(encoding='utf-8'), headers=headers, auth=self._auth).text + requests.post(url=self.url, data=message.encode(encoding='utf-8'), headers=headers).text ) return response @@ -28,11 +39,16 @@ class NtfyClient: server: str = "https://ntfy.sh", user: str = "", password: str = "", + token: str = "", + actions: str = "", ): self._server = server self._topic = topic self.__set_url(server, topic) - self._auth = (user, password) + self._user = user + self._password = password + self._token = token + self._actions = actions def __set_url(self, server, topic): self.url = server.strip("/") + "/" + topic @@ -64,6 +80,8 @@ class NtfyMsg(_PluginBase): _topic = None _user = None _password = None + _token = None + _actions = None _msgtypes = [] def init_plugin(self, config: dict = None): @@ -74,6 +92,8 @@ class NtfyMsg(_PluginBase): self._topic = config.get("topic") self._user = config.get("user") self._password = config.get("password") + self._token = config.get("token") + self._actions = config.get("actions") def get_state(self) -> bool: return self._enabled and (True if self._server and self._topic else False) @@ -194,6 +214,45 @@ class NtfyMsg(_PluginBase): } ] }, + { + 'component': 'VRow', + 'content': [ + { + 'component': 'VCol', + 'props': { + 'cols': 12, + 'md': 6 + }, + 'content': [ + { + 'component': 'VTextField', + 'props': { + 'model': 'token', + 'label': '访问令牌', + 'placeholder': 'ntfytoken', + } + } + ] + }, + { + 'component': 'VCol', + 'props': { + 'cols': 12, + 'md': 6 + }, + 'content': [ + { + 'component': 'VTextField', + 'props': { + 'model': 'actions', + 'label': '用户动作', + 'placeholder': 'ntfyactions', + } + } + ] + } + ] + }, { 'component': 'VRow', 'content': [ @@ -217,6 +276,48 @@ class NtfyMsg(_PluginBase): } ] }, + { + 'component': 'VRow', + 'content': [ + { + 'component': 'VCol', + 'props': { + 'cols': 12, + }, + 'content': [ + { + 'component': 'VAlert', + 'props': { + 'type': 'info', + 'variant': 'tonal', + 'text': '用户或Token创建参考:https://docs.ntfy.sh/config/#access-control' + } + } + ] + } + ] + }, + { + 'component': 'VRow', + 'content': [ + { + 'component': 'VCol', + 'props': { + 'cols': 12, + }, + 'content': [ + { + 'component': 'VAlert', + 'props': { + 'type': 'info', + 'variant': 'tonal', + 'text': '用户动作创建参考:https://docs.ntfy.sh/publish/?h=action#using-a-header' + } + } + ] + } + ] + }, ] } ], { @@ -226,6 +327,8 @@ class NtfyMsg(_PluginBase): 'topic': 'MoviePilot', 'user': '', 'password': '', + 'token': '', + 'actions': '', } def get_page(self) -> List[dict]: @@ -266,7 +369,11 @@ class NtfyMsg(_PluginBase): try: if not self._server or not self._topic: return False, "参数未配置" - ntfy = NtfyClient(server=self._server, topic=self._topic, user=self._user, password=self._password) + ntfy = NtfyClient( + server=self._server, topic=self._topic, + user=self._user, password=self._password, + token=self._token, actions=self._actions + ) ntfy.send(title=title, message=text, format_as_markdown=True) except Exception as msg_e: