feat(monitor): 保存目标时把分享短链解成完整链接
短链粘进来会被原样存成 external_id,而那是采集时唯一拿去请求的东西 —— 于是永远
抓不到东西(真事:任务 22 的 xhslink 短链,还报成了「疑似登录态失效」)。
做法:**GET 且不跟随跳转,直接读第一个 Location**。
* 小红书的 xhslink.cn/o/xxx —— 第一个 Location 就是
xiaohongshu.com/user/profile/<id>?xsec_token=...&xsec_source=...,要的都在里面。
跟到底反而会丢:它会一路跳到登录页。
* 抖音的 v.douyin.com 同样是 302 + Location;码不存在时跳到首页,那就解不出来。
解不出来时**返回原值、不报错** —— 链接校验已经按用户要求关掉了,再造一个拒绝点
自相矛盾。代价(那个目标抓不到东西)由 ingest 直说,不静默。
解析本身仍是纯函数,网络那一步单独放在 expand_short_link;两个调用点走
prepare_target。raw_value 记的仍是**用户当初粘的原文**(短链),不是解开后的地址 ——
解错目标时靠它回溯。
测试里的跳转 URL 用的是实测到的真实响应,不是编的。
This commit is contained in:
@@ -354,3 +354,69 @@ class TestDashboardQueries:
|
||||
overview = (await client.get("/api/monitor/overview")).json()
|
||||
assert overview["tasks"] == 0
|
||||
assert overview["notes"] == 0
|
||||
|
||||
|
||||
class TestShortLinksAreResolvedOnCreate:
|
||||
"""建任务时短链要**先解开再存** —— 存的是短码本身的话,采集永远抓不到东西。
|
||||
|
||||
这一条测的是**接线**(create_task / update_task 有没有走 prepare_target)。
|
||||
解短链本身的逻辑在 test_monitor_short_link.py 里测。
|
||||
"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_share_link_is_stored_expanded(self, client, monkeypatch):
|
||||
from api.monitor import service
|
||||
|
||||
real = "https://www.xiaohongshu.com/user/profile/69d388f00000000032024a75"
|
||||
|
||||
async def fake_expand(value, platform):
|
||||
return real
|
||||
|
||||
monkeypatch.setattr(service, "expand_short_link", fake_expand)
|
||||
|
||||
response = await client.post(
|
||||
"/api/monitor/tasks",
|
||||
json={
|
||||
"name": "短链",
|
||||
"mode": "creator",
|
||||
"platform": "xhs",
|
||||
"targets": ["https://xhslink.cn/o/AhQpHsqcme5"],
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
tasks = (await client.get("/api/monitor/tasks")).json()["tasks"]
|
||||
target = tasks[0]["targets"][0]
|
||||
assert target["external_id"] == "69d388f00000000032024a75"
|
||||
# 原文留着 —— 解错目标时靠它回溯用户当初粘的是什么。
|
||||
assert target["raw_value"] == "https://xhslink.cn/o/AhQpHsqcme5"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_updating_targets_resolves_them_too(self, client, monkeypatch):
|
||||
"""改目标走的是另一条路,同样不能漏。"""
|
||||
from api.monitor import service
|
||||
|
||||
async def fake_expand(value, platform):
|
||||
return "https://www.xiaohongshu.com/user/profile/aaaabbbbccccddddeeeeffff"
|
||||
|
||||
created = await client.post(
|
||||
"/api/monitor/tasks",
|
||||
json={
|
||||
"name": "改目标",
|
||||
"mode": "creator",
|
||||
"platform": "xhs",
|
||||
"targets": ["5f58bd990000000001003753"],
|
||||
},
|
||||
)
|
||||
assert created.status_code == 201
|
||||
task_id = (await client.get("/api/monitor/tasks")).json()["tasks"][0]["id"]
|
||||
monkeypatch.setattr(service, "expand_short_link", fake_expand)
|
||||
|
||||
response = await client.patch(
|
||||
f"/api/monitor/tasks/{task_id}",
|
||||
json={"targets": ["https://xhslink.cn/o/whatever"]},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
|
||||
tasks = (await client.get("/api/monitor/tasks")).json()["tasks"]
|
||||
assert tasks[0]["targets"][0]["external_id"] == "aaaabbbbccccddddeeeeffff"
|
||||
|
||||
Reference in New Issue
Block a user