feat(monitor): 作品模式也采博主粉丝数,从作品详情的 author 里拿
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

超级编导导入的是作品模式任务,从不问 user/profile,所以这些任务里每个博主
的粉丝数都是空的 —— 而运营要看的就是「这批号多大」。(之前只补了主页链接。)

详情返回的 author 里现成有 follower_count / total_favorited /
following_count,白拿的,不需要多发请求。aweme_count 实测是 null,作品数
留 None —— 为一个数字给每个博主多打一次资料接口不划算。

字段缺失时留 None 而不是 0:「粉丝 0」和「没采到」不是一回事。author 是
精简版(字段全缺)时不写快照行,免得界面上出现「有采集时间、没有数字」。
This commit is contained in:
2026-10-24 16:56:23 +08:00
parent 7d552c1206
commit d9fa5ecb99
4 changed files with 148 additions and 0 deletions
+23
View File
@@ -409,6 +409,19 @@ def _as_int(value: Any) -> int:
return 0 return 0
def _opt_int(value: Any) -> Optional[int]:
"""``_as_int`` 的「不知道就是不知道」版:字段缺失时留 None,不写 0。
粉丝数用 0 顶替字段缺失,界面上就成了「粉丝 0」—— 和「没采到」是两件事。
"""
if value is None or value == "":
return None
try:
return int(value)
except (TypeError, ValueError):
return None
def normalize_aweme(aweme: Dict[str, Any]) -> Dict[str, Any]: def normalize_aweme(aweme: Dict[str, Any]) -> Dict[str, Any]:
"""把接口返回的一条作品,翻译成 store 落盘的那套键名。 """把接口返回的一条作品,翻译成 store 落盘的那套键名。
@@ -446,6 +459,16 @@ def normalize_aweme(aweme: Dict[str, Any]) -> Dict[str, Any]:
# —— 不带出来,库里抖音作品的主页链接就全是空的。 # —— 不带出来,库里抖音作品的主页链接就全是空的。
"creator_id": str(author.get("sec_uid") or ""), "creator_id": str(author.get("sec_uid") or ""),
"nickname": nickname, "nickname": nickname,
# 作者账号级指标。详情接口的 ``author`` 里现成就有,**白拿的**:作品模式
# (超级编导导入就是)只拉作品详情、从不问 ``user/profile``,不从这里带出来,
# 那些任务里所有博主的粉丝数就都是空的。
#
# ``aweme_count`` 实测是 null(详情返回里没有作品总数),所以 works 只能留空 ——
# 为了一个作品数给每个博主多打一次资料接口,不划算。
"creator_fans": _opt_int(author.get("follower_count")),
"creator_total_favorited": _opt_int(author.get("total_favorited")),
"creator_works": _opt_int(author.get("aweme_count")),
"creator_following": _opt_int(author.get("following_count")),
"liked_count": str(_as_int(statistics.get("digg_count"))), "liked_count": str(_as_int(statistics.get("digg_count"))),
"comment_count": str(_as_int(statistics.get("comment_count"))), "comment_count": str(_as_int(statistics.get("comment_count"))),
"collected_count": str(_as_int(statistics.get("collect_count"))), "collected_count": str(_as_int(statistics.get("collect_count"))),
+36
View File
@@ -205,6 +205,12 @@ async def _collect_batch(
except douyin_api.DouyinApiError as exc: except douyin_api.DouyinApiError as exc:
errors.append(f"拉取作品 {external_id} 失败:{exc}") errors.append(f"拉取作品 {external_id} 失败:{exc}")
videos = [] videos = []
# 作品详情里带着作者的粉丝/总获赞。作品模式**不会**单独问博主资料 ——
# 不从这里收一份,这类任务(超级编导导入的都在这里)的博主粉丝数永远是空的。
for video in videos:
profile = _profile_from_author(video)
if profile is not None:
profiles.append(profile)
else: else:
videos = await _creator_works( videos = await _creator_works(
external_id, external_id,
@@ -250,6 +256,36 @@ async def _collect_batch(
} }
def _profile_from_author(video: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""把作品详情里那份 author 拼成一条账号快照;什么都不全就返回 None。
只在**有值**时收:author 对象偶尔是精简版(只有 uid/nickname),那种情况下写一条
全 NULL 的快照没有意义,还会让界面显示「采集时间有、数字没有」。
"""
creator_hash = str(video.get("creator_hash") or "")
if not creator_hash:
return None
stats = (
video.get("creator_fans"),
video.get("creator_total_favorited"),
video.get("creator_works"),
video.get("creator_following"),
)
if all(value is None for value in stats):
return None
return {
"creator_hash": creator_hash,
"creator_id": str(video.get("creator_id") or ""),
"nickname": str(video.get("nickname") or ""),
"fans": video.get("creator_fans"),
"total_favorited": video.get("creator_total_favorited"),
"works": video.get("creator_works"),
"following": video.get("creator_following"),
}
async def _creator_profile( async def _creator_profile(
sec_user_id: str, sec_user_id: str,
videos: Sequence[Dict[str, Any]], videos: Sequence[Dict[str, Any]],
+32
View File
@@ -126,6 +126,38 @@ class TestNormalizeAweme:
"https://www.douyin.com/user/" "https://www.douyin.com/user/"
) )
def test_the_authors_account_stats_ride_along(self):
"""作品详情里的 author 带着粉丝数 —— 作品模式靠它填博主的账号指标。"""
record = douyin_api.normalize_aweme(
{
"aweme_id": "1",
"author": {
"uid": "776719710825195",
"follower_count": 1444,
"total_favorited": 9751,
# 实测详情里 aweme_count 是 null
"aweme_count": None,
"following_count": 0,
},
}
)
assert record["creator_fans"] == 1444
assert record["creator_total_favorited"] == 9751
assert record["creator_works"] is None
assert record["creator_following"] == 0
def test_missing_author_counts_stay_none_not_zero(self):
"""0 会被界面显示成「粉丝 0」;事实是没采到,必须留 None。"""
record = douyin_api.normalize_aweme(
{"aweme_id": "1", "author": {"uid": "1", "nickname": "x"}}
)
assert record["creator_fans"] is None
assert record["creator_total_favorited"] is None
assert record["creator_works"] is None
assert record["creator_following"] is None
def test_hashtags_come_from_text_extra_not_the_truncated_desc(self): def test_hashtags_come_from_text_extra_not_the_truncated_desc(self):
"""`desc` 会在话题列表中途被截断,后半段话题只在 `text_extra` 里。 """`desc` 会在话题列表中途被截断,后半段话题只在 `text_extra` 里。
+57
View File
@@ -214,6 +214,63 @@ class TestNoteMode:
assert any("222" in error for error in result["errors"]) assert any("222" in error for error in result["errors"])
@pytest.mark.asyncio
async def test_creator_stats_come_from_the_detail_itself(self, monkeypatch, tmp_path):
"""作品模式不拉博主资料,粉丝数只能从详情返回的 author 里拿。
不带出来的话,超级编导导入的任务里每个博主都没有粉丝数 —— 而运营要看的就是
「这批号多大」。详情里作者那段是白拿的,不需要多发一次请求。
"""
async def fake_detail(aweme_id, *, cookie="", identity=None):
record = _video(aweme_id)
record.update(
{
"creator_id": "MS4wLjABAAAAsecuid",
"creator_fans": 1444,
"creator_total_favorited": 9751,
"creator_works": None,
"creator_following": 0,
}
)
return record
monkeypatch.setattr(douyin_api, "video_detail", fake_detail)
result = await _collect(
tmp_path, mode="note", want_comments=False, targets=[_Target("111")]
)
profiles = _read(
list((tmp_path / "douyin" / "jsonl").glob("*_profile_*.jsonl"))[0]
)
assert result["notes"] == 1
assert len(profiles) == 1
assert profiles[0]["fans"] == 1444
assert profiles[0]["total_favorited"] == 9751
assert profiles[0]["creator_id"] == "MS4wLjABAAAAsecuid"
# 详情里没有作品总数,留 None 而不是 0。
assert profiles[0]["works"] is None
@pytest.mark.asyncio
async def test_no_stats_means_no_profile_row(self, monkeypatch, tmp_path):
"""精简版 author(字段全缺)不该写一条全 NULL 的快照。"""
async def fake_detail(aweme_id, *, cookie="", identity=None):
return _video(aweme_id)
monkeypatch.setattr(douyin_api, "video_detail", fake_detail)
await _collect(
tmp_path, mode="note", want_comments=False, targets=[_Target("111")]
)
profiles = _read(
list((tmp_path / "douyin" / "jsonl").glob("*_profile_*.jsonl"))[0]
)
assert profiles == []
class TestDegradation: class TestDegradation:
"""作品列表被挡时的行为 —— 决定了这个功能今天有没有用。""" """作品列表被挡时的行为 —— 决定了这个功能今天有没有用。"""