From 27b4f206a141a612b968a009e0b408cba4ceaf3d Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 24 May 2024 12:31:11 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E8=AE=A2=E9=98=85=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E7=AB=99=E7=82=B9=E8=8C=83=E5=9B=B4=E6=89=A9=E5=A4=A7=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/subscribe.py | 15 ++++++++++++++- app/chain/torrents.py | 7 ++++++- app/db/models/site.py | 6 ++++++ app/db/site_oper.py | 6 ++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/chain/subscribe.py b/app/chain/subscribe.py index 5f286ad8..90aa21dc 100644 --- a/app/chain/subscribe.py +++ b/app/chain/subscribe.py @@ -16,6 +16,7 @@ from app.core.event import eventmanager, Event, EventManager from app.core.meta import MetaBase from app.core.metainfo import MetaInfo from app.db.models.subscribe import Subscribe +from app.db.site_oper import SiteOper from app.db.subscribe_oper import SubscribeOper from app.db.subscribehistory_oper import SubscribeHistoryOper from app.db.systemconfig_oper import SystemConfigOper @@ -44,6 +45,7 @@ class SubscribeChain(ChainBase): self.message = MessageHelper() self.systemconfig = SystemConfigOper() self.torrenthelper = TorrentHelper() + self.siteoper = SiteOper() def add(self, title: str, year: str, mtype: MediaType = None, @@ -525,6 +527,15 @@ class SubscribeChain(ChainBase): meta.year = subscribe.year meta.begin_season = subscribe.season or None meta.type = MediaType(subscribe.type) + # 订阅的站点域名列表 + domains = [] + if subscribe.sites: + try: + siteids = json.loads(subscribe.sites) + if siteids: + domains = self.siteoper.get_domains_by_ids(siteids) + except JSONDecodeError: + pass # 识别媒体信息 mediainfo: MediaInfo = self.recognize_media(meta=meta, mtype=meta.type, tmdbid=subscribe.tmdbid, @@ -593,7 +604,9 @@ class SubscribeChain(ChainBase): # 遍历缓存种子 _match_context = [] for domain, contexts in torrents.items(): - logger.info(f'开始匹配站点:{domain},共缓存了 {len(contexts)} 个种子...') + if domains and domain not in domains: + continue + logger.debug(f'开始匹配站点:{domain},共缓存了 {len(contexts)} 个种子...') for context in contexts: # 检查是否匹配 torrent_meta = context.meta_info diff --git a/app/chain/torrents.py b/app/chain/torrents.py index bc63073c..9b6b1083 100644 --- a/app/chain/torrents.py +++ b/app/chain/torrents.py @@ -153,12 +153,15 @@ class TorrentsChain(ChainBase, metaclass=Singleton): # 所有站点索引 indexers = self.siteshelper.get_indexers() + # 需要刷新的站点domain + domains = [] # 遍历站点缓存资源 for indexer in indexers: # 未开启的站点不刷新 if sites and indexer.get("id") not in sites: continue domain = StringUtils.get_url_domain(indexer.get("domain")) + domains.append(domain) if stype == "spider": # 刷新首页种子 torrents: List[TorrentInfo] = self.browse(domain=domain) @@ -219,7 +222,9 @@ class TorrentsChain(ChainBase, metaclass=Singleton): else: self.save_cache(torrents_cache, self._rss_file) - # 返回 + # 去除不在站点范围内的缓存种子 + if sites and torrents_cache: + torrents_cache = {k: v for k, v in torrents_cache.items() if k in domains} return torrents_cache def __renew_rss_url(self, domain: str, site: dict): diff --git a/app/db/models/site.py b/app/db/models/site.py index 7016eb93..7a9b54dd 100644 --- a/app/db/models/site.py +++ b/app/db/models/site.py @@ -69,6 +69,12 @@ class Site(Base): result = db.query(Site).order_by(Site.pri).all() return list(result) + @staticmethod + @db_query + def get_domains_by_ids(db: Session, ids: list): + result = db.query(Site.domain).filter(Site.id.in_(ids)).all() + return [r[0] for r in result] + @staticmethod @db_update def reset(db: Session): diff --git a/app/db/site_oper.py b/app/db/site_oper.py index 1eda3f56..ce153277 100644 --- a/app/db/site_oper.py +++ b/app/db/site_oper.py @@ -63,6 +63,12 @@ class SiteOper(DbOper): """ return Site.get_by_domain(self._db, domain) + def get_domains_by_ids(self, ids: List[int]) -> List[str]: + """ + 按ID获取站点域名 + """ + return Site.get_domains_by_ids(self._db, ids) + def exists(self, domain: str) -> bool: """ 判断站点是否存在