From d207d3eaecffdc3596863d06c550f214c8574126 Mon Sep 17 00:00:00 2001 From: jqtmviyu Date: Mon, 15 Apr 2024 11:12:39 +0800 Subject: [PATCH 1/6] feat: wecom-robot notify --- .../src/module/notification/notification.py | 3 ++ .../module/notification/plugin/__init__.py | 1 + .../module/notification/plugin/wecom_robot.py | 46 +++++++++++++++++++ .../setting/config-notification.vue | 1 + 4 files changed, 51 insertions(+) create mode 100644 backend/src/module/notification/plugin/wecom_robot.py diff --git a/backend/src/module/notification/notification.py b/backend/src/module/notification/notification.py index 909fc92ca..e6310db85 100644 --- a/backend/src/module/notification/notification.py +++ b/backend/src/module/notification/notification.py @@ -9,6 +9,7 @@ ServerChanNotification, TelegramNotification, WecomNotification, + WecomRobotNotification, ) logger = logging.getLogger(__name__) @@ -23,6 +24,8 @@ def getClient(type: str): return BarkNotification elif type.lower() == "wecom": return WecomNotification + elif type.lower() == "wecom-robot": + return WecomRobotNotification else: return None diff --git a/backend/src/module/notification/plugin/__init__.py b/backend/src/module/notification/plugin/__init__.py index e9acda8f1..af63d22bf 100644 --- a/backend/src/module/notification/plugin/__init__.py +++ b/backend/src/module/notification/plugin/__init__.py @@ -2,3 +2,4 @@ from .server_chan import ServerChanNotification from .telegram import TelegramNotification from .wecom import WecomNotification +from .wecom_robot import WecomRobotNotification diff --git a/backend/src/module/notification/plugin/wecom_robot.py b/backend/src/module/notification/plugin/wecom_robot.py new file mode 100644 index 000000000..516bc1a23 --- /dev/null +++ b/backend/src/module/notification/plugin/wecom_robot.py @@ -0,0 +1,46 @@ +import logging +import requests + +from module.models import Notification +from module.network import RequestContent + +logger = logging.getLogger(__name__) + + +class WecomRobotNotification(RequestContent): + """企业微信群机器人""" + + def __init__(self, token, chat_id, **kwargs): + super().__init__() + # token is wecom group robot webhook key + self.notification_url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={token}" + + @staticmethod + def gen_message(notify: Notification) -> str: + text = f""" + 番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集\n{notify.poster_path}\n + """ + return text.strip() + + def post_msg(self, notify: Notification) -> bool: + title = "【番剧更新】" + notify.official_title + msg = self.gen_message(notify) + picurl = notify.poster_path + if picurl == "": + picurl = "https://article.biliimg.com/bfs/article/d8bcd0408bf32594fd82f27de7d2c685829d1b2e.png" + data = { + "msgtype": "news", + "news": { + "articles" : [ + { + "title" : title, + "description" : msg, + "url" : "https://mikanime.tv", + "picurl" : picurl + } + ] + } + } + resp = requests.post(url=self.notification_url, json=data, timeout=3) + logger.debug(f"Wecom-robot notification: {resp.status_code}") + return resp.status_code == 200 diff --git a/webui/src/components/setting/config-notification.vue b/webui/src/components/setting/config-notification.vue index c2bf5088a..607e6648f 100644 --- a/webui/src/components/setting/config-notification.vue +++ b/webui/src/components/setting/config-notification.vue @@ -11,6 +11,7 @@ const notificationType: NotificationType = [ 'server-chan', 'bark', 'wecom', + 'wecom-robot' ]; const items: SettingItem[] = [ From 747d6e27bc61350efe65adf9f7c3e5ddad1fdf4a Mon Sep 17 00:00:00 2001 From: jqtmviyu Date: Mon, 15 Apr 2024 12:28:34 +0800 Subject: [PATCH 2/6] =?UTF-8?q?test(notification):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E6=A8=A1=E5=9D=97=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加通知模块测试 --- backend/src/module/notification/notification.py | 3 ++- backend/src/test/test_notification.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 backend/src/test/test_notification.py diff --git a/backend/src/module/notification/notification.py b/backend/src/module/notification/notification.py index e6310db85..803fee925 100644 --- a/backend/src/module/notification/notification.py +++ b/backend/src/module/notification/notification.py @@ -46,8 +46,9 @@ def _get_poster(notify: Notification): def send_msg(self, notify: Notification) -> bool: self._get_poster(notify) try: - self.notifier.post_msg(notify) + resp = self.notifier.post_msg(notify) logger.debug(f"Send notification: {notify.official_title}") + return resp except Exception as e: logger.warning(f"Failed to send notification: {e}") return False diff --git a/backend/src/test/test_notification.py b/backend/src/test/test_notification.py new file mode 100644 index 000000000..3a9c3c86e --- /dev/null +++ b/backend/src/test/test_notification.py @@ -0,0 +1,14 @@ +from module.notification import PostNotification +from module.models import Notification + + +def test_notification(): + info = Notification( + official_title="番剧名", + season=1, + episode=1, + poster_path="https://article.biliimg.com/bfs/article/d8bcd0408bf32594fd82f27de7d2c685829d1b2e.png", + ) + with PostNotification() as notifier: + assert notifier.send_msg(info) == True + From 4074b31ed5de368b4298b3fbf5b6babfad5ec544 Mon Sep 17 00:00:00 2001 From: jqtmviyu Date: Mon, 15 Apr 2024 12:17:26 +0800 Subject: [PATCH 3/6] =?UTF-8?q?test(test=5Frss=5Fengine):=20=E6=8A=8Amikan?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E4=B8=8D=E5=A2=99=E7=9A=84tv=E5=9F=9F?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把mikan改为不墙的tv域名 --- backend/src/test/test_rss_engine.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/src/test/test_rss_engine.py b/backend/src/test/test_rss_engine.py index cda69f6ed..130979d6e 100644 --- a/backend/src/test/test_rss_engine.py +++ b/backend/src/test/test_rss_engine.py @@ -5,7 +5,7 @@ def test_rss_engine(): with RSSEngine(e) as engine: - rss_link = "https://mikanani.me/RSS/Bangumi?bangumiId=2353&subgroupid=552" + rss_link = "https://mikanime.tv/RSS/Bangumi?bangumiId=2353&subgroupid=552" engine.add_rss(rss_link, aggregate=False) @@ -14,5 +14,7 @@ def test_rss_engine(): new_torrents = engine.pull_rss(result[1]) torrent = new_torrents[0] - assert torrent.name == "[Lilith-Raws] 无职转生,到了异世界就拿出真本事 / Mushoku Tensei - 11 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]" - + assert ( + torrent.name + == "[Lilith-Raws] 无职转生,到了异世界就拿出真本事 / Mushoku Tensei - 11 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]" + ) From fefef7d5129670b4f71e1cf0d58e23fd446b9ed1 Mon Sep 17 00:00:00 2001 From: jqtmviyu Date: Mon, 15 Apr 2024 14:07:23 +0800 Subject: [PATCH 4/6] =?UTF-8?q?style:=20=E5=8F=98=E6=9B=B4=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81lint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/notification/plugin/wecom_robot.py | 27 ++++++++++--------- backend/src/test/test_notification.py | 3 +-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/backend/src/module/notification/plugin/wecom_robot.py b/backend/src/module/notification/plugin/wecom_robot.py index 516bc1a23..aa3b7ae98 100644 --- a/backend/src/module/notification/plugin/wecom_robot.py +++ b/backend/src/module/notification/plugin/wecom_robot.py @@ -1,4 +1,5 @@ import logging + import requests from module.models import Notification @@ -13,7 +14,9 @@ class WecomRobotNotification(RequestContent): def __init__(self, token, chat_id, **kwargs): super().__init__() # token is wecom group robot webhook key - self.notification_url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={token}" + self.notification_url = ( + f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={token}" + ) @staticmethod def gen_message(notify: Notification) -> str: @@ -29,17 +32,17 @@ def post_msg(self, notify: Notification) -> bool: if picurl == "": picurl = "https://article.biliimg.com/bfs/article/d8bcd0408bf32594fd82f27de7d2c685829d1b2e.png" data = { - "msgtype": "news", - "news": { - "articles" : [ - { - "title" : title, - "description" : msg, - "url" : "https://mikanime.tv", - "picurl" : picurl - } - ] - } + "msgtype": "news", + "news": { + "articles": [ + { + "title": title, + "description": msg, + "url": "https://mikanime.tv", + "picurl": picurl, + } + ] + }, } resp = requests.post(url=self.notification_url, json=data, timeout=3) logger.debug(f"Wecom-robot notification: {resp.status_code}") diff --git a/backend/src/test/test_notification.py b/backend/src/test/test_notification.py index 3a9c3c86e..1f95d8e58 100644 --- a/backend/src/test/test_notification.py +++ b/backend/src/test/test_notification.py @@ -1,5 +1,5 @@ -from module.notification import PostNotification from module.models import Notification +from module.notification import PostNotification def test_notification(): @@ -11,4 +11,3 @@ def test_notification(): ) with PostNotification() as notifier: assert notifier.send_msg(info) == True - From 5d1b6cfeededbb173dde285945a71f2ea4532f88 Mon Sep 17 00:00:00 2001 From: jqtmviyu Date: Tue, 16 Apr 2024 02:14:09 +0800 Subject: [PATCH 5/6] =?UTF-8?q?revert(test=5Frss=5Fengine):=20mikan?= =?UTF-8?q?=E5=9B=9E=E9=80=80.me=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/test/test_rss_engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/test/test_rss_engine.py b/backend/src/test/test_rss_engine.py index 130979d6e..315956c14 100644 --- a/backend/src/test/test_rss_engine.py +++ b/backend/src/test/test_rss_engine.py @@ -5,7 +5,7 @@ def test_rss_engine(): with RSSEngine(e) as engine: - rss_link = "https://mikanime.tv/RSS/Bangumi?bangumiId=2353&subgroupid=552" + rss_link = "https://mikanani.me/RSS/Bangumi?bangumiId=2353&subgroupid=552" engine.add_rss(rss_link, aggregate=False) From 4c34c9537bdc6b7859f2df8e09bd2f06cd9dc22f Mon Sep 17 00:00:00 2001 From: jqtmviyu Date: Fri, 19 Apr 2024 19:35:55 +0800 Subject: [PATCH 6/6] =?UTF-8?q?fix(wecom=5Frobot,test=5Fnotification):=20?= =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E4=BC=81=E5=BE=AE=E7=BE=A4?= =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E9=80=9A=E7=9F=A5=E5=92=8C=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/notification/plugin/wecom_robot.py | 36 ++++++++++--------- backend/src/test/test_notification.py | 21 ++++++++--- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/backend/src/module/notification/plugin/wecom_robot.py b/backend/src/module/notification/plugin/wecom_robot.py index aa3b7ae98..b071c56a2 100644 --- a/backend/src/module/notification/plugin/wecom_robot.py +++ b/backend/src/module/notification/plugin/wecom_robot.py @@ -21,29 +21,31 @@ def __init__(self, token, chat_id, **kwargs): @staticmethod def gen_message(notify: Notification) -> str: text = f""" - 番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集\n{notify.poster_path}\n + 番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集 """ return text.strip() def post_msg(self, notify: Notification) -> bool: - title = "【番剧更新】" + notify.official_title + title = "【番剧更新】" msg = self.gen_message(notify) picurl = notify.poster_path - if picurl == "": - picurl = "https://article.biliimg.com/bfs/article/d8bcd0408bf32594fd82f27de7d2c685829d1b2e.png" - data = { - "msgtype": "news", - "news": { - "articles": [ - { - "title": title, - "description": msg, - "url": "https://mikanime.tv", - "picurl": picurl, - } - ] - }, - } + if picurl.startswith("http"): + data = { + "msgtype": "news", + "news": { + "articles": [ + { + "title": title, + "description": msg, + "url": "https://mikanime.tv", + "picurl": picurl, + } + ] + }, + } + else: + msg = f"{title}\n{msg}" + data = {"msgtype": "text", "text": {"content": msg}} resp = requests.post(url=self.notification_url, json=data, timeout=3) logger.debug(f"Wecom-robot notification: {resp.status_code}") return resp.status_code == 200 diff --git a/backend/src/test/test_notification.py b/backend/src/test/test_notification.py index 1f95d8e58..54f457338 100644 --- a/backend/src/test/test_notification.py +++ b/backend/src/test/test_notification.py @@ -2,12 +2,25 @@ from module.notification import PostNotification +class TestPostNotification(PostNotification): + @staticmethod + def _get_poster(notify: Notification): + notify.poster_path = notify.poster_path + + def test_notification(): - info = Notification( + info1 = Notification( + official_title="番剧名", + season=1, + episode=1, + poster_path="https://mikanime.tv/images/Bangumi/202404/0fd46fc8.jpg", + ) + info2 = Notification( official_title="番剧名", season=1, episode=1, - poster_path="https://article.biliimg.com/bfs/article/d8bcd0408bf32594fd82f27de7d2c685829d1b2e.png", + poster_path="posters/0fd46fc8.jpg", ) - with PostNotification() as notifier: - assert notifier.send_msg(info) == True + with TestPostNotification() as notifier: + assert notifier.send_msg(info1) + assert notifier.send_msg(info2)