返回 douyin-downloader
metadata.py
根目录 / core / metadata.py
1 """Shared helpers for extracting normalized fields from raw Douyin aweme payloads.
2
3 These helpers centralize the payload-shape dereferencing so that callers across
4 downloaders (``downloader_base``, ``music_downloader``, future strategies, …)
5 all agree on how to pull fields like ``author.sec_uid`` out of the various
6 aweme dict shapes returned by the upstream API.
7 """
8
9 from __future__ import annotations
10
11 from typing import Any, Mapping, Optional
12
13
14 def extract_author_sec_uid(aweme: Optional[Mapping[str, Any]]) -> Optional[str]:
15 """Return ``aweme["author"]["sec_uid"]`` or ``None`` if unavailable.
16
17 Defensive against every shape variation observed so far:
18 * ``aweme`` itself being ``None`` or not a mapping
19 * ``aweme["author"]`` being missing, ``None``, or not a mapping
20 * ``sec_uid`` being missing, ``None``, or an empty / whitespace string
21 (all collapse to ``None`` so downstream consumers can treat NULL and
22 empty-string identically).
23 """
24
25 if not isinstance(aweme, Mapping):
26 return None
27 author = aweme.get("author")
28 if not isinstance(author, Mapping):
29 return None
30 sec_uid = author.get("sec_uid")
31 if not isinstance(sec_uid, str):
32 return None
33 sec_uid = sec_uid.strip()
34 return sec_uid or None
35
35 lines PYTHON