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:
@@ -0,0 +1,141 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""分享短链 —— 保存目标时联网跟一次跳转,解出真实地址。
|
||||
|
||||
不这么做的话,粘进去的是一串短码,被当成 user_id 存下来,采集时拿它去请求什么都抓不到
|
||||
(真事:任务 22,`https://xhslink.cn/o/AhQpHsqcme5`,报出来却是「疑似登录态失效」)。
|
||||
"""
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from api.monitor.service import expand_short_link, parse_target_input, prepare_target
|
||||
|
||||
# **真实的跳转**(实测 xhslink.cn/o/AhQpHsqcme5 的第一个 302),不是编的 ——
|
||||
# 这个地址的形状决定了能不能抠出 id 和 xsec_token。
|
||||
REAL_XHS_REDIRECT = (
|
||||
"https://www.xiaohongshu.com/user/profile/69d388f00000000032024a75"
|
||||
"?xsec_token=YBpTJH9kYbkTkgL-F4POKqd_kEM5A05smIjgbcyMSI2k4%3D"
|
||||
"&xsec_source=app_share&xhsshare=CopyLink&track_code=4s40v6M3mwa"
|
||||
)
|
||||
|
||||
|
||||
def _client_returning(monkeypatch, status_code: int, location: str):
|
||||
"""假装是那个短链服务器:只看 Location,不跟跳转。"""
|
||||
seen: dict = {}
|
||||
|
||||
class _Response:
|
||||
headers = {"location": location} if location else {}
|
||||
|
||||
_Response.status_code = status_code
|
||||
|
||||
class _Client:
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *exc):
|
||||
return False
|
||||
|
||||
async def get(self, url, **kwargs):
|
||||
seen["url"] = url
|
||||
seen.update(kwargs)
|
||||
return _Response()
|
||||
|
||||
monkeypatch.setattr(httpx, "AsyncClient", lambda **kwargs: _Client())
|
||||
return seen
|
||||
|
||||
|
||||
class TestExpandShortLink:
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_share_link_is_expanded(self, monkeypatch):
|
||||
_client_returning(monkeypatch, 302, REAL_XHS_REDIRECT)
|
||||
|
||||
expanded = await expand_short_link("https://xhslink.cn/o/AhQpHsqcme5", "xhs")
|
||||
|
||||
assert expanded == REAL_XHS_REDIRECT
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_it_asks_for_the_first_hop_only(self, monkeypatch):
|
||||
"""**不跟随跳转**:跟到底会落到登录页,反而把要的东西丢了。"""
|
||||
seen = _client_returning(monkeypatch, 302, REAL_XHS_REDIRECT)
|
||||
|
||||
await expand_short_link("https://xhslink.cn/o/AhQpHsqcme5", "xhs")
|
||||
|
||||
assert seen["follow_redirects"] is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_normal_link_is_not_even_requested(self, monkeypatch):
|
||||
"""普通链接不联网 —— 多发一个请求既慢又没有意义,还可能把 token 弄丢。"""
|
||||
seen = _client_returning(monkeypatch, 302, REAL_XHS_REDIRECT)
|
||||
|
||||
value = "https://www.xiaohongshu.com/user/profile/69d388f00000000032024a75"
|
||||
|
||||
assert await expand_short_link(value, "xhs") == value
|
||||
assert seen == {}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_bare_id_is_not_requested(self, monkeypatch):
|
||||
seen = _client_returning(monkeypatch, 302, REAL_XHS_REDIRECT)
|
||||
|
||||
assert await expand_short_link("69d388f00000000032024a75", "xhs") == (
|
||||
"69d388f00000000032024a75"
|
||||
)
|
||||
assert seen == {}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_dead_short_link_keeps_the_original_value(self, monkeypatch):
|
||||
"""码不存在时抖音会跳到首页 —— 那就解不出来。**保持原值,不报错**:
|
||||
链接校验已经关掉了,再造一个拒绝点自相矛盾。"""
|
||||
_client_returning(monkeypatch, 302, "https://www.douyin.com/")
|
||||
|
||||
assert await expand_short_link("https://v.douyin.com/zzz/", "dy") != ""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_network_error_keeps_the_original_value(self, monkeypatch):
|
||||
"""短链服务不通时不能把「新建任务」这个动作也带崩。"""
|
||||
|
||||
class _Boom:
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *exc):
|
||||
return False
|
||||
|
||||
async def get(self, *args, **kwargs):
|
||||
raise httpx.ConnectTimeout("boom")
|
||||
|
||||
monkeypatch.setattr(httpx, "AsyncClient", lambda **kwargs: _Boom())
|
||||
value = "https://xhslink.cn/o/AhQpHsqcme5"
|
||||
|
||||
assert await expand_short_link(value, "xhs") == value
|
||||
|
||||
|
||||
class TestPrepareTarget:
|
||||
@pytest.mark.asyncio
|
||||
async def test_the_real_short_link_yields_a_usable_target(self, monkeypatch):
|
||||
"""**这就是任务 22 该有的结果** —— 而不是把短链本身存成 external_id。"""
|
||||
_client_returning(monkeypatch, 302, REAL_XHS_REDIRECT)
|
||||
|
||||
parsed = await prepare_target("https://xhslink.cn/o/AhQpHsqcme5", "creator", "xhs")
|
||||
|
||||
assert parsed["external_id"] == "69d388f00000000032024a75"
|
||||
assert parsed["xsec_token"] == "YBpTJH9kYbkTkgL-F4POKqd_kEM5A05smIjgbcyMSI2k4="
|
||||
assert parsed["xsec_source"] == "app_share"
|
||||
# 原文留着,出问题时能回溯用户当初粘的是什么。
|
||||
assert parsed["raw_value"] == "https://xhslink.cn/o/AhQpHsqcme5"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_an_unexpandable_link_still_gets_stored(self, monkeypatch):
|
||||
"""解不开也照存 —— 这是「完全不校验」的既定行为,不能在这里偷偷加一个拒绝点。"""
|
||||
_client_returning(monkeypatch, 302, "")
|
||||
|
||||
parsed = await prepare_target("https://xhslink.cn/o/dead", "creator", "xhs")
|
||||
|
||||
assert parsed["external_id"] == "https://xhslink.cn/o/dead"
|
||||
|
||||
|
||||
class TestParseIsStillPure:
|
||||
"""纯函数那层没变 —— 短链解不开时它仍然只能原样存,网络那一步在外面。"""
|
||||
|
||||
def test_parse_alone_does_not_expand(self):
|
||||
parsed = parse_target_input("https://xhslink.cn/o/AhQpHsqcme5", "creator", "xhs")
|
||||
assert parsed["external_id"] == "https://xhslink.cn/o/AhQpHsqcme5"
|
||||
Reference in New Issue
Block a user