feat(tiktok): author normalizer

This commit is contained in:
CREDO23 2026-07-08 15:40:38 +02:00
parent 8840fd2c53
commit 0bbcf99852
2 changed files with 33 additions and 1 deletions

View file

@ -2,6 +2,7 @@
from __future__ import annotations
from .author import parse_author
from .hydration import extract_rehydration_data
__all__ = ["extract_rehydration_data"]
__all__ = ["extract_rehydration_data", "parse_author"]

View file

@ -0,0 +1,31 @@
"""Normalize TikTok author/profile payloads into an ``authorMeta`` dict."""
from __future__ import annotations
from typing import Any
_PROFILE_URL = "https://www.tiktok.com/@{username}"
def build_author_meta(author: dict[str, Any], stats: dict[str, Any]) -> dict[str, Any]:
"""Map an author object + its stats to the ``authorMeta`` output shape."""
username = author.get("uniqueId")
return {
"id": author.get("id"),
"name": username,
"nickName": author.get("nickname"),
"profileUrl": _PROFILE_URL.format(username=username) if username else None,
"verified": author.get("verified"),
"signature": author.get("signature"),
"avatar": author.get("avatarLarger") or author.get("avatarMedium"),
"privateAccount": author.get("privateAccount"),
"fans": stats.get("followerCount"),
"following": stats.get("followingCount"),
"heart": stats.get("heartCount"),
"video": stats.get("videoCount"),
}
def parse_author(user_info: dict[str, Any]) -> dict[str, Any]:
"""Map a ``webapp.user-detail`` ``userInfo`` (``{user, stats}``) to authorMeta."""
return build_author_meta(user_info.get("user") or {}, user_info.get("stats") or {})