diff --git a/app/modules/indexer/parser/nexus_php.py b/app/modules/indexer/parser/nexus_php.py index eb82c48e1..f1ea7a03f 100644 --- a/app/modules/indexer/parser/nexus_php.py +++ b/app/modules/indexer/parser/nexus_php.py @@ -151,26 +151,29 @@ class NexusPhpSiteUserInfo(SiteParserBase): def _parse_user_traffic_info(self, html_text: str) -> None: """ - 解析用户流量信息。 + 解析用户流量信息,优先读取修道院概览指标,缺失字段沿用传统页面规则。 """ html_text = self._prepare_html_text(html_text) - upload_match = re.search(r"[^总]上[传傳]量?[::_<>/a-zA-Z-=\"'\s#;]+([\d,.\s]+[KMGTPI]*B)", html_text, - re.IGNORECASE) - self.upload = self.num_filesize(upload_match.group(1).strip()) if upload_match else 0 - download_match = re.search(r"[^总子影力]下[载載]量?[::_<>/a-zA-Z-=\"'\s#;]+([\d,.\s]+[KMGTPI]*B)", html_text, - re.IGNORECASE) - self.download = self.num_filesize(download_match.group(1).strip()) if download_match else 0 - ratio_match = re.search(r"分享率[::_<>/a-zA-Z-=\"'\s#;]+([\d,.\s]+)", html_text) - # 计算分享率 - calc_ratio = 0.0 if self.download <= 0.0 else round(self.upload / self.download, 3) - # 优先使用页面上的分享率 - self.ratio = text_tools.parse_float(ratio_match.group(1)) if ( - ratio_match and ratio_match.group(1).strip()) else calc_ratio - leeching_match = re.search(r"(Torrents leeching|下载中)[\u4E00-\u9FA5\D\s]+(\d+)[\s\S]+<", html_text) - self.leeching = text_tools.parse_int(leeching_match.group(2)) if leeching_match and leeching_match.group( - 2).strip() else 0 html = etree.HTML(html_text) try: + metrics = self._traffic_metric_values(html) + upload_match = re.search(r"[^总]上[传傳]量?[::_<>/a-zA-Z-=\"'\s#;]+([\d,.\s]+[KMGTPI]*B)", html_text, + re.IGNORECASE) + upload_text = metrics.get("上传量") or (upload_match.group(1).strip() if upload_match else "") + self.upload = self.num_filesize(upload_text) + download_match = re.search(r"[^总子影力]下[载載]量?[::_<>/a-zA-Z-=\"'\s#;]+([\d,.\s]+[KMGTPI]*B)", html_text, + re.IGNORECASE) + download_text = metrics.get("下载量") or (download_match.group(1).strip() if download_match else "") + self.download = self.num_filesize(download_text) + ratio_match = re.search(r"分享率[::_<>/a-zA-Z-=\"'\s#;]+([\d,.\s]+)", html_text) + ratio_text = metrics.get("分享率") or (ratio_match.group(1).strip() if ratio_match else "") + # 优先使用页面上的分享率,否则按最终解析到的流量计算。 + calc_ratio = 0.0 if self.download <= 0.0 else round(self.upload / self.download, 3) + self.ratio = text_tools.parse_float(ratio_text) if ratio_text else calc_ratio + leeching_match = re.search(r"(Torrents leeching|下载中)[\u4E00-\u9FA5\D\s]+(\d+)[\s\S]+<", html_text) + self.leeching = text_tools.parse_int(leeching_match.group(2)) if leeching_match and leeching_match.group( + 2).strip() else 0 + has_ucoin, self.bonus = self._parse_ucoin(html) if has_ucoin: return @@ -221,6 +224,25 @@ class NexusPhpSiteUserInfo(SiteParserBase): if html is not None: del html + @staticmethod + def _traffic_metric_values(html: Any) -> dict[str, str]: + """ + 读取修道院概览中的账户流量指标,忽略字段名下的中文副标题。 + + :param html: 首页节点;响应为空时可为 None + :return: 指标名称到显示值的映射;缺失字段不加入结果 + """ + metrics: dict[str, str] = {} + if not DomUtils.has_child_elements(html): + return metrics + for metric in html.xpath( + '//dl[contains(concat(" ", normalize-space(@class), " "), " cultivation-metrics ")]/div[dt and dd]' + ): + label = metric.xpath('normalize-space(dt/text()[1])') + if label in {"上传量", "下载量", "分享率"}: + metrics[label] = metric.xpath('normalize-space(dd)') + return metrics + @staticmethod def _parse_ucoin(html: etree._Element) -> tuple[bool, float]: """ diff --git a/tests/fixtures/cspt_homepage.html b/tests/fixtures/cspt_homepage.html new file mode 100644 index 000000000..543248d6d --- /dev/null +++ b/tests/fixtures/cspt_homepage.html @@ -0,0 +1,38 @@ +
+
+
欢迎回来, xxxx (练气期) 退出 + 控制面板 + 收藏 + 法钱 使用: 1,585.8 签到已得95, 补签卡: 0 勋章 + 任务 + 邀请 发送: 2(0)
当前活动: Torrents seeding2 Torrents leeching0  可连接:是连接数:无限制认领: [0/1000]
+
+
+

我的概览

修持在日常,贡献有记录。

+ 个人资料与记录 +
+
+ 境界 人材(User) + 从芸芸黔首中显露出利用价值的人,可被仙门体系吸纳;耗材:已经“有用”,但这种价值更多是对上修而言——可用、可炼、可牺牲。 道籍 普通道籍
+
+
上传量 功行
105.06 GB
+
下载量 所耗资粮
10.00 GB
+
分享率 取予有度
10.506
+
做种时长 修持
42天18:11:03
+
魔力值 法钱
1,585.8
+
做种积分 成长积累
935
+
+
+
+
+

资源检索 道藏

找到所需的版本,让每一份资源持续可达。

+ 发布资源
+ + +
+
+ Buddylist RSS
inbox 0 sentbox 0 +
diff --git a/tests/test_nexus_php_parser.py b/tests/test_nexus_php_parser.py index 297e7e1bc..96c6a443d 100644 --- a/tests/test_nexus_php_parser.py +++ b/tests/test_nexus_php_parser.py @@ -25,6 +25,101 @@ def _build_parser() -> NexusPhpSiteUserInfo: ) +def test_nexus_php_cspt_homepage_traffic() -> None: + """ + 回放 Issue #7231 的脱敏首页,指标副标题不能阻断上传量、下载量和分享率解析。 + """ + homepage = (Path(__file__).parent / "fixtures" / "cspt_homepage.html").read_text(encoding="utf-8") + parser = _build_parser() + + parser._parse_site_page(homepage) + parser._parse_user_base_info(homepage) + + assert parser.upload == round(105.06 * 1024 ** 3) + assert parser.download == 10 * 1024 ** 3 + assert parser.ratio == 10.506 + assert parser.userid == "xxxx" + assert parser.username == "xxxx" + + parser._parse_user_detail_info( + '' + '
上传量1 GB
下载量2 GB
分享率0.5
' + ) + + assert parser.upload == round(105.06 * 1024 ** 3) + assert parser.download == 10 * 1024 ** 3 + assert parser.ratio == 10.506 + + +@pytest.mark.parametrize("metrics, legacy, expected_upload, expected_download, expected_ratio", [ + ('
上传量 功行
8 GB
' + '
下载量 所耗资粮
2 GB
' + '
分享率 取予有度
4.001
', + "上传量: 1 GB 下载量: 1 GB 分享率: 1", 8 * 1024 ** 3, 2 * 1024 ** 3, 4.001), + ('
上传量 功行
8 GB
' + '
下载量 所耗资粮
2 GB
', + "", 8 * 1024 ** 3, 2 * 1024 ** 3, 4.0), + ('
上传量 功行
8 GB
', + "下载量: 2 GB", 8 * 1024 ** 3, 2 * 1024 ** 3, 4.0), + ('
上传量 功行
0 B
' + '
下载量 所耗资粮
0 B
' + '
分享率 取予有度
0
', + "上传量: 1 GB 下载量: 1 GB 分享率: 1", 0, 0, 0.0), + ('
上传量 功行
8 GB
' + '
下载量 所耗资粮
0 B
', + "", 8 * 1024 ** 3, 0, 0.0), + ('
上传量 功行
', + "上传量: 1 GB 下载量: 2 GB 分享率: 0.501", 1024 ** 3, 2 * 1024 ** 3, 0.501), +], ids=["displayed-ratio", "calculated-ratio", "partial-metrics", "zero-values", "zero-download", "empty-value"]) +def test_nexus_php_cspt_metrics_prefer_displayed_values( + metrics: str, legacy: str, expected_upload: int, expected_download: int, expected_ratio: float +) -> None: + """ + 概览指标优先取页面显示值,缺失或为空时沿用传统字段及分享率计算。 + """ + parser = _build_parser() + + parser._parse_user_traffic_info( + f'{legacy}
{metrics}
' + ) + + assert parser.upload == expected_upload + assert parser.download == expected_download + assert parser.ratio == expected_ratio + + +@pytest.mark.parametrize("class_name, upload_label", [ + ("other-metrics", "上传量"), + ("cultivation-metrics-other", "上传量"), + ("cultivation-metrics", "实际上传量"), +], ids=["unrelated-list", "class-substring", "actual-upload"]) +def test_nexus_php_cspt_metrics_ignore_unrelated_fields(class_name: str, upload_label: str) -> None: + """ + 不把其他列表或实际流量字段识别为账户总流量。 + """ + parser = _build_parser() + + parser._parse_user_traffic_info( + f'
' + f'
{upload_label} 功行
8 GB
' + '
' + ) + + assert (parser.upload, parser.download, parser.ratio) == (0, 0, 0.0) + + +@pytest.mark.parametrize("html_text", ["", ""]) +def test_nexus_php_empty_traffic_page(html_text: str) -> None: + """ + 空响应或空页面没有概览指标时,保持零流量且不抛出异常。 + """ + parser = _build_parser() + + parser._parse_user_traffic_info(html_text) + + assert (parser.upload, parser.download, parser.ratio) == (0, 0, 0.0) + + def test_nexus_php_depthstudio_traffic_falls_back_to_details(): """ 首页解析失败后从详情表格补齐流量,不依赖卡片类名或实际流量。