上游爬虫本身不缺抖音能力(三模式、四项指标、二级评论都与小红书对等、指标还是同名同列),
缺的全在监控层的适配。这次把「平台之间不一样」的管子集中到一个新模块,再把散落的
xhs 硬编码接上去。
* 新增 api/monitor/adapters.py:产物目录名、jsonl 字段别名、目标链接形态与正则、
通知链接模板。不放进 platforms.py 是因为那个模块被 describe_all() 整个序列化进
/api/config/platforms 交给前端,塞进正则和目录名会让爬虫内部细节漏进 API 载荷。
代价是两个注册表可能漂移,用一条测试钉住「声明接通就必须有适配器」。
* 两个必须知道的坑,都在这版里处理掉了:
1) 抖音的平台 id 是 dy,而 store 把产物写在 douyin/ 下(store/douyin/_store_impl.py:47)。
不改就是 ingest 一个文件都读不到 —— 不报错,只是 0 条,然后被冒充成「疑似登录失效」。
2) 抖音的作品没有 note_id(叫 aweme_id)、评论也用 aweme_id 指作品。ingest 第一步是
`if not note_id: continue`,不映射就逐条全丢。
另外抖音顶层评论的 parent_comment_id 是字符串 "0",归一成空串,免得前端多出悬空的父节点。
* 顺带把「东西抓到了、只是没落在期望目录里」单独识别出来。这类故障的现象和登录失效
一模一样,按登录失效报会把人指去查完全错误的方向。
* 修两个既有 bug(今天只有小红书所以无害,加抖音就踩响):
- service.py update_task 换目标时漏传 task.platform,回落到默认小红书
- scheduler.py 取 cookie 没传 platform,抖音任务会读着小红书那份 cookie 不动
* 行为变更(已与用户确认):cookie 闸门改成「没 cookie 且没开 CDP」才跳过。
CDP 模式下登录态来自被接管的浏览器,粘不粘 cookie 由不得它决定;不放行的话,
选了「接管已有 Chrome」却没粘 cookie 的用户会看到任务永远不触发,而且不报错。
副作用是开启了 CDP 的小红书任务也不再被该闸门拦住 —— 语义上是对的。
* 目标输入框的示例链接与措辞改由能力矩阵提供(notes_label 抖音说「作品」、小红书说
「笔记」;「建议只填纯 ID」是小红书专属劝告,抖音链接不带令牌,不再显示)。
测试 +22 条(858 通过),其中最关键的是「抖音作品/评论不被静默丢弃」与「产物目录名
不等于平台 id」两条 —— 都是把最难查的失败模式钉死在回归网里。
注意:抖音这条路的**端到端尚未验证**,需要一份可用的抖音登录态(CDP 那台 Chrome 里
登录,或导出一份 cookie)。单测覆盖的是解析与入库,真实抓取还没跑过。
198 lines
6.6 KiB
Python
198 lines
6.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
import pytest
|
|
import config
|
|
from unittest.mock import AsyncMock, patch
|
|
from fastapi.testclient import TestClient
|
|
from cmd_arg import parse_cmd
|
|
from api.schemas import CrawlerStartRequest, PlatformEnum, LoginTypeEnum, CrawlerTypeEnum
|
|
from api.services.crawler_manager import CrawlerManager
|
|
from api.main import app
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cmd_arg_crawler_max_notes_count():
|
|
# Store original values
|
|
orig_notes = config.CRAWLER_MAX_NOTES_COUNT
|
|
orig_comments = config.CRAWLER_MAX_COMMENTS_COUNT_SINGLENOTES
|
|
|
|
try:
|
|
await parse_cmd([
|
|
"--platform", "xhs",
|
|
"--crawler_max_notes_count", "42",
|
|
"--max_comments_count_singlenotes", "24"
|
|
])
|
|
assert config.CRAWLER_MAX_NOTES_COUNT == 42
|
|
assert config.CRAWLER_MAX_COMMENTS_COUNT_SINGLENOTES == 24
|
|
finally:
|
|
config.CRAWLER_MAX_NOTES_COUNT = orig_notes
|
|
config.CRAWLER_MAX_COMMENTS_COUNT_SINGLENOTES = orig_comments
|
|
|
|
def test_douyin_monitor_command_uses_the_right_flags():
|
|
"""抖音监控任务拼出来的命令行。
|
|
|
|
与 runner 走的是同一条 _build_command 路径,所以这一条能守住「监控任务的参数
|
|
没拼错」—— 尤其是平台值必须是 dy(而不是 douyin),否则上游根本认不出平台。
|
|
"""
|
|
cm = CrawlerManager()
|
|
req = CrawlerStartRequest(
|
|
platform=PlatformEnum.DOUYIN,
|
|
login_type=LoginTypeEnum.COOKIE,
|
|
crawler_type=CrawlerTypeEnum.CREATOR,
|
|
creator_ids="https://www.douyin.com/user/MS4wLjABAAAATJPY7LAlaa5X",
|
|
save_data_path="./data/monitor_runs/1/2",
|
|
enable_cdp_mode=True,
|
|
inject_all_cookies=True,
|
|
save_login_state=True,
|
|
max_notes_count=20,
|
|
max_comments_count=50,
|
|
)
|
|
cmd = cm._build_command(req)
|
|
|
|
idx = cmd.index("--platform")
|
|
assert cmd[idx + 1] == "dy"
|
|
idx = cmd.index("--type")
|
|
assert cmd[idx + 1] == "creator"
|
|
idx = cmd.index("--creator_id")
|
|
assert cmd[idx + 1] == "https://www.douyin.com/user/MS4wLjABAAAATJPY7LAlaa5X"
|
|
idx = cmd.index("--enable_cdp_mode")
|
|
assert cmd[idx + 1] == "true"
|
|
|
|
|
|
def test_crawler_manager_build_command():
|
|
cm = CrawlerManager()
|
|
|
|
# 1. No max limits passed in API request
|
|
req1 = CrawlerStartRequest(
|
|
platform=PlatformEnum.XHS,
|
|
login_type=LoginTypeEnum.QRCODE,
|
|
crawler_type=CrawlerTypeEnum.SEARCH,
|
|
keywords="test",
|
|
max_notes_count=None,
|
|
max_comments_count=None
|
|
)
|
|
cmd1 = cm._build_command(req1)
|
|
# Check that the custom arguments are NOT present
|
|
assert "--crawler_max_notes_count" not in cmd1
|
|
assert "--max_comments_count_singlenotes" not in cmd1
|
|
|
|
# 2. Both limits passed in API request
|
|
req2 = CrawlerStartRequest(
|
|
platform=PlatformEnum.XHS,
|
|
login_type=LoginTypeEnum.QRCODE,
|
|
crawler_type=CrawlerTypeEnum.SEARCH,
|
|
keywords="test",
|
|
max_notes_count=50,
|
|
max_comments_count=5
|
|
)
|
|
cmd2 = cm._build_command(req2)
|
|
# Check that they are correctly added
|
|
assert "--crawler_max_notes_count" in cmd2
|
|
idx_notes = cmd2.index("--crawler_max_notes_count")
|
|
assert cmd2[idx_notes + 1] == "50"
|
|
|
|
assert "--max_comments_count_singlenotes" in cmd2
|
|
idx_comments = cmd2.index("--max_comments_count_singlenotes")
|
|
assert cmd2[idx_comments + 1] == "5"
|
|
|
|
|
|
def test_crawler_manager_passes_media_switch():
|
|
cm = CrawlerManager()
|
|
|
|
req_off = CrawlerStartRequest(
|
|
platform=PlatformEnum.XHS,
|
|
login_type=LoginTypeEnum.QRCODE,
|
|
crawler_type=CrawlerTypeEnum.DETAIL,
|
|
specified_ids="note-1",
|
|
)
|
|
cmd_off = cm._build_command(req_off)
|
|
idx_off = cmd_off.index("--get_media")
|
|
assert cmd_off[idx_off + 1] == "false"
|
|
|
|
req_on = CrawlerStartRequest(
|
|
platform=PlatformEnum.XHS,
|
|
login_type=LoginTypeEnum.QRCODE,
|
|
crawler_type=CrawlerTypeEnum.DETAIL,
|
|
specified_ids="note-1",
|
|
enable_media=True,
|
|
)
|
|
cmd_on = cm._build_command(req_on)
|
|
idx_on = cmd_on.index("--get_media")
|
|
assert cmd_on[idx_on + 1] == "true"
|
|
|
|
|
|
def test_api_schema_exposes_media_switch_default_off():
|
|
assert CrawlerStartRequest(platform=PlatformEnum.XHS).enable_media is False
|
|
|
|
def test_api_start_crawler_with_limits():
|
|
client = TestClient(app)
|
|
|
|
with patch("api.routers.crawler.crawler_manager.start", new_callable=AsyncMock) as mock_start:
|
|
mock_start.return_value = True
|
|
|
|
# Test case 1: with limits
|
|
response = client.post("/api/crawler/start", json={
|
|
"platform": "xhs",
|
|
"login_type": "qrcode",
|
|
"crawler_type": "search",
|
|
"keywords": "test",
|
|
"max_notes_count": 50,
|
|
"max_comments_count": 5
|
|
})
|
|
|
|
assert response.status_code == 200
|
|
assert response.json() == {"status": "ok", "message": "Crawler started successfully"}
|
|
|
|
mock_start.assert_called_once()
|
|
called_request = mock_start.call_args[0][0]
|
|
assert called_request.platform == PlatformEnum.XHS
|
|
assert called_request.max_notes_count == 50
|
|
assert called_request.max_comments_count == 5
|
|
|
|
def test_api_start_crawler_without_limits():
|
|
client = TestClient(app)
|
|
|
|
with patch("api.routers.crawler.crawler_manager.start", new_callable=AsyncMock) as mock_start:
|
|
mock_start.return_value = True
|
|
|
|
# Test case 2: without limits
|
|
response = client.post("/api/crawler/start", json={
|
|
"platform": "xhs",
|
|
"login_type": "qrcode",
|
|
"crawler_type": "search",
|
|
"keywords": "test"
|
|
})
|
|
|
|
assert response.status_code == 200
|
|
mock_start.assert_called_once()
|
|
called_request = mock_start.call_args[0][0]
|
|
assert called_request.platform == PlatformEnum.XHS
|
|
assert called_request.max_notes_count is None
|
|
assert called_request.max_comments_count is None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("field_name", "value"),
|
|
[
|
|
("max_notes_count", 0),
|
|
("max_notes_count", -1),
|
|
("max_notes_count", 10001),
|
|
("max_comments_count", 0),
|
|
("max_comments_count", -1),
|
|
("max_comments_count", 10001),
|
|
],
|
|
)
|
|
def test_api_rejects_invalid_limits(field_name, value):
|
|
client = TestClient(app)
|
|
payload = {
|
|
"platform": "xhs",
|
|
"login_type": "qrcode",
|
|
"crawler_type": "search",
|
|
"keywords": "test",
|
|
field_name: value,
|
|
}
|
|
|
|
with patch("api.routers.crawler.crawler_manager.start", new_callable=AsyncMock) as mock_start:
|
|
response = client.post("/api/crawler/start", json=payload)
|
|
|
|
assert response.status_code == 422
|
|
mock_start.assert_not_called()
|