mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-27 06:13:18 +08:00
* feat(indexer): 支持 FluxTorrent 兼容层站点搜索与用户数据
好学(hxpt.org)已由 NexusPHP 迁移到 FluxTorrent,原有页面刮削全部失效。
FluxTorrent 提供 /api/v1/compat/nexusphp/* 只读 JSON 兼容层,本提交按该契约
新增索引器与用户数据解析器,并在分发层加入能力探测,使未在站点资源中登记
专站协议的站点也能被自动识别(站点清单是闭源发布资源,主仓无法新增条目)。
- 新增 FluxTorrentSpider:调用兼容层 torrents.json 搜索,按 category_newznab
映射媒体类型,按 promotion 换算下载与上传量系数,下载地址携带开放 API 令牌
- 新增 FluxTorrentSiteUserInfo:解析兼容层 user.json 的用户资料与流量
- 新增同步与异步能力探测:站点未登记专站协议时,用站点令牌请求一次
user.json,响应符合 {code, message, data, request_id} 信封即判定为 FluxTorrent
- 探测要求站点已配置令牌,并用命中 24 小时、未命中 30 分钟的双层 TTL 缓存
- 不下发分类与媒体类型筛选:兼容层的 category 只接受站内分类 ID,且客户端
过滤会破坏分页续拉契约,分类筛选待站点资源发布链路补齐后再放开
* fix(indexer): 抽出用户数据模型解析,消除复杂度增长
CI 复杂度门禁报 IndexerModule.refresh_userdata 圈复杂度 15->16(上限 15)。
把 FluxTorrent 能力探测与模型选择抽成模块级函数 _resolve_userdata_schema,
refresh_userdata 的方法体不再因新增分支而增长。
* refactor(indexer): FluxTorrent 模块文件改为单词命名
命名门禁 test_new_host_production_modules_use_single_word_filenames 要求
app 下的模块文件名满足 [a-z][a-z0-9]*,flux_torrent.py 不合规 ⇒ 两个模块
改名为 fluxtorrent.py,同步更新导入与测试打桩路径。
* test(indexer): 修正发布时间断言并同步架构依赖基线
- 发布时间断言原先写死本机时区渲染结果,在 UTC 的 CI 上必然失败 ⇒ 改为对同一
转换函数取期望值,只验证 Unix 时间戳经由 format_timestamp 输出
- dependency-baseline.json 新增 app.modules.indexer.spider.fluxtorrent 与
app.modules.indexer.parser.fluxtorrent 的模块与导入边(edge_count 9973→9990、
module_count 1151→1153),结构与既有 haidan 等专站模块一致,边界未越权
* docs(architecture): 同步架构快照的模块与导入边数量
架构依赖基线新增两个索引器模块后,docs/architecture-overview.md 的
快照表需同步:Python 模块 1151→1153、内部导入边 9,973→9,990。
* test(indexer): 补齐异步探测与失败路径用例,满足新文件覆盖率门槛
新增文件的覆盖率低水位要求 80%,补齐后 spider 90.81%、parser 92.21%:
- 异步能力探测命中与请求异常两条路径
- 站点无响应、响应不是 JSON 时的降级判定
- 异步搜索缺少令牌时的直接失败
- 用户接口空响应 / 非 JSON / 结构异常 / 业务错误码的错误信息
- 站点地址缺协议与空白地址的归一
* test(indexer): 覆盖协议解析的三条分支
存量文件覆盖率低水位要求未覆盖语句数不增长,补齐协议解析的已登记协议、
探测命中、未识别三种情况(同步与异步),以及用户数据模型名的两条分支。
* refactor(indexer): 按站点资源路由 FluxTorrent 协议
删除运行时能力探测与 TTL 缓存,搜索和用户数据直接使用站点资源声明的 parser/schema;保留 FluxTorrent spider 与用户数据解析器的注册,由站点构建配置决定路由。
---------
Co-authored-by: gntv456 <gntv456@users.noreply.github.com>
Co-authored-by: jxxghp <jxxghp@gmail.com>
146 lines
5.2 KiB
Python
146 lines
5.2 KiB
Python
"""
|
||
解析 FluxTorrent 兼容层的用户资料与流量信息。
|
||
"""
|
||
|
||
import json
|
||
from typing import Any, Dict, Optional, Tuple
|
||
|
||
from app.modules.indexer.parser import SitePage, SiteParserBase, SiteSchema
|
||
from app.runtime.log import logger
|
||
|
||
|
||
class FluxTorrentSiteUserInfo(SiteParserBase):
|
||
"""
|
||
FluxTorrent 兼容层用户数据解析器。
|
||
|
||
兼容层按 NexusPHP userdetails 口径返回用户资料,因此不需要解析站点页面。
|
||
"""
|
||
|
||
schema = SiteSchema.FluxTorrent
|
||
request_mode = "apikey"
|
||
|
||
def _parse_site_page(self, html_text: SitePage) -> None:
|
||
"""
|
||
配置兼容层用户信息接口地址和认证请求头。
|
||
|
||
:param html_text: API Key 模式下的空首页数据
|
||
"""
|
||
self._user_basic_page = "api/v1/compat/nexusphp/user.json"
|
||
self._user_basic_params = {}
|
||
self._user_detail_page = None
|
||
self._user_traffic_page = None
|
||
self._torrent_seeding_page = None
|
||
self._user_mail_unread_page = None
|
||
self._sys_mail_unread_page = None
|
||
token = self.apikey or self.token or ""
|
||
headers: Dict[str, Optional[str]] = {
|
||
"Authorization": f"Token {token}",
|
||
"Accept": "application/json",
|
||
}
|
||
if self._ua:
|
||
headers["User-Agent"] = self._ua
|
||
self._addition_headers = headers
|
||
|
||
def _load_api_payload(self, html_text: SitePage, operation: str) -> Optional[Dict[str, Any]]:
|
||
"""
|
||
解析并校验兼容层统一响应信封。
|
||
|
||
:param html_text: 兼容层响应文本
|
||
:param operation: 错误信息中的操作名称
|
||
:return: 成功时返回响应 data,失败时返回 None
|
||
"""
|
||
if not html_text:
|
||
self.err_msg = f"{operation}失败,未收到接口响应"
|
||
return None
|
||
payload: Any
|
||
if isinstance(html_text, dict):
|
||
payload = html_text
|
||
else:
|
||
try:
|
||
payload = json.loads(html_text)
|
||
except (TypeError, json.JSONDecodeError) as err:
|
||
self.err_msg = f"{operation}失败,接口响应不是有效 JSON"
|
||
logger.warning(f"{self._site_name} {self.err_msg}:{str(err)}")
|
||
return None
|
||
if not isinstance(payload, dict):
|
||
self.err_msg = f"{operation}失败,接口响应结构无效"
|
||
logger.warning(f"{self._site_name} {self.err_msg}")
|
||
return None
|
||
if payload.get("code") != 0:
|
||
self.err_msg = payload.get("message") or f"{operation}失败"
|
||
logger.warning(f"{self._site_name} {operation}失败:{self.err_msg}")
|
||
return None
|
||
data = payload.get("data")
|
||
return data if isinstance(data, dict) else None
|
||
|
||
def _parse_user_base_info(self, html_text: SitePage) -> None:
|
||
"""
|
||
解析兼容层返回的用户资料与流量统计。
|
||
|
||
:param html_text: user.json 接口响应文本
|
||
"""
|
||
user_info = self._load_api_payload(html_text, "获取用户信息")
|
||
if not user_info:
|
||
return
|
||
self.userid = user_info.get("id")
|
||
self.username = user_info.get("username")
|
||
self.user_level = user_info.get("class_name") or str(user_info.get("class") or "")
|
||
self.upload = int(user_info.get("uploaded") or 0)
|
||
self.download = int(user_info.get("downloaded") or 0)
|
||
# 站点在只上传未下载时把分享率记为无穷大,此时没有可用的有限数值。
|
||
if user_info.get("ratio_infinite") is True:
|
||
self.ratio = 0.0
|
||
else:
|
||
self.ratio = float(user_info.get("ratio") or 0)
|
||
self.bonus = float(user_info.get("seedbonus") or 0)
|
||
|
||
def _parse_user_traffic_info(self, html_text: str) -> None:
|
||
"""
|
||
跳过独立流量页面,兼容层用户接口已经返回完整统计。
|
||
|
||
:param html_text: 未使用的页面文本
|
||
"""
|
||
|
||
def _parse_user_detail_info(self, html_text: str) -> None:
|
||
"""
|
||
跳过独立用户详情页面,兼容层用户接口已经返回完整资料。
|
||
|
||
:param html_text: 未使用的页面文本
|
||
"""
|
||
|
||
def _parse_user_torrent_seeding_info(
|
||
self,
|
||
html_text: str,
|
||
multi_page: bool = False,
|
||
) -> Optional[str]:
|
||
"""
|
||
跳过独立做种列表,兼容层用户接口未提供做种统计。
|
||
|
||
:param html_text: 未使用的页面文本
|
||
:param multi_page: 是否为后续分页
|
||
:return: 始终返回 None
|
||
"""
|
||
return None
|
||
|
||
def _parse_message_unread_links(self, html_text: str, msg_links: list[str]) -> Optional[str]:
|
||
"""
|
||
兼容层未提供站内消息接口,跳过未读消息解析。
|
||
|
||
:param html_text: 未使用的页面文本
|
||
:param msg_links: 兼容解析器基类签名的消息链接容器
|
||
:return: 始终返回 None
|
||
"""
|
||
return None
|
||
|
||
def _parse_message_content(
|
||
self,
|
||
html_text: str,
|
||
) -> Tuple[Optional[str], Optional[str], Optional[str]]:
|
||
"""
|
||
兼容层未提供站内消息接口,跳过消息内容解析。
|
||
|
||
:param html_text: 未使用的页面文本
|
||
:return: 三个空值
|
||
"""
|
||
return None, None, None
|