From ef2cfe1c1d88ad4ed807fdb75ee649b1f7aa9327 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 24 Nov 2023 14:32:56 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#1141=20=E4=BF=AE=E5=A4=8D=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=A7=8D=E5=AD=90=E9=93=BE=E6=8E=A5=E4=B8=80=E7=9B=B4?= =?UTF-8?q?=E5=81=BF=E8=AF=95=E4=B8=8B=E8=BD=BD=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/torrents.py | 7 +++++++ app/helper/torrent.py | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/chain/torrents.py b/app/chain/torrents.py index 3af74851..cfa8df33 100644 --- a/app/chain/torrents.py +++ b/app/chain/torrents.py @@ -12,6 +12,7 @@ from app.db.site_oper import SiteOper from app.db.systemconfig_oper import SystemConfigOper from app.helper.rss import RssHelper from app.helper.sites import SitesHelper +from app.helper.torrent import TorrentHelper from app.log import logger from app.schemas import Notification from app.schemas.types import SystemConfigKey, MessageChannel, NotificationType @@ -34,6 +35,7 @@ class TorrentsChain(ChainBase, metaclass=Singleton): self.rsshelper = RssHelper() self.systemconfig = SystemConfigOper() self.mediachain = MediaChain() + self.torrenthelper = TorrentHelper() def remote_refresh(self, channel: MessageChannel, userid: Union[str, int] = None): """ @@ -143,6 +145,11 @@ class TorrentsChain(ChainBase, metaclass=Singleton): # 读取缓存 torrents_cache = self.get_torrents() + # 缓存过滤掉无效种子 + for _domain, _torrents in torrents_cache.items(): + torrents_cache[_domain] = [_torrent for _torrent in _torrents + if not self.torrenthelper.is_invalid(_torrent.torrent_info.enclosure)] + # 所有站点索引 indexers = self.siteshelper.get_indexers() # 遍历站点缓存资源 diff --git a/app/helper/torrent.py b/app/helper/torrent.py index c7aa8cc6..5970f782 100644 --- a/app/helper/torrent.py +++ b/app/helper/torrent.py @@ -14,13 +14,17 @@ from app.db.systemconfig_oper import SystemConfigOper from app.log import logger from app.utils.http import RequestUtils from app.schemas.types import MediaType, SystemConfigKey +from app.utils.singleton import Singleton -class TorrentHelper: +class TorrentHelper(metaclass=Singleton): """ 种子帮助类 """ + # 失败的种子:站点链接 + _invalid_torrents = [] + def __init__(self): self.system_config = SystemConfigOper() @@ -123,6 +127,8 @@ class TorrentHelper: elif req.status_code == 429: return None, None, "", [], "触发站点流控,请稍后重试" else: + # 把错误的种子记下来,避免重复使用 + self.add_invalid(url) return None, None, "", [], f"下载种子出错,状态码:{req.status_code}" @staticmethod @@ -276,3 +282,16 @@ class TorrentHelper: continue episodes = list(set(episodes).union(set(meta.episode_list))) return episodes + + def is_invalid(self, url: str) -> bool: + """ + 判断种子是否是无效种子 + """ + return url in self._invalid_torrents + + def add_invalid(self, url: str): + """ + 添加无效种子 + """ + if url not in self._invalid_torrents: + self._invalid_torrents.append(url)