fix(indexer): FluxTorrent 连接检测改用兼容层令牌

FluxTorrent 是前后端分离站点,首页 HTML 不含 NexusPHP 登录标记。通用
网页检测会把有效的开放 API Token 误判成「Cookie已失效」,于是添加站点
后连接测试报错、用户资料也不再刷新。

给 FluxTorrentSpider 增加 test_connection:与搜索、用户资料解析共用同一
枚令牌请求兼容层 user.json 并校验统一信封,鉴权失败时把站点返回的原因
透出,不再让站长排查早已不需要的 Cookie。

同时修正两处数据表达:

- 教育类分类(小学/初中/高中/大学/教育影音)没有对应的 Newznab 媒体号,
  原来统一写成「未知」,还把「教育影音」映射成电视剧。现在保留站内分类
  名,既保留信息也不会把图书与课程资源标成剧集。
- 用户资料补做种数、做种体积与注册时间。老版本兼容层的 user.json 没有
  这些字段时改用 PTPP 聚合端点补齐;字段齐全时不重复请求,补充失败也
  不会把已解析成功的资料判成失败。
This commit is contained in:
gntv456
2026-11-12 20:35:49 +08:00
parent 8580eb2cae
commit d411ad4c82
2 changed files with 110 additions and 8 deletions

View File

@@ -5,6 +5,7 @@
import json
from typing import Any, Dict, Optional, Tuple
from app.foundation import temporal as time_tools
from app.modules.indexer.parser import SitePage, SiteParserBase, SiteSchema
from app.runtime.log import logger
@@ -28,7 +29,9 @@ class FluxTorrentSiteUserInfo(SiteParserBase):
self._user_basic_page = "api/v1/compat/nexusphp/user.json"
self._user_basic_params = {}
self._user_detail_page = None
self._user_traffic_page = None
# 老版本兼容层的 user.json 还没有做种与注册时间,用 PTPP 聚合端点补齐;
# 新版本已经返回时 `_parse_user_base_info` 会把这里置空,少发一次请求。
self._user_traffic_page = "api/v1/plugins/ptppUserInfo"
self._torrent_seeding_page = None
self._user_mail_unread_page = None
self._sys_mail_unread_page = None
@@ -93,13 +96,55 @@ class FluxTorrentSiteUserInfo(SiteParserBase):
else:
self.ratio = float(user_info.get("ratio") or 0)
self.bonus = float(user_info.get("seedbonus") or 0)
# 兼容层直接返回做种统计与注册时间;老版本没有这些字段时保持默认
# 0 / None,交给 PTPP 端点补齐,不能因此判成解析失败。
self.seeding = int(user_info.get("seeding") or 0)
self.seeding_size = int(
user_info.get("seeding_size") or user_info.get("seedingSize") or 0
)
join_at = user_info.get("join_at") or user_info.get("joinTime")
self.join_at = (
time_tools.normalize_datetime(str(join_at)) if join_at else None
)
# 三项字段齐全说明站点已是新版兼容层,不必再查一次聚合端点:
# 开放 API 有配额,两个端点的口径也不必再比一遍。
has_seeding = user_info.get("seeding") is not None
has_size = any(
user_info.get(key) is not None
for key in ("seeding_size", "seedingSize")
)
has_join = any(
user_info.get(key) is not None for key in ("join_at", "joinTime")
)
if has_seeding and has_size and has_join:
self._user_traffic_page = None
def _parse_user_traffic_info(self, html_text: str) -> None:
"""
跳过独立流量页面,兼容层用户接口已经返回完整统计。
用 PTPP 聚合端点补齐老版本站点缺失的做种数与注册时间。
:param html_text: 未使用的页面文本
user.json 已经返回这些字段时不覆盖,避免两个端点口径互相打架。
:param html_text: ptppUserInfo 接口响应文本
"""
previous_error = self.err_msg
info = self._load_api_payload(html_text, "获取做种统计")
# 补充端点失败不能把已经成功的用户资料判成失败:
# 老站可能没有部署它,令牌也可能没有该接口权限。
if previous_error is None:
self.err_msg = None
if not info:
return
if not self.seeding:
self.seeding = int(info.get("seeding") or 0)
if not self.seeding_size:
self.seeding_size = int(info.get("seedingSize") or 0)
if not self.join_at:
join_time = info.get("joinTime")
self.join_at = (
time_tools.normalize_datetime(str(join_time))
if join_time else None
)
def _parse_user_detail_info(self, html_text: str) -> None:
"""
@@ -114,7 +159,7 @@ class FluxTorrentSiteUserInfo(SiteParserBase):
multi_page: bool = False,
) -> Optional[str]:
"""
跳过独立做种列表,兼容层用户接口未提供做种统计。
跳过独立做种列表,统计已由 user.json / PTPP 端点给出。
:param html_text: 未使用的页面文本
:param multi_page: 是否为后续分页

View File

@@ -57,7 +57,8 @@ _CATEGORY_NAME_MEDIA_TYPES: Dict[str, MediaType] = {
"综艺": MediaType.TV,
"动漫": MediaType.TV,
"纪录片": MediaType.TV,
"教育影音": MediaType.TV,
# 教育影音不是剧集:映射成电视剧会把图书与课程资源标成剧集,
# 未识别的分类统一回落到站内分类名(见 _media_type)。
"音乐": MediaType.MUSIC,
"有声书": MediaType.COLLECTION,
"电子书": MediaType.COLLECTION,
@@ -195,21 +196,77 @@ class FluxTorrentSpider:
params["keyword"] = keyword
return params
def test_connection(self) -> Tuple[bool, str]:
"""
用开放 API Token 验证兼容层用户接口。
FluxTorrent 是前后端分离站点,首页 HTML 不含 NexusPHP 的登录标记,
通用网页检测会把有效令牌误判成「Cookie已失效」。这里与搜索、用户资料
解析共用同一枚令牌和同一套请求头,不做任何页面抓取。
:return: 令牌是否可用及认证或连接失败原因
"""
if not self._api_token:
return False, "未配置开放 API Token(fxo_ 开头)"
with RequestUtils(
headers=self._request_headers(),
ua=self._user_agent,
proxies=self._proxy or {},
timeout=self._timeout,
).response_manager(
method="GET",
url=f"{self._base_url}/user.json",
allow_redirects=False,
) as response:
if response is None:
return False, "无法连接 FluxTorrent 兼容层接口"
if response.status_code != 200:
# 兼容层鉴权失败也是标准信封,把「未认证 / 权限不足」原样透出,
# 避免站长再去排查早已不需要的 Cookie。
try:
payload = response.json()
except (TypeError, ValueError):
payload = None
if isinstance(payload, dict) and payload.get("message"):
return False, (
f"开放 API Token 无效:{payload['message']}"
f"(HTTP {response.status_code})"
)
return False, f"错误:{response.status_code} {response.reason}"
try:
payload = response.json()
except (TypeError, ValueError):
return False, "站点未返回 FluxTorrent 兼容层响应"
if not isinstance(payload, dict):
return False, "站点未返回 FluxTorrent 兼容层响应"
if payload.get("code") == 0 and isinstance(payload.get("data"), dict):
return True, "连接成功"
return False, str(
payload.get("message") or "开放 API Token 无效或已过期"
)
@staticmethod
def _media_type(item: Dict[str, Any]) -> str:
"""
将兼容层种子分类映射为 MoviePilot 媒体类型。
标准号与通用分类名都不认识时回落到站内分类名:教育类(小学/初中/高中/
大学/教育影音)没有对应的 Newznab 媒体号,一律写「未知」既丢掉了站内
已有的分类信息,也分不出图书、课程与影音资源。
:param item: 兼容层返回的种子记录
:return: MoviePilot 媒体类型,未识别时为未知类型
:return: MoviePilot 媒体类型或站内分类名,两者都缺失时为未知类型
"""
newznab_id = item.get("category_newznab")
media_type = None
if isinstance(newznab_id, int):
media_type = _NEWZNAB_MEDIA_TYPES.get(newznab_id)
site_category = str(item.get("category_name") or "").strip()
if media_type is None:
media_type = _CATEGORY_NAME_MEDIA_TYPES.get(str(item.get("category_name") or "").strip())
return (media_type or MediaType.UNKNOWN).value
media_type = _CATEGORY_NAME_MEDIA_TYPES.get(site_category)
if media_type is not None:
return media_type.value
return site_category or MediaType.UNKNOWN.value
@staticmethod
def _volume_factors(promotion: Optional[str]) -> Tuple[float, float]: