From 25f347116152bbc4eaeff3b02094504bda385eff Mon Sep 17 00:00:00 2001 From: zthxxx Date: Sat, 3 Jun 2023 21:11:00 +0800 Subject: [PATCH 1/8] fix: fix the Notification Type is not applied after changing it from WebUI --- src/module/manager/renamer.py | 4 ++-- src/module/network/request_contents.py | 4 ++-- src/module/notification/notification.py | 15 +++++++++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/module/manager/renamer.py b/src/module/manager/renamer.py index d88de5fb..c6b25b5e 100644 --- a/src/module/manager/renamer.py +++ b/src/module/manager/renamer.py @@ -139,12 +139,12 @@ class Renamer(DownloadClient): if not renamed: logger.warning(f"[Renamer] {subtitle_path} rename failed") - def rename(self): + def rename(self) -> list[Notification]: # Get torrent info logger.debug("[Renamer] Start rename process.") rename_method = settings.bangumi_manage.rename_method torrents_info = self.get_torrent_info() - renamed_info = [] + renamed_info: list[Notification] = [] for info in torrents_info: media_list, subtitle_list = self.check_files(info) bangumi_name, season = self._path_to_bangumi(info.save_path) diff --git a/src/module/network/request_contents.py b/src/module/network/request_contents.py index 63168d7b..0c2096b6 100644 --- a/src/module/network/request_contents.py +++ b/src/module/network/request_contents.py @@ -42,11 +42,11 @@ class RequestContent(RequestURL): _url: str, _filter: str = "|".join(settings.rss_parser.filter), retry: int = 3, - ) -> [TorrentInfo]: + ) -> list[TorrentInfo]: try: soup = self.get_xml(_url, retry) torrent_titles, torrent_urls, torrent_homepage = mikan_parser(soup) - torrents = [] + torrents: list[TorrentInfo] = [] for _title, torrent_url, homepage in zip( torrent_titles, torrent_urls, torrent_homepage ): diff --git a/src/module/notification/notification.py b/src/module/notification/notification.py index ec0147c1..22bde17a 100644 --- a/src/module/notification/notification.py +++ b/src/module/notification/notification.py @@ -10,7 +10,7 @@ from module.database import BangumiDatabase logger = logging.getLogger(__name__) -def getClient(type=settings.notification.type): +def getClient(type: str): if type.lower() == "telegram": return TelegramNotification elif type.lower() == "server-chan": @@ -23,9 +23,10 @@ def getClient(type=settings.notification.type): return None -class PostNotification(getClient()): +class PostNotification: def __init__(self): - super().__init__( + Notifier = getClient(settings.notification.type) + self.notifier = Notifier( token=settings.notification.token, chat_id=settings.notification.chat_id ) @@ -48,12 +49,18 @@ class PostNotification(getClient()): def send_msg(self, notify: Notification) -> bool: text = self._gen_message(notify) try: - self.post_msg(text) + self.notifier.post_msg(text) logger.debug(f"Send notification: {notify.official_title}") except Exception as e: logger.warning(f"Failed to send notification: {e}") return False + def __enter__(self): + self.notifier.__enter__() + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.notifier.__exit__(exc_type, exc_val, exc_tb) if __name__ == "__main__": info = Notification( From 879a3399d2142413d9e688d6d5bae2208ecfa392 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Sun, 4 Jun 2023 01:09:07 +0800 Subject: [PATCH 2/8] fix: log --- docs/wiki | 2 +- src/module/downloader/download_client.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/wiki b/docs/wiki index 410ddf6b..931b4286 160000 --- a/docs/wiki +++ b/docs/wiki @@ -1 +1 @@ -Subproject commit 410ddf6ba0ef4e949c424c133da3207bf6db99e9 +Subproject commit 931b4286447937e413608e6f911ce6712da862c7 diff --git a/src/module/downloader/download_client.py b/src/module/downloader/download_client.py index 37c843f2..d7122cec 100644 --- a/src/module/downloader/download_client.py +++ b/src/module/downloader/download_client.py @@ -45,7 +45,7 @@ class DownloadClient(TorrentPath): def auth(self): self.authed = self.client.auth() if self.authed: - logger.info("[Downloader] Authed.") + logger.debug("[Downloader] Authed.") else: logger.error("[Downloader] Auth failed.") From ea7ee6ed02b45a2b271adce5e943de9be9df515f Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Sun, 4 Jun 2023 01:11:34 +0800 Subject: [PATCH 3/8] fix: No filter when collect bangumi --- src/module/manager/collector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/module/manager/collector.py b/src/module/manager/collector.py index 1b341e3e..f7d5804c 100644 --- a/src/module/manager/collector.py +++ b/src/module/manager/collector.py @@ -25,7 +25,7 @@ class SeasonCollector(DownloadClient): if not link: torrents = st.search_season(data) else: - torrents = st.get_torrents(link) + torrents = st.get_torrents(link, _filter="|".join(data.filter)) self.add_season_torrents(data, torrents) logger.info("Completed!") From ddb89df40ea387dfd8621ffde56b6f9e8fb6902d Mon Sep 17 00:00:00 2001 From: umbor <14857490+umbors@users.noreply.github.com> Date: Sun, 4 Jun 2023 11:02:24 +0800 Subject: [PATCH 4/8] Add Wecom Notification MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了企业微信推送通道 --- src/module/notification/notification.py | 2 ++ src/module/notification/plugin/__init__.py | 3 +- src/module/notification/plugin/wecom.py | 35 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/module/notification/plugin/wecom.py diff --git a/src/module/notification/notification.py b/src/module/notification/notification.py index 4181ac02..ec0147c1 100644 --- a/src/module/notification/notification.py +++ b/src/module/notification/notification.py @@ -17,6 +17,8 @@ def getClient(type=settings.notification.type): return ServerChanNotification elif type.lower() == "bark": return BarkNotification + elif type.lower() == "wecom": + return WecomNotification else: return None diff --git a/src/module/notification/plugin/__init__.py b/src/module/notification/plugin/__init__.py index 106087f6..ece03d58 100644 --- a/src/module/notification/plugin/__init__.py +++ b/src/module/notification/plugin/__init__.py @@ -1,3 +1,4 @@ from .bark import BarkNotification from .server_chan import ServerChanNotification -from .telegram import TelegramNotification \ No newline at end of file +from .telegram import TelegramNotification +from .wecom import WecomNotification \ No newline at end of file diff --git a/src/module/notification/plugin/wecom.py b/src/module/notification/plugin/wecom.py new file mode 100644 index 00000000..c56e97e2 --- /dev/null +++ b/src/module/notification/plugin/wecom.py @@ -0,0 +1,35 @@ +import logging +from module.network import RequestContent + +logger = logging.getLogger(__name__) + + +class WecomNotification(RequestContent): + """企业微信推送 基于图文消息""" + + def __init__(self, token, chat_id, **kwargs): + super().__init__() + #Chat_id is used as noti_url in this push tunnel + self.notification_url = f"{chat_id}" + self.token = token + + def post_msg(self, text: str) -> 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() + #Default pic to avoid blank in message. Resolution:1068*455 + if picurl == "": + picurl = "https://article.biliimg.com/bfs/article/d8bcd0408bf32594fd82f27de7d2c685829d1b2e.png" + data = { + "key":self.token, + "type": "news", + "title": title, + "msg": msg, + "picurl":picurl + } + resp = self.post_data(self.notification_url, data) + logger.debug(f"Wecom notification: {resp.status_code}") + return resp.status_code == 200 From b1fc759366010d4ce453021e81c42979f392b68a Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Sun, 4 Jun 2023 12:48:34 +0800 Subject: [PATCH 5/8] fix: #296 --- src/module/downloader/client/qb_downloader.py | 3 ++- src/module/parser/analyser/torrent_parser.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/module/downloader/client/qb_downloader.py b/src/module/downloader/client/qb_downloader.py index d3563185..ccc0c4dd 100644 --- a/src/module/downloader/client/qb_downloader.py +++ b/src/module/downloader/client/qb_downloader.py @@ -80,10 +80,11 @@ class QbDownloader: def torrents_info(self, status_filter, category, tag=None): return self._client.torrents_info(status_filter=status_filter, category=category, tag=tag) - def torrents_add(self, urls, save_path, category): + def torrents_add(self, urls, save_path, category, torrent_files=None): return self._client.torrents_add( is_paused=False, urls=urls, + torrent_files=torrent_files, save_path=save_path, category=category, use_auto_torrent_management=False diff --git a/src/module/parser/analyser/torrent_parser.py b/src/module/parser/analyser/torrent_parser.py index fab6e485..ba2a1880 100644 --- a/src/module/parser/analyser/torrent_parser.py +++ b/src/module/parser/analyser/torrent_parser.py @@ -18,8 +18,8 @@ RULES = [ ] SUBTITLE_LANG = { - "zh-tw": ["TC", "CHT", "繁", "zh-tw"], - "zh": ["SC", "CHS", "简", "zh"], + "zh-tw": ["TC", "CHT", "cht", "繁", "zh-tw"], + "zh": ["SC", "CHS", "chs", "简", "zh"], } From 691457cb0070c20ced5a8aad05c76f30553fcf4c Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Sun, 4 Jun 2023 18:00:36 +0800 Subject: [PATCH 6/8] =?UTF-8?q?fix:=20socks=20=E4=B8=8B=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E5=88=B0=20QB=20=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/wiki | 2 +- src/module/network/request_url.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/wiki b/docs/wiki index 931b4286..d0bb98f0 160000 --- a/docs/wiki +++ b/docs/wiki @@ -1 +1 @@ -Subproject commit 931b4286447937e413608e6f911ce6712da862c7 +Subproject commit d0bb98f004fb292519dd56c42238ecb2f034eac9 diff --git a/src/module/network/request_url.py b/src/module/network/request_url.py index 85dadb7e..4f4c52d4 100644 --- a/src/module/network/request_url.py +++ b/src/module/network/request_url.py @@ -13,6 +13,7 @@ logger = logging.getLogger(__name__) class RequestURL: def __init__(self): self.header = {"user-agent": "Mozilla/5.0", "Accept": "application/xml"} + self._socks5_proxy = False def get_url(self, url, retry=3): try_time = 0 @@ -77,6 +78,7 @@ class RequestURL: "http": url, } elif settings.proxy.type == "socks5": + self._socks5_proxy = True socks.set_default_proxy( socks.SOCKS5, addr=settings.proxy.host, @@ -91,4 +93,7 @@ class RequestURL: return self def __exit__(self, exc_type, exc_val, exc_tb): + if self._socks5_proxy: + socks.set_default_proxy() + socket.socket = socks.socksocket self.session.close() From a1bd26570f78c6960b793f5b7c5202ae32b30506 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Sun, 4 Jun 2023 18:02:18 +0800 Subject: [PATCH 7/8] =?UTF-8?q?fix:=20socks=20=E4=BB=A3=E7=90=86=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/module/network/request_url.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/module/network/request_url.py b/src/module/network/request_url.py index 4f4c52d4..16da3d4c 100644 --- a/src/module/network/request_url.py +++ b/src/module/network/request_url.py @@ -96,4 +96,5 @@ class RequestURL: if self._socks5_proxy: socks.set_default_proxy() socket.socket = socks.socksocket + self._socks5_proxy = False self.session.close() From 5309ebecb8052eba51fcfa4cfedbe46db3552bbb Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Sun, 4 Jun 2023 18:57:23 +0800 Subject: [PATCH 8/8] =?UTF-8?q?fix:=20collect=20=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=BB=A3=E7=90=86=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20Close=20#290?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/module/api/download.py | 6 ++++-- src/module/downloader/client/qb_downloader.py | 3 ++- src/module/downloader/download_client.py | 14 ++++++++++--- src/module/manager/collector.py | 21 ++++++++++++------- src/module/network/request_contents.py | 1 - 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/module/api/download.py b/src/module/api/download.py index ed3bc9df..6ce80569 100644 --- a/src/module/api/download.py +++ b/src/module/api/download.py @@ -32,8 +32,10 @@ async def download_collection( ) if data: with SeasonCollector() as collector: - collector.collect_season(data, data.rss_link[0]) - return {"status": "Success"} + if collector.collect_season(data, data.rss_link[0], proxy=True): + return {"status": "Success"} + else: + return {"status": "Failed to add torrent"} else: return {"status": "Failed to parse link"} diff --git a/src/module/downloader/client/qb_downloader.py b/src/module/downloader/client/qb_downloader.py index ccc0c4dd..8b22b813 100644 --- a/src/module/downloader/client/qb_downloader.py +++ b/src/module/downloader/client/qb_downloader.py @@ -81,7 +81,7 @@ class QbDownloader: return self._client.torrents_info(status_filter=status_filter, category=category, tag=tag) def torrents_add(self, urls, save_path, category, torrent_files=None): - return self._client.torrents_add( + resp = self._client.torrents_add( is_paused=False, urls=urls, torrent_files=torrent_files, @@ -89,6 +89,7 @@ class QbDownloader: category=category, use_auto_torrent_management=False ) + return resp == "Ok." def torrents_delete(self, hash): return self._client.torrents_delete(delete_files=True, torrent_hashes=hash) diff --git a/src/module/downloader/download_client.py b/src/module/downloader/download_client.py index d7122cec..c30c1cd3 100644 --- a/src/module/downloader/download_client.py +++ b/src/module/downloader/download_client.py @@ -112,9 +112,17 @@ class DownloadClient(TorrentPath): logger.info(f"[Downloader] Remove torrents.") def add_torrent(self, torrent: dict): - self.client.torrents_add( - urls=torrent["url"], save_path=torrent["save_path"], category="Bangumi" - ) + if self.client.torrents_add( + urls=torrent.get("urls"), + torrent_files=torrent.get("torrent_files"), + save_path=torrent.get("save_path"), + category="Bangumi" + ): + logger.debug(f"[Downloader] Add torrent: {torrent.get('save_path')}") + return True + else: + logger.error(f"[Downloader] Add torrent failed: {torrent.get('save_path')}") + return False def move_torrent(self, hashes, location): self.client.move_torrent(hashes=hashes, new_location=location) diff --git a/src/module/manager/collector.py b/src/module/manager/collector.py index f7d5804c..ec8d260b 100644 --- a/src/module/manager/collector.py +++ b/src/module/manager/collector.py @@ -11,23 +11,30 @@ logger = logging.getLogger(__name__) class SeasonCollector(DownloadClient): - def add_season_torrents(self, data: BangumiData, torrents): - for torrent in torrents: + def add_season_torrents(self, data: BangumiData, torrents, torrent_files=None): + if torrent_files: download_info = { - "url": torrent.torrent_link, + "torrent_files": torrent_files, "save_path": self._gen_save_path(data), } - self.add_torrent(download_info) + return self.add_torrent(download_info) + else: + download_info = { + "urls": [torrent.torrent_link for torrent in torrents], + "save_path": self._gen_save_path(data), + } + return self.add_torrent(download_info) - def collect_season(self, data: BangumiData, link: str = None): + def collect_season(self, data: BangumiData, link: str = None, proxy: bool = False): logger.info(f"Start collecting {data.official_title} Season {data.season}...") with SearchTorrent() as st: if not link: torrents = st.search_season(data) else: torrents = st.get_torrents(link, _filter="|".join(data.filter)) - self.add_season_torrents(data, torrents) - logger.info("Completed!") + if proxy: + torrent_files = [st.get_content(torrent.torrent_link) for torrent in torrents] + return self.add_season_torrents(data, torrents, torrent_files=torrent_files) def subscribe_season(self, data: BangumiData): with BangumiDatabase() as db: diff --git a/src/module/network/request_contents.py b/src/module/network/request_contents.py index b9b2b7af..63168d7b 100644 --- a/src/module/network/request_contents.py +++ b/src/module/network/request_contents.py @@ -46,7 +46,6 @@ class RequestContent(RequestURL): try: soup = self.get_xml(_url, retry) torrent_titles, torrent_urls, torrent_homepage = mikan_parser(soup) - torrents = [] for _title, torrent_url, homepage in zip( torrent_titles, torrent_urls, torrent_homepage