超级编导的数据中心里已经有每条分发作品的抖音链接,不用再一条条粘。按 「分发活动创建时间」选一段区间 → 预览作品/博主/播放数据 → 一键建成作品 模式的监控任务,建完默认立刻采一轮,主页信息第一轮就补全。 - 活动列表不支持日期过滤(传了也被静默忽略),只能全量拉下来本地按 created_at 筛;边界按东八区算 —— 容器跑 UTC,按 localtime 会整体错开 8 小时。 - data_center 必须带 video_status=-1,不带默认 0,接口返回空列表。 - share_url 是双斜杠形态,建任务时只取裸作品 id;video_link 的 id 提取 也顺带认双斜杠了。 - 作品模式不拉创作者资料、没有账号快照,组头的主页链接一直出不来。 作品上新增 creator_id(author.sec_uid),优先快照、再用作品兜底。 - token 存设置页(secret),不进仓库。
211 lines
7.6 KiB
Python
211 lines
7.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""视频链接 → 博主主页:链接抠取、id 解析、批量解析的容错。"""
|
|
|
|
import httpx
|
|
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"
|
|
# 超级编导导出的链接是**双斜杠**形态。浏览器里点得开,所以不能按「坏链接」处理。
|
|
assert video_link.aweme_id_from_url(
|
|
"https://www.douyin.com/video//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_a_transport_hiccup_is_retried_once(monkeypatch):
|
|
"""容器刚起来时第一发可能撞上 DNS 失败;重试一次就过去了,别让它红掉一行。"""
|
|
calls = []
|
|
|
|
async def flaky_detail(aweme_id, **_):
|
|
calls.append(aweme_id)
|
|
if len(calls) == 1:
|
|
raise httpx.ConnectError("[Errno -3] Temporary failure in name resolution")
|
|
return {"aweme_id": aweme_id, "nickname": "锋哥", "creator_id": SEC_UID}
|
|
|
|
monkeypatch.setattr(douyin_api, "video_detail", flaky_detail)
|
|
monkeypatch.setattr(video_link, "_DETAIL_RETRY_SLEEP_SECONDS", 0)
|
|
|
|
result = await video_link.resolve("https://www.douyin.com/video/7353137957861084427")
|
|
|
|
assert len(calls) == 2
|
|
assert result["results"][0]["ok"] is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_an_empty_body_is_not_retried(monkeypatch):
|
|
"""空 body 是风控信号 —— 重试只会让它更确定。"""
|
|
calls = []
|
|
|
|
async def blocked_detail(aweme_id, **_):
|
|
calls.append(aweme_id)
|
|
raise douyin_api.DouyinApiError("接口返回了空内容")
|
|
|
|
monkeypatch.setattr(douyin_api, "video_detail", blocked_detail)
|
|
|
|
result = await video_link.resolve("https://www.douyin.com/video/7353137957861084427")
|
|
|
|
assert len(calls) == 1
|
|
assert result["results"][0]["ok"] is False
|
|
|
|
|
|
@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("") == []
|