"""Build unified YouTube channel card meta (OAuth / YPP / brand hint).""" from __future__ import annotations from typing import Any def brand_hint_for_handle(handle: str, title: str = "", note: str = "") -> str: from . import db h = str(handle or "").strip() if not h.startswith("@") and h and not h.startswith("UC"): h = f"@{h.lstrip('@')}" from_note = "" raw_note = str(note or "") for sep in ("Google品牌:", "Google品牌:", "brand:"): if sep in raw_note: from_note = raw_note.split(sep, 1)[1].strip().split("\n")[0].strip() break if from_note: return from_note try: import json settings = db.get_settings_map() raw = settings.get("shorts_oauth_brand_hints") or "{}" hints = json.loads(raw) if isinstance(raw, str) else (raw if isinstance(raw, dict) else {}) if h and hints.get(h): return str(hints[h]) t = str(title or "").strip().lower() for key, val in hints.items(): if isinstance(key, str) and key.startswith("@") and key.lower() in h.lower(): return str(val) title_map = hints.get("_by_title") or {} if isinstance(title_map, dict): for k, v in title_map.items(): if k.lower() in t: return str(v) except Exception: pass return "" def default_brand_hints() -> dict[str, Any]: return { "@ewfwefwefewf": "digitl nomad", "@nomadstreetfood": "hackrobot-jp", "@nomadrohub": "nomad Immigration", "@evanstreetfood": "Nhat Vy", "@nomadvloglife": "Lyca Hack", "@walkingtour-c8t": "walking tour", "@hackrobotlite": "hackrobot-jp", "_by_title": { "nomad vlog": "Lyca Hack", "nomad street food": "hackrobot-jp", "nomadro hub": "nomad Immigration", "异度世界": "digitl nomad", "nomdfsfbjksfbsjk": "digitl nomad", }, } def ensure_default_brand_hints() -> None: from . import db import json settings = db.get_settings_map() raw = settings.get("shorts_oauth_brand_hints") if raw: return db.update_settings_map({"shorts_oauth_brand_hints": json.dumps(default_brand_hints(), ensure_ascii=False)}) def oauth_meta_from_channel(ch: dict[str, Any] | None) -> dict[str, Any]: if not ch: return {} ok = ch.get("oauth_ok") err = str(ch.get("oauth_error") or "") has_rt = bool(str(ch.get("refresh_token") or "").strip()) profile = None try: from . import youtube_manage profile = youtube_manage._snippet_from_channel_row(ch) except Exception: profile = {} handle = str((profile or {}).get("custom_url") or ch.get("youtube_channel_id") or "") return { "authorized": has_rt, "oauth_ok": ok, "oauth_error": err, "oauth_profile_id": str(ch.get("oauth_profile_id") or "").strip().upper(), "brand_hint": brand_hint_for_handle( handle, str((profile or {}).get("title") or ch.get("name") or ""), str(ch.get("note") or ""), ), }