Files
MediaCrawler/tests/test_platforms.py
T
butubb 4e60524f37
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s
feat: 监控面板 / 登录鉴权 / 多平台切换 / MySQL
在上游 MediaCrawler 之上新增一层:

- 监控层 api/monitor/ —— 多博主/多笔记的定时采集、指标快照差分、报表、
  企业微信通知。每轮采集写入独立目录,差分才成立。
- WebUI 登录鉴权 api/auth.py —— PBKDF2 口令 + 服务端会话,/api 全接口防护。
  WebSocket 单独加依赖:BaseHTTPMiddleware 对 ws 作用域直接放行,覆盖不到。
- 全局平台切换 + 能力矩阵 —— 如实区分「爬虫模块支持」与「监控层已接线」,
  未接通的平台直接拒绝建任务,而不是静默跑空。
- 监控库改用 MySQL 5.7(可回退 SQLite 供测试):逐表强制 utf8mb4
  (服务端与库默认都是 latin1),启动校验所连 schema 以防写错库,
  连接池 recycle + pre_ping 应对 MySQL 的 8 小时空闲断连。

修复上游缺陷:

- xhs/core.py: 主页抓取失败会跳掉整个博主,导致一条作品都抓不到,
  而那份资料只喂给一个空函数。改为尽力而为,失败不中断。
- xhs/login.py: cookie 登录只注入 web_session,冷启动签名会失败。
  新增 INJECT_ALL_COOKIES 开关(默认关闭,原有行为不变)。
- requirements.txt: 补上 websockets。它在上游 pyproject.toml 里有声明、
  这里漏了,导致 uvicorn 没有 WebSocket 能力,实时日志流从未工作。

改动过的上游文件清单及合并方式见 UPSTREAM.md。

测试:492 passed(另有 1 个既有的 Windows/gbk 上游测试失败,与本改动无关)
2026-10-07 09:58:40 +08:00

303 lines
12 KiB
Python

# -*- coding: utf-8 -*-
# Copyright (c) 2025 [email protected]
#
# This file is part of MediaCrawler project.
# Repository: https://github.com/NanmiCoder/MediaCrawler/blob/main/tests/test_platforms.py
# GitHub: https://github.com/NanmiCoder
# Licensed under NON-COMMERCIAL LEARNING LICENSE 1.1
#
# 声明:本代码仅供学习和研究目的使用。使用者应遵守以下原则:
# 1. 不得用于任何商业用途。
# 2. 使用时应遵守目标平台的使用条款和robots.txt规则。
# 3. 不得进行大规模爬取或对平台造成运营干扰。
# 4. 应合理控制请求频率,避免给目标平台带来不必要的负担。
# 5. 不得用于任何非法或不当的用途。
#
# 详细许可条款请参阅根目录下的LICENSE文件。
# 使用本代码即表示您同意遵守上述原则和LICENSE中的所有条款。
"""Platform capability matrix, platform scoping, and per-platform settings."""
import httpx
import pytest
import pytest_asyncio
from sqlalchemy import text
from api.main import app
from api.monitor import db as monitor_db
from api.monitor import platforms
from api.monitor.models import MonitorTask
XHS_TARGET = "5f58bd990000000001003753"
@pytest_asyncio.fixture
async def client(tmp_path):
monitor_db.set_sqlite_path(tmp_path / "monitor.db")
await monitor_db.init_db()
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(transport=transport, base_url="http://test") as http_client:
yield http_client
await monitor_db.dispose_engine()
class TestCapabilityMatrix:
@pytest.mark.asyncio
async def test_matrix_is_exposed_to_the_ui(self, client):
body = (await client.get("/api/config/platforms")).json()
by_value = {p["value"]: p for p in body["platforms"]}
assert set(by_value) == {"xhs", "dy", "ks", "bili", "wb", "tieba", "zhihu"}
# Every entry must say whether monitoring is actually wired up -- this is
# what stops the UI offering a platform that can never produce data.
assert all("monitor_wired" in p for p in body["platforms"])
assert by_value["xhs"]["monitor_wired"] is True
assert by_value["dy"]["monitor_wired"] is False
@pytest.mark.asyncio
async def test_metrics_are_per_platform_and_labelled(self, client):
body = (await client.get("/api/config/platforms")).json()
by_value = {p["value"]: p for p in body["platforms"]}
# Bilibili has play count and danmaku; Xiaohongshu has neither.
assert "video_play_count" in by_value["bili"]["metrics"]
assert "video_danmaku" in by_value["bili"]["metrics"]
assert "video_play_count" not in by_value["xhs"]["metrics"]
# Every metric shown to a user must have a human label.
for capability in body["platforms"]:
for metric in capability["metrics"]:
assert capability["metric_labels"][metric]
def test_unknown_platform_is_not_monitor_wired(self):
assert platforms.is_known("xhs") is True
assert platforms.is_known("myspace") is False
assert platforms.is_monitor_wired("myspace") is False
class TestTaskCreationGuard:
@pytest.mark.asyncio
async def test_unwired_platform_is_rejected_with_an_explanation(self, client):
"""Accepting it would create a task that silently never produces data."""
response = await client.post(
"/api/monitor/tasks",
json={"name": "抖音任务", "mode": "creator", "platform": "dy", "targets": ["x"]},
)
assert response.status_code == 400
detail = response.json()["detail"]
assert "抖音" in detail
assert "尚未接通" in detail
@pytest.mark.asyncio
async def test_unknown_platform_is_rejected(self, client):
response = await client.post(
"/api/monitor/tasks",
json={"name": "x", "mode": "creator", "platform": "myspace", "targets": ["x"]},
)
assert response.status_code == 400
@pytest.mark.asyncio
async def test_no_task_row_is_created_when_rejected(self, client):
await client.post(
"/api/monitor/tasks",
json={"name": "抖音任务", "mode": "creator", "platform": "dy", "targets": ["x"]},
)
assert (await client.get("/api/monitor/tasks")).json()["tasks"] == []
@pytest.mark.asyncio
async def test_xhs_still_works_and_is_the_default(self, client):
explicit = await client.post(
"/api/monitor/tasks",
json={"name": "显式", "mode": "creator", "platform": "xhs", "targets": [XHS_TARGET]},
)
assert explicit.status_code == 201
defaulted = await client.post(
"/api/monitor/tasks",
json={"name": "默认", "mode": "creator", "targets": [XHS_TARGET]},
)
assert defaulted.status_code == 201
tasks = (await client.get("/api/monitor/tasks")).json()["tasks"]
assert {t["platform"] for t in tasks} == {"xhs"}
class TestPlatformScoping:
async def _seed_two_platforms(self, client):
"""One real XHS task plus a Douyin task inserted directly, since the API
refuses to create the latter."""
await client.post(
"/api/monitor/tasks",
json={"name": "小红书任务", "mode": "creator", "targets": [XHS_TARGET]},
)
async with monitor_db.get_session() as session:
session.add(
MonitorTask(
name="抖音任务", platform="dy", mode="creator", enabled=True,
interval_minutes=60, max_notes_count=20, enable_comments=True,
max_comments_count=50, run_timeout_seconds=3600,
notify_enabled=False, created_at=0, updated_at=0,
)
)
@pytest.mark.asyncio
async def test_tasks_are_filtered_by_platform(self, client):
await self._seed_two_platforms(client)
all_tasks = (await client.get("/api/monitor/tasks")).json()["tasks"]
assert len(all_tasks) == 2
xhs_only = (await client.get("/api/monitor/tasks", params={"platform": "xhs"})).json()
assert [t["name"] for t in xhs_only["tasks"]] == ["小红书任务"]
dy_only = (await client.get("/api/monitor/tasks", params={"platform": "dy"})).json()
assert [t["name"] for t in dy_only["tasks"]] == ["抖音任务"]
@pytest.mark.asyncio
async def test_overview_is_scoped(self, client):
await self._seed_two_platforms(client)
assert (await client.get("/api/monitor/overview")).json()["tasks"] == 2
assert (
await client.get("/api/monitor/overview", params={"platform": "xhs"})
).json()["tasks"] == 1
@pytest.mark.asyncio
async def test_a_platform_with_no_tasks_yields_empty_not_everything(self, client):
"""An empty task set must not degrade into "no filter"."""
await self._seed_two_platforms(client)
body = (await client.get("/api/monitor/notes", params={"platform": "bili"})).json()
assert body["notes"] == []
report = (
await client.get("/api/monitor/report", params={"platform": "bili"})
).json()
assert report["totals"]["liked_count_delta"] == 0
assert report["note_count"] == 0
class TestPerPlatformSettings:
@pytest.mark.asyncio
async def test_each_platform_keeps_its_own_values(self, client):
await client.put(
"/api/settings",
params={"platform": "xhs"},
json={"platform.xhs.crawl_sleep_sec": 3},
)
await client.put(
"/api/settings",
params={"platform": "dy"},
json={"platform.dy.crawl_sleep_sec": 9},
)
xhs = (await client.get("/api/settings", params={"platform": "xhs"})).json()
dy = (await client.get("/api/settings", params={"platform": "dy"})).json()
assert xhs["values"]["platform.xhs.crawl_sleep_sec"] == 3
assert dy["values"]["platform.dy.crawl_sleep_sec"] == 9
@pytest.mark.asyncio
async def test_system_settings_are_shared_across_platforms(self, client):
await client.put(
"/api/settings",
params={"platform": "xhs"},
json={"system.active_hours_start": 8},
)
dy = (await client.get("/api/settings", params={"platform": "dy"})).json()
assert dy["values"]["system.active_hours_start"] == 8
# ...and the system specs are present in every platform's response.
assert "system.active_hours_end" in dy["values"]
@pytest.mark.asyncio
async def test_a_key_for_another_platform_is_rejected(self, client):
"""Writing xhs's key while scoped to dy would land somewhere unexpected."""
response = await client.put(
"/api/settings",
params={"platform": "dy"},
json={"platform.xhs.crawl_sleep_sec": 5},
)
assert response.status_code == 400
@pytest.mark.asyncio
async def test_cookies_are_per_platform(self, client):
await client.post(
"/api/monitor/cookie",
params={"platform": "xhs"},
json={"cookie": "web_session=xhs-secret"},
)
xhs = (await client.get("/api/monitor/cookie", params={"platform": "xhs"})).json()
dy = (await client.get("/api/monitor/cookie", params={"platform": "dy"})).json()
assert xhs["present"] is True
assert dy["present"] is False
# The old endpoint still defaults to Xiaohongshu.
assert (await client.get("/api/monitor/cookie")).json()["present"] is True
class TestLegacyKeyMigration:
@pytest.mark.asyncio
async def test_old_flat_keys_are_moved_to_the_new_namespace(self, tmp_path):
"""Existing installs must not lose their cookie on upgrade."""
monitor_db.set_sqlite_path(tmp_path / "monitor.db")
await monitor_db.init_db()
async with monitor_db.get_engine().begin() as conn:
await conn.execute(
text(
"INSERT INTO monitor_setting (key, value, updated_at) "
"VALUES ('xhs_cookie', 'web_session=legacy', 1)"
)
)
await conn.execute(
text(
"INSERT INTO monitor_setting (key, value, updated_at) "
"VALUES ('wecom_webhook', 'https://qyapi.weixin.qq.com/x', 1)"
)
)
# Re-running init performs the rename.
await monitor_db.init_db()
async with monitor_db.get_engine().begin() as conn:
rows = dict(
(await conn.execute(text("SELECT key, value FROM monitor_setting"))).all()
)
assert rows.get("platform.xhs.cookie") == "web_session=legacy"
assert rows.get("system.wecom_webhook") == "https://qyapi.weixin.qq.com/x"
assert "xhs_cookie" not in rows
assert "wecom_webhook" not in rows
await monitor_db.dispose_engine()
@pytest.mark.asyncio
async def test_migration_is_idempotent_and_keeps_the_newer_value(self, tmp_path):
monitor_db.set_sqlite_path(tmp_path / "monitor.db")
await monitor_db.init_db()
async with monitor_db.get_engine().begin() as conn:
await conn.execute(
text(
"INSERT INTO monitor_setting (key, value, updated_at) VALUES "
"('platform.xhs.cookie', 'current', 2), ('xhs_cookie', 'stale', 1)"
)
)
await monitor_db.init_db()
async with monitor_db.get_engine().begin() as conn:
rows = dict(
(await conn.execute(text("SELECT key, value FROM monitor_setting"))).all()
)
assert rows.get("platform.xhs.cookie") == "current"
assert "xhs_cookie" not in rows
await monitor_db.dispose_engine()