feat(monitor): 粘贴视频链接换出博主主页
运营手上常常只有一条作品的分享链接,可监控任务认的是主页里的 sec_uid。 - 从整段分享文案里抠链接(链接前后都是字,按行切切不开),短链先展开, 再从中取作品 id,最后用详情接口换 author.sec_uid - 一次最多 20 条,串行 + 0.4 秒间隔;单条失败只在那一条上报错,不连累整批 - normalize_aweme 补上 creator_id:哈希拼不出主页地址,顺带让以后入库的 抖音作品也带主页链接 - 监控页左上角「视频转主页」,支持逐条复制和一键复制全部(http 下没有 navigator.clipboard,走 execCommand 兜底)
This commit is contained in:
@@ -12,7 +12,7 @@ import asyncio
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from api.monitor import douyin_api
|
||||
from api.monitor import adapters, douyin_api
|
||||
|
||||
|
||||
class TestCookieParsing:
|
||||
@@ -105,6 +105,26 @@ class TestNormalizeAweme:
|
||||
# 指标按 store 的形态落成字符串,交给 ingest 的 parse_count 解析。
|
||||
assert record["liked_count"] == "3"
|
||||
assert record["collected_count"] == "2"
|
||||
# 这条 payload 里没有 sec_uid:主页 id 就空着,不许拿 uid 冒充。
|
||||
assert record["creator_id"] == ""
|
||||
|
||||
def test_creator_id_is_the_sec_uid_not_the_uid(self):
|
||||
"""主页链接只能由 sec_uid 拼出来 —— uid 是数字、creator_hash 是单向哈希。"""
|
||||
record = douyin_api.normalize_aweme(
|
||||
{
|
||||
"aweme_id": "1",
|
||||
"author": {
|
||||
"uid": "776719710825195",
|
||||
"sec_uid": "MS4wLjABAAAAcmv4-Oaxe7FN4bdQECz1knSBzKb50uhnykbLIArdkk7",
|
||||
"nickname": "AA建材王总",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
assert record["creator_id"] == "MS4wLjABAAAAcmv4-Oaxe7FN4bdQECz1knSBzKb50uhnykbLIArdkk7"
|
||||
assert adapters.creator_url("dy", record["creator_id"]).startswith(
|
||||
"https://www.douyin.com/user/"
|
||||
)
|
||||
|
||||
def test_hashtags_come_from_text_extra_not_the_truncated_desc(self):
|
||||
"""`desc` 会在话题列表中途被截断,后半段话题只在 `text_extra` 里。
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""视频链接 → 博主主页:链接抠取、id 解析、批量解析的容错。"""
|
||||
|
||||
import pytest
|
||||
|
||||
from api.monitor import douyin_api, service, video_link
|
||||
|
||||
SEC_UID = "MS4wLjABAAAAcmv4-Oaxe7FN4bdQECz1knSBzKb50uhnykbLIArdkk7ennhVy6Gpwp11XkVIDPgY"
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _no_gap(monkeypatch):
|
||||
"""测试里不需要真的等那 0.4 秒节流。"""
|
||||
monkeypatch.setattr(video_link, "REQUEST_GAP_SECONDS", 0)
|
||||
|
||||
|
||||
def test_extract_candidates_pulls_links_out_of_share_text():
|
||||
text = """7.42 复制打开抖音,看看【锋哥的作品】 https://v.douyin.com/iAbCdEf/ 复制此链接
|
||||
https://www.douyin.com/video/7353137957861084427
|
||||
7353137957861084427
|
||||
|
||||
这一行是说明文字,应该被忽略
|
||||
"""
|
||||
assert video_link.extract_candidates(text) == [
|
||||
"https://v.douyin.com/iAbCdEf/",
|
||||
"https://www.douyin.com/video/7353137957861084427",
|
||||
"7353137957861084427",
|
||||
]
|
||||
|
||||
|
||||
def test_a_line_with_two_links_yields_both():
|
||||
text = "https://v.douyin.com/aaa/ https://v.douyin.com/bbb/"
|
||||
|
||||
assert len(video_link.extract_candidates(text)) == 2
|
||||
|
||||
|
||||
def test_aweme_id_from_url_handles_the_known_shapes():
|
||||
assert video_link.aweme_id_from_url("https://www.douyin.com/video/7353137957861084427") == (
|
||||
"7353137957861084427"
|
||||
)
|
||||
# 短链展开后落到的分享页
|
||||
assert video_link.aweme_id_from_url(
|
||||
"https://www.iesdouyin.com/share/video/7353137957861084427/?region=CN"
|
||||
) == "7353137957861084427"
|
||||
# 直播/精选页那种把作品挂在 modal 参数上的
|
||||
assert video_link.aweme_id_from_url(
|
||||
"https://www.douyin.com/discover?modal_id=7353137957861084427"
|
||||
) == "7353137957861084427"
|
||||
assert video_link.aweme_id_from_url("7353137957861084427") == "7353137957861084427"
|
||||
|
||||
|
||||
def test_aweme_id_from_url_rejects_a_creator_link():
|
||||
"""主页链接不是作品链接 —— 认不出来就该空着,而不是瞎猜一个 id。"""
|
||||
assert video_link.aweme_id_from_url(f"https://www.douyin.com/user/{SEC_UID}") == ""
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_builds_a_profile_url(monkeypatch):
|
||||
async def fake_detail(aweme_id, **_):
|
||||
return {"aweme_id": aweme_id, "nickname": "锋哥苏州寻宝藏小店", "creator_id": SEC_UID}
|
||||
|
||||
monkeypatch.setattr(douyin_api, "video_detail", fake_detail)
|
||||
|
||||
result = await video_link.resolve("https://www.douyin.com/video/7353137957861084427")
|
||||
|
||||
assert result["truncated"] is False
|
||||
item = result["results"][0]
|
||||
assert item["ok"] is True
|
||||
assert item["nickname"] == "锋哥苏州寻宝藏小店"
|
||||
assert item["profile_url"] == f"https://www.douyin.com/user/{SEC_UID}"
|
||||
assert item["error"] == ""
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_short_links_are_expanded_before_parsing(monkeypatch):
|
||||
calls = []
|
||||
|
||||
async def fake_expand(value, platform):
|
||||
calls.append((value, platform))
|
||||
return "https://www.iesdouyin.com/share/video/7353137957861084427/?region=CN"
|
||||
|
||||
async def fake_detail(aweme_id, **_):
|
||||
return {"aweme_id": aweme_id, "nickname": "锋哥", "creator_id": SEC_UID}
|
||||
|
||||
monkeypatch.setattr(service, "expand_short_link", fake_expand)
|
||||
monkeypatch.setattr(douyin_api, "video_detail", fake_detail)
|
||||
|
||||
result = await video_link.resolve("https://v.douyin.com/iAbCdEf/")
|
||||
|
||||
assert calls == [("https://v.douyin.com/iAbCdEf/", "dy")]
|
||||
assert result["results"][0]["aweme_id"] == "7353137957861084427"
|
||||
assert result["results"][0]["ok"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_the_same_video_twice_only_hits_the_api_once(monkeypatch):
|
||||
calls = []
|
||||
|
||||
async def fake_detail(aweme_id, **_):
|
||||
calls.append(aweme_id)
|
||||
return {"aweme_id": aweme_id, "nickname": "锋哥", "creator_id": SEC_UID}
|
||||
|
||||
monkeypatch.setattr(douyin_api, "video_detail", fake_detail)
|
||||
|
||||
result = await video_link.resolve(
|
||||
"https://www.douyin.com/video/7353137957861084427\n"
|
||||
"https://www.douyin.com/video/7353137957861084427"
|
||||
)
|
||||
|
||||
assert calls == ["7353137957861084427"]
|
||||
# 两条输入各自一行结果,但只有一次请求。
|
||||
assert [item["profile_url"] for item in result["results"]] == [
|
||||
f"https://www.douyin.com/user/{SEC_UID}",
|
||||
f"https://www.douyin.com/user/{SEC_UID}",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_one_failure_does_not_sink_the_batch(monkeypatch):
|
||||
async def fake_detail(aweme_id, **_):
|
||||
if aweme_id == "1111111111111111111":
|
||||
raise douyin_api.DouyinApiError("接口拒绝了请求")
|
||||
return {"aweme_id": aweme_id, "nickname": "好号", "creator_id": SEC_UID}
|
||||
|
||||
monkeypatch.setattr(douyin_api, "video_detail", fake_detail)
|
||||
|
||||
result = await video_link.resolve(
|
||||
"https://www.douyin.com/video/1111111111111111111\n"
|
||||
"https://www.douyin.com/video/7353137957861084427"
|
||||
)
|
||||
|
||||
first, second = result["results"]
|
||||
assert first["ok"] is False and "接口拒绝了" in first["error"]
|
||||
assert second["ok"] is True and second["profile_url"].endswith(SEC_UID)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_missing_sec_uid_is_reported_not_faked(monkeypatch):
|
||||
async def fake_detail(aweme_id, **_):
|
||||
return {"aweme_id": aweme_id, "nickname": "空号", "creator_id": ""}
|
||||
|
||||
monkeypatch.setattr(douyin_api, "video_detail", fake_detail)
|
||||
|
||||
result = await video_link.resolve("https://www.douyin.com/video/7353137957861084427")
|
||||
|
||||
item = result["results"][0]
|
||||
assert item["ok"] is False
|
||||
assert item["profile_url"] == ""
|
||||
assert "sec_uid" in item["error"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_more_than_the_limit_is_truncated(monkeypatch):
|
||||
async def fake_detail(aweme_id, **_):
|
||||
return {"aweme_id": aweme_id, "nickname": "x", "creator_id": SEC_UID}
|
||||
|
||||
monkeypatch.setattr(douyin_api, "video_detail", fake_detail)
|
||||
text = "\n".join(f"https://www.douyin.com/video/{1000000000000000000 + i}" for i in range(25))
|
||||
|
||||
result = await video_link.resolve(text)
|
||||
|
||||
assert result["truncated"] is True
|
||||
assert len(result["results"]) == video_link.MAX_LINKS
|
||||
|
||||
|
||||
def test_garbage_input_yields_nothing():
|
||||
assert video_link.extract_candidates("随便说点什么") == []
|
||||
assert video_link.extract_candidates("") == []
|
||||
Reference in New Issue
Block a user