From 28d5245424ccba760819899aaf7944819849ec28 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Mon, 5 Jun 2023 00:34:05 +0800 Subject: [PATCH 1/7] feat: add search data in ORM class --- src/module/ab_decorator/__init__.py | 9 +++ src/module/database/bangumi.py | 94 ++++++++++++++++++----------- src/module/database/connector.py | 34 +++++++++++ 3 files changed, 101 insertions(+), 36 deletions(-) diff --git a/src/module/ab_decorator/__init__.py b/src/module/ab_decorator/__init__.py index f469894c..f25a083b 100644 --- a/src/module/ab_decorator/__init__.py +++ b/src/module/ab_decorator/__init__.py @@ -1,7 +1,9 @@ import logging import time +import threading logger = logging.getLogger(__name__) +lock = threading.Lock() def qb_connect_failed_wait(func): @@ -30,3 +32,10 @@ def api_failed(func): logger.debug(e) return wrapper + + +def locked(func): + def wrapper(*args, **kwargs): + with lock: + return func(*args, **kwargs) + return wrapper \ No newline at end of file diff --git a/src/module/database/bangumi.py b/src/module/database/bangumi.py index 74e7fec5..f2129826 100644 --- a/src/module/database/bangumi.py +++ b/src/module/database/bangumi.py @@ -2,6 +2,7 @@ import logging from module.database.connector import DataConnector from module.models import BangumiData +from module.ab_decorator import locked logger = logging.getLogger(__name__) @@ -68,6 +69,7 @@ class BangumiDatabase(DataConnector): data_list = [self.__data_to_db(x) for x in data] self._update_list(data_list=data_list, table_name=self.__table_name) + @locked def update_rss(self, title_raw, rss_set: str): # Update rss and added self._cursor.execute( @@ -108,51 +110,59 @@ class BangumiDatabase(DataConnector): self._delete_all(self.__table_name) def search_all(self) -> list[BangumiData]: - self._cursor.execute( - """ - SELECT * FROM bangumi - """ - ) - return self.__fetch_data() + dict_data = self._search_datas(self.__table_name) + return [self.__db_to_data(x) for x in dict_data] def search_id(self, _id: int) -> BangumiData | None: - self._cursor.execute( - """ - SELECT * FROM bangumi WHERE id = :id - """, - {"id": _id}, - ) - values = self._cursor.fetchone() - if values is None: + condition = {"id": _id} + value = self._search_data(table_name=self.__table_name, condition=condition) + # self._cursor.execute( + # """ + # SELECT * FROM bangumi WHERE id = :id + # """, + # {"id": _id}, + # ) + # values = self._cursor.fetchone() + if value is None: return None keys = [x[0] for x in self._cursor.description] - dict_data = dict(zip(keys, values)) + dict_data = dict(zip(keys, value)) return self.__db_to_data(dict_data) def search_official_title(self, official_title: str) -> BangumiData | None: - self._cursor.execute( - """ - SELECT * FROM bangumi WHERE official_title = :official_title - """, - {"official_title": official_title}, + value = self._search_data( + table_name=self.__table_name, condition={"official_title": official_title} ) - values = self._cursor.fetchone() - if values is None: + # self._cursor.execute( + # """ + # SELECT * FROM bangumi WHERE official_title = :official_title + # """, + # {"official_title": official_title}, + # ) + # values = self._cursor.fetchone() + if value is None: return None keys = [x[0] for x in self._cursor.description] - dict_data = dict(zip(keys, values)) + dict_data = dict(zip(keys, value)) return self.__db_to_data(dict_data) def match_poster(self, bangumi_name: str) -> str: - self._cursor.execute( - """ - SELECT official_title, poster_link - FROM bangumi - WHERE INSTR(:bangumi_name, official_title) > 0 - """, - {"bangumi_name": bangumi_name}, + condition = f"INSTR({bangumi_name}, official_title) > 0" + keys = ["official_title", "poster_link"] + data = self._search_data( + table_name=self.__table_name, + keys=keys, + condition=condition, ) - data = self._cursor.fetchone() + # self._cursor.execute( + # """ + # SELECT official_title, poster_link + # FROM bangumi + # WHERE INSTR(:bangumi_name, official_title) > 0 + # """, + # {"bangumi_name": bangumi_name}, + # ) + # data = self._cursor.fetchone() if not data: return "" official_title, poster_link = data @@ -160,14 +170,20 @@ class BangumiDatabase(DataConnector): return "" return poster_link + @locked def match_list(self, torrent_list: list, rss_link: str) -> list: # Match title_raw in database - self._cursor.execute( - """ - SELECT title_raw, rss_link, poster_link FROM bangumi - """ + keys = ["title_raw", "rss_link", "poster_link"] + data = self._search_datas( + table_name=self.__table_name, + keys=keys, ) - data = self._cursor.fetchall() + # self._cursor.execute( + # """ + # SELECT title_raw, rss_link, poster_link FROM bangumi + # """ + # ) + # data = self._cursor.fetchall() if not data: return torrent_list # Match title @@ -189,6 +205,12 @@ class BangumiDatabase(DataConnector): def not_complete(self) -> list[BangumiData]: # Find eps_complete = False + condition = "eps_complete = 0" + data = self._search_datas( + table_name=self.__table_name, + condition=condition, + ) + self._cursor.execute( """ SELECT * FROM bangumi WHERE eps_collect = 0 diff --git a/src/module/database/connector.py b/src/module/database/connector.py index d74bb159..84a6ae56 100644 --- a/src/module/database/connector.py +++ b/src/module/database/connector.py @@ -2,7 +2,9 @@ import os import sqlite3 import logging + from module.conf import DATA_PATH +from module.ab_decorator import locked logger = logging.getLogger(__name__) @@ -15,6 +17,7 @@ class DataConnector: self._conn = sqlite3.connect(DATA_PATH) self._cursor = self._conn.cursor() + @locked def _update_table(self, table_name: str, db_data: dict): columns = ", ".join( [ @@ -38,6 +41,7 @@ class DataConnector: self._conn.commit() logger.debug(f"Create / Update table {table_name}.") + @locked def _insert(self, table_name: str, db_data: dict): columns = ", ".join(db_data.keys()) values = ", ".join([f":{key}" for key in db_data.keys()]) @@ -46,6 +50,7 @@ class DataConnector: ) self._conn.commit() + @locked def _insert_list(self, table_name: str, data_list: list[dict]): columns = ", ".join(data_list[0].keys()) values = ", ".join([f":{key}" for key in data_list[0].keys()]) @@ -54,6 +59,7 @@ class DataConnector: ) self._conn.commit() + @locked def _select(self, keys: list[str], table_name: str, condition: str = None) -> dict: if condition is None: self._cursor.execute(f"SELECT {', '.join(keys)} FROM {table_name}") @@ -63,6 +69,7 @@ class DataConnector: ) return dict(zip(keys, self._cursor.fetchone())) + @locked def _update(self, table_name: str, db_data: dict): _id = db_data.get("id") if _id is None: @@ -74,6 +81,7 @@ class DataConnector: self._conn.commit() return self._cursor.rowcount == 1 + @locked def _update_list(self, table_name: str, data_list: list[dict]): if len(data_list) == 0: return @@ -85,6 +93,7 @@ class DataConnector: ) self._conn.commit() + @locked def _update_section(self, table_name: str, location: dict, update_dict: dict): set_sql = ", ".join([f"{key} = :{key}" for key in update_dict.keys()]) sql_loc = f"{location['key']} = {location['value']}" @@ -93,10 +102,35 @@ class DataConnector: ) self._conn.commit() + @locked def _delete_all(self, table_name: str): self._cursor.execute(f"DELETE FROM {table_name}") self._conn.commit() + @locked + def _search_data(self, table_name: str, keys: list[str] | None, condition: str) -> dict: + if keys is None: + self._cursor.execute(f"SELECT * FROM {table_name} WHERE {condition}") + else: + self._cursor.execute( + f"SELECT {', '.join(keys)} FROM {table_name} WHERE {condition}" + ) + return dict(zip(keys, self._cursor.fetchone())) + + @locked + def _search_datas(self, table_name: str, keys: list[str] | None, condition: str = None) -> list[dict]: + if keys is None: + select_sql = "*" + else: + select_sql = ", ".join(keys) + if condition is None: + self._cursor.execute(f"SELECT {select_sql} FROM {table_name}") + else: + self._cursor.execute( + f"SELECT {select_sql} FROM {table_name} WHERE {condition}" + ) + return [dict(zip(keys, row)) for row in self._cursor.fetchall()] + def _table_exists(self, table_name: str) -> bool: self._cursor.execute( f"SELECT name FROM sqlite_master WHERE type='table' AND name=?;", From 530ed4c14f0fc07e26b76bad1c77bcb50d1eb166 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Mon, 5 Jun 2023 14:03:55 +0800 Subject: [PATCH 2/7] change: update wiki --- docs/wiki | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/wiki b/docs/wiki index 410ddf6b..519e381e 160000 --- a/docs/wiki +++ b/docs/wiki @@ -1 +1 @@ -Subproject commit 410ddf6ba0ef4e949c424c133da3207bf6db99e9 +Subproject commit 519e381e8a1add62e76a39181ee61bad02816035 From dbc73ea66546756017c4c3d9afa8192b05071572 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Mon, 5 Jun 2023 18:19:17 +0800 Subject: [PATCH 3/7] fix: fix #300 --- src/module/database/connector.py | 14 +++----------- src/module/downloader/client/qb_downloader.py | 7 ++++++- src/module/manager/renamer.py | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/module/database/connector.py b/src/module/database/connector.py index 84a6ae56..dbd87c04 100644 --- a/src/module/database/connector.py +++ b/src/module/database/connector.py @@ -4,7 +4,6 @@ import logging from module.conf import DATA_PATH -from module.ab_decorator import locked logger = logging.getLogger(__name__) @@ -17,7 +16,6 @@ class DataConnector: self._conn = sqlite3.connect(DATA_PATH) self._cursor = self._conn.cursor() - @locked def _update_table(self, table_name: str, db_data: dict): columns = ", ".join( [ @@ -41,7 +39,6 @@ class DataConnector: self._conn.commit() logger.debug(f"Create / Update table {table_name}.") - @locked def _insert(self, table_name: str, db_data: dict): columns = ", ".join(db_data.keys()) values = ", ".join([f":{key}" for key in db_data.keys()]) @@ -50,7 +47,6 @@ class DataConnector: ) self._conn.commit() - @locked def _insert_list(self, table_name: str, data_list: list[dict]): columns = ", ".join(data_list[0].keys()) values = ", ".join([f":{key}" for key in data_list[0].keys()]) @@ -59,7 +55,6 @@ class DataConnector: ) self._conn.commit() - @locked def _select(self, keys: list[str], table_name: str, condition: str = None) -> dict: if condition is None: self._cursor.execute(f"SELECT {', '.join(keys)} FROM {table_name}") @@ -69,7 +64,6 @@ class DataConnector: ) return dict(zip(keys, self._cursor.fetchone())) - @locked def _update(self, table_name: str, db_data: dict): _id = db_data.get("id") if _id is None: @@ -81,7 +75,6 @@ class DataConnector: self._conn.commit() return self._cursor.rowcount == 1 - @locked def _update_list(self, table_name: str, data_list: list[dict]): if len(data_list) == 0: return @@ -93,7 +86,6 @@ class DataConnector: ) self._conn.commit() - @locked def _update_section(self, table_name: str, location: dict, update_dict: dict): set_sql = ", ".join([f"{key} = :{key}" for key in update_dict.keys()]) sql_loc = f"{location['key']} = {location['value']}" @@ -102,12 +94,12 @@ class DataConnector: ) self._conn.commit() - @locked + def _delete_all(self, table_name: str): self._cursor.execute(f"DELETE FROM {table_name}") self._conn.commit() - @locked + def _search_data(self, table_name: str, keys: list[str] | None, condition: str) -> dict: if keys is None: self._cursor.execute(f"SELECT * FROM {table_name} WHERE {condition}") @@ -117,7 +109,7 @@ class DataConnector: ) return dict(zip(keys, self._cursor.fetchone())) - @locked + def _search_datas(self, table_name: str, keys: list[str] | None, condition: str = None) -> list[dict]: if keys is None: select_sql = "*" diff --git a/src/module/downloader/client/qb_downloader.py b/src/module/downloader/client/qb_downloader.py index 8b22b813..fc5b622e 100644 --- a/src/module/downloader/client/qb_downloader.py +++ b/src/module/downloader/client/qb_downloader.py @@ -132,7 +132,12 @@ class QbDownloader: return self._client.torrents_info(hashes=_hash)[0].save_path def set_category(self, _hash, category): - self._client.torrents_set_category(category, hashes=_hash) + try: + self._client.torrents_set_category(category, hashes=_hash) + except Conflict409Error: + logger.warning(f"[Downloader] Category {category} does not exist") + self.add_category(category) + self._client.torrents_set_category(category, hashes=_hash) def check_connection(self): return self._client.app_version() diff --git a/src/module/manager/renamer.py b/src/module/manager/renamer.py index c6b25b5e..6cfc9b8b 100644 --- a/src/module/manager/renamer.py +++ b/src/module/manager/renamer.py @@ -106,7 +106,7 @@ class Renamer(DownloadClient): _hash=_hash, old_path=media_path, new_path=new_path ) if not renamed: - logger.warning(f"[Renamrr] {media_path} rename failed") + logger.warning(f"[Renamer] {media_path} rename failed") # Delete bad torrent. if settings.bangumi_manage.remove_bad_torrent: self.delete_torrent(_hash) From 1404a5c29daf1654ac2cde8cfd78dd1c21361fcd Mon Sep 17 00:00:00 2001 From: root Date: Wed, 7 Jun 2023 23:30:12 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=A0=87=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/module/notification/notification.py | 10 ++++++---- src/module/notification/plugin/bark.py | 4 ++-- src/module/notification/plugin/server_chan.py | 4 ++-- src/module/notification/plugin/slack.py | 4 ++-- src/module/notification/plugin/telegram.py | 2 +- src/module/notification/plugin/wecom.py | 10 +++++----- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/module/notification/notification.py b/src/module/notification/notification.py index 22bde17a..d94841e0 100644 --- a/src/module/notification/notification.py +++ b/src/module/notification/notification.py @@ -32,7 +32,7 @@ class PostNotification: ) @staticmethod - def _gen_message(notify: Notification) -> str: + def _gen_message(notify: Notification) -> (str, str): with BangumiDatabase() as db: poster_path = db.match_poster(notify.official_title) if poster_path: @@ -44,12 +44,14 @@ class PostNotification: text = f""" 番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集\n """ - return text + + title = f"""{notify.official_title}S{notify.season}E{notify.episode}""" + return text, title def send_msg(self, notify: Notification) -> bool: - text = self._gen_message(notify) + text, title = self._gen_message(notify) try: - self.notifier.post_msg(text) + self.notifier.post_msg(text, title) logger.debug(f"Send notification: {notify.official_title}") except Exception as e: logger.warning(f"Failed to send notification: {e}") diff --git a/src/module/notification/plugin/bark.py b/src/module/notification/plugin/bark.py index f405ab4b..05e8b693 100644 --- a/src/module/notification/plugin/bark.py +++ b/src/module/notification/plugin/bark.py @@ -11,8 +11,8 @@ class BarkNotification(RequestContent): self.token = token self.notification_url = "https://api.day.app/push" - def post_msg(self, text) -> bool: - data = {"title": "AutoBangumi 番剧更新", "body": text, "device_key": self.token} + def post_msg(self, text, title: str) -> bool: + data = {"title": title, "body": text, "device_key": self.token} resp = self.post_data(self.notification_url, data) logger.debug(f"Bark notification: {resp.status_code}") return resp.status_code == 200 diff --git a/src/module/notification/plugin/server_chan.py b/src/module/notification/plugin/server_chan.py index 5b4b32d0..e2fed8ba 100644 --- a/src/module/notification/plugin/server_chan.py +++ b/src/module/notification/plugin/server_chan.py @@ -12,9 +12,9 @@ class ServerChanNotification(RequestContent): super().__init__() self.notification_url = f"https://sctapi.ftqq.com/{token}.send" - def post_msg(self, text: str) -> bool: + def post_msg(self, text: str, title: str) -> bool: data = { - "title": "AutoBangumi 番剧更新", + "title": title, "desp": text, } resp = self.post_data(self.notification_url, data) diff --git a/src/module/notification/plugin/slack.py b/src/module/notification/plugin/slack.py index 1773e85d..43097657 100644 --- a/src/module/notification/plugin/slack.py +++ b/src/module/notification/plugin/slack.py @@ -11,8 +11,8 @@ class SlackNotification(RequestContent): self.token = token self.notification_url = "https://api.day.app/push" - def post_msg(self, text) -> bool: - data = {"title": "AutoBangumi 番剧更新", "body": text, "device_key": self.token} + def post_msg(self, text: str, title: str) -> bool: + data = {"title": title, "body": text, "device_key": self.token} resp = self.post_data(self.notification_url, data) logger.debug(f"Bark notification: {resp.status_code}") return resp.status_code == 200 \ No newline at end of file diff --git a/src/module/notification/plugin/telegram.py b/src/module/notification/plugin/telegram.py index 71e90c16..94211f73 100644 --- a/src/module/notification/plugin/telegram.py +++ b/src/module/notification/plugin/telegram.py @@ -11,7 +11,7 @@ class TelegramNotification(RequestContent): self.notification_url = f"https://api.telegram.org/bot{token}/sendMessage" self.chat_id = chat_id - def post_msg(self, text: str) -> bool: + def post_msg(self, text: str, title: str) -> bool: data = { "chat_id": self.chat_id, "text": text, diff --git a/src/module/notification/plugin/wecom.py b/src/module/notification/plugin/wecom.py index c56e97e2..78bb6649 100644 --- a/src/module/notification/plugin/wecom.py +++ b/src/module/notification/plugin/wecom.py @@ -13,7 +13,7 @@ class WecomNotification(RequestContent): self.notification_url = f"{chat_id}" self.token = token - def post_msg(self, text: str) -> bool: + def post_msg(self, text: str, title: str) -> bool: ##Change message format to match Wecom push better info = text.split(":") print(info) @@ -24,10 +24,10 @@ class WecomNotification(RequestContent): if picurl == "": picurl = "https://article.biliimg.com/bfs/article/d8bcd0408bf32594fd82f27de7d2c685829d1b2e.png" data = { - "key":self.token, - "type": "news", - "title": title, - "msg": msg, + "key":self.token, + "type": "news", + "title": title, + "msg": msg, "picurl":picurl } resp = self.post_data(self.notification_url, data) From ea11bb8a6a4330b20cf2deb7164a060f4eb9abb7 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Thu, 8 Jun 2023 11:01:52 +0800 Subject: [PATCH 5/7] change: Update notify method, make it easy to custom. --- src/module/models/bangumi.py | 1 + src/module/notification/notification.py | 23 +++++++++---------- src/module/notification/plugin/bark.py | 14 +++++++++-- src/module/notification/plugin/server_chan.py | 13 +++++++++-- src/module/notification/plugin/slack.py | 13 +++++++++-- src/module/notification/plugin/telegram.py | 13 +++++++++-- src/module/notification/plugin/wecom.py | 21 +++++++++++------ 7 files changed, 71 insertions(+), 27 deletions(-) diff --git a/src/module/models/bangumi.py b/src/module/models/bangumi.py index f1c03369..2dd73340 100644 --- a/src/module/models/bangumi.py +++ b/src/module/models/bangumi.py @@ -28,6 +28,7 @@ class Notification(BaseModel): official_title: str = Field(..., alias="official_title", title="番剧名") season: int = Field(..., alias="season", title="番剧季度") episode: int = Field(..., alias="episode", title="番剧集数") + poster_path: str | None = Field(None, alias="poster_path", title="番剧海报路径") @dataclass diff --git a/src/module/notification/notification.py b/src/module/notification/notification.py index d94841e0..1e13ff67 100644 --- a/src/module/notification/notification.py +++ b/src/module/notification/notification.py @@ -32,26 +32,25 @@ class PostNotification: ) @staticmethod - def _gen_message(notify: Notification) -> (str, str): + def _get_poster(notify: Notification): with BangumiDatabase() as db: poster_path = db.match_poster(notify.official_title) if poster_path: poster_link = "https://mikanani.me" + poster_path - text = f""" - 番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集\n{poster_link}\n - """ + # text = f""" + # 番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集\n{poster_link}\n + # """ else: - text = f""" - 番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集\n - """ - - title = f"""{notify.official_title}S{notify.season}E{notify.episode}""" - return text, title + poster_link = "https://mikanani.me" + # text = f""" + # 番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集\n + # """ + notify.poster_path = poster_link def send_msg(self, notify: Notification) -> bool: - text, title = self._gen_message(notify) + self._get_poster(notify) try: - self.notifier.post_msg(text, title) + self.notifier.post_msg(notify) logger.debug(f"Send notification: {notify.official_title}") except Exception as e: logger.warning(f"Failed to send notification: {e}") diff --git a/src/module/notification/plugin/bark.py b/src/module/notification/plugin/bark.py index 05e8b693..e825a2c9 100644 --- a/src/module/notification/plugin/bark.py +++ b/src/module/notification/plugin/bark.py @@ -1,5 +1,7 @@ import logging + from module.network import RequestContent +from module.models import Notification logger = logging.getLogger(__name__) @@ -11,8 +13,16 @@ class BarkNotification(RequestContent): self.token = token self.notification_url = "https://api.day.app/push" - def post_msg(self, text, title: str) -> bool: - data = {"title": title, "body": text, "device_key": self.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 + + def post_msg(self, notify: Notification) -> bool: + text = self.gen_message(notify) + data = {"title": notify.official_title, "body": text, "device_key": self.token} resp = self.post_data(self.notification_url, data) logger.debug(f"Bark notification: {resp.status_code}") return resp.status_code == 200 diff --git a/src/module/notification/plugin/server_chan.py b/src/module/notification/plugin/server_chan.py index e2fed8ba..9903ed3e 100644 --- a/src/module/notification/plugin/server_chan.py +++ b/src/module/notification/plugin/server_chan.py @@ -1,6 +1,7 @@ import logging from module.network import RequestContent +from module.models import Notification logger = logging.getLogger(__name__) @@ -12,9 +13,17 @@ class ServerChanNotification(RequestContent): super().__init__() self.notification_url = f"https://sctapi.ftqq.com/{token}.send" - def post_msg(self, text: str, title: str) -> bool: + @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 + + def post_msg(self, notify: Notification) -> bool: + text = self.gen_message(notify) data = { - "title": title, + "title": notify.official_title, "desp": text, } resp = self.post_data(self.notification_url, data) diff --git a/src/module/notification/plugin/slack.py b/src/module/notification/plugin/slack.py index 43097657..af4a0d83 100644 --- a/src/module/notification/plugin/slack.py +++ b/src/module/notification/plugin/slack.py @@ -1,6 +1,7 @@ import logging from module.network import RequestContent +from module.models import Notification logger = logging.getLogger(__name__) @@ -11,8 +12,16 @@ class SlackNotification(RequestContent): self.token = token self.notification_url = "https://api.day.app/push" - def post_msg(self, text: str, title: str) -> bool: - data = {"title": title, "body": text, "device_key": self.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 + + def post_msg(self, notify: Notification) -> bool: + text = self.gen_message(notify) + data = {"title": notify.official_title, "body": text, "device_key": self.token} resp = self.post_data(self.notification_url, data) logger.debug(f"Bark notification: {resp.status_code}") return resp.status_code == 200 \ No newline at end of file diff --git a/src/module/notification/plugin/telegram.py b/src/module/notification/plugin/telegram.py index 94211f73..b269aea3 100644 --- a/src/module/notification/plugin/telegram.py +++ b/src/module/notification/plugin/telegram.py @@ -1,6 +1,7 @@ import logging -from module.network.request_contents import RequestContent +from module.network import RequestContent +from module.models import Notification logger = logging.getLogger(__name__) @@ -11,7 +12,15 @@ class TelegramNotification(RequestContent): self.notification_url = f"https://api.telegram.org/bot{token}/sendMessage" self.chat_id = chat_id - def post_msg(self, text: str, title: str) -> bool: + @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 + + def post_msg(self, notify: Notification) -> bool: + text = self.gen_message(notify) data = { "chat_id": self.chat_id, "text": text, diff --git a/src/module/notification/plugin/wecom.py b/src/module/notification/plugin/wecom.py index 78bb6649..11a63a75 100644 --- a/src/module/notification/plugin/wecom.py +++ b/src/module/notification/plugin/wecom.py @@ -1,5 +1,7 @@ import logging + from module.network import RequestContent +from module.models import Notification logger = logging.getLogger(__name__) @@ -13,15 +15,20 @@ class WecomNotification(RequestContent): self.notification_url = f"{chat_id}" self.token = token - def post_msg(self, text: str, title: str) -> bool: + @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 + + def post_msg(self, notify: Notification) -> bool: ##Change message format to match Wecom push better - info = text.split(":") - print(info) - title = "【番剧更新】" + info[1].split("\n")[0].strip() - msg = info[2].split("\n")[0].strip()+" "+info[3].split("\n")[0].strip() - picurl = info[3].split("\n")[1].strip() + title = "【番剧更新】" + notify.official_title + msg = self.gen_message(notify) + picurl = notify.poster_path #Default pic to avoid blank in message. Resolution:1068*455 - if picurl == "": + if picurl == "https://mikanani.me": picurl = "https://article.biliimg.com/bfs/article/d8bcd0408bf32594fd82f27de7d2c685829d1b2e.png" data = { "key":self.token, From 39c7699711a18c1a0fbb8c286ad9e5cc83c11278 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Thu, 8 Jun 2023 12:28:20 +0800 Subject: [PATCH 6/7] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/module/checker/checker.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/module/checker/checker.py b/src/module/checker/checker.py index fc2c46d8..a42a44a3 100644 --- a/src/module/checker/checker.py +++ b/src/module/checker/checker.py @@ -47,7 +47,8 @@ class Checker: @staticmethod def check_first_run() -> bool: token_exist = False if settings.rss_parser.token in ["", "token"] else True - if token_exist: - return False - else: - return True + if settings.rss_parser.enable: + if token_exist: + return False + else: + return True From f1359e1786451e4f2d845f88a2b382ebf7fc05f2 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Thu, 8 Jun 2023 20:18:43 +0800 Subject: [PATCH 7/7] =?UTF-8?q?fix:=20=E5=90=AF=E5=8A=A8=20AB=20=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=9D=E5=A7=8B=E5=8C=96=20QB=20=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/module/checker/checker.py | 13 ++++++------- src/module/core/sub_thread.py | 2 ++ src/module/manager/collector.py | 5 ----- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/module/checker/checker.py b/src/module/checker/checker.py index a42a44a3..474637f6 100644 --- a/src/module/checker/checker.py +++ b/src/module/checker/checker.py @@ -2,7 +2,8 @@ import os.path from module.downloader import DownloadClient from module.network import RequestContent -from module.conf import settings, DATA_PATH +from module.conf import settings +from module.models import Config class Checker: @@ -46,9 +47,7 @@ class Checker: @staticmethod def check_first_run() -> bool: - token_exist = False if settings.rss_parser.token in ["", "token"] else True - if settings.rss_parser.enable: - if token_exist: - return False - else: - return True + if settings.dict() == Config().dict(): + return True + else: + return False diff --git a/src/module/core/sub_thread.py b/src/module/core/sub_thread.py index 33f9d1c5..122bf744 100644 --- a/src/module/core/sub_thread.py +++ b/src/module/core/sub_thread.py @@ -19,6 +19,8 @@ class RSSThread(ProgramStatus): ) def rss_loop(self): + with DownloadClient() as client: + client.init_downloader() while not self.stop_event.is_set(): # Analyse RSS with BangumiDatabase() as db: diff --git a/src/module/manager/collector.py b/src/module/manager/collector.py index 7350108a..cff45d3f 100644 --- a/src/module/manager/collector.py +++ b/src/module/manager/collector.py @@ -59,8 +59,3 @@ def eps_complete(): bd.update_list(datas) -if __name__ == "__main__": - from module.conf import setup_logger - - setup_logger() - eps_complete()