移植自 mac-agent-os 的 mediacrawler_adapter:不起子进程、不开页面,用浏览器里那份
登录态直接调抖音 Web 接口。产物键名照抄 store/douyin,所以 ingest 那条链路一个字不用改。
**目前能用的(真环境实测,非推断)**:
profile/other : 200, 7075 字节 —— 博主主页指标(粉丝/获赞/作品数/昵称)
aweme/detail : 200, 45425 字节 —— 单条作品详情(含点赞/评论/收藏/分享)
**目前不能用的:作品列表 `aweme/post`。** 两个互相独立的原因:
1. 这个接口被抖音单独升级成了真校验:不带 x-tt-argus 回 403「Uifid Not Found」,
带上 dummy 值回 200 + **空 body**。也就是说「头在不在」骗得过,「真校验」过不了。
同一套头打 profile/other 和 aweme/detail 都是通的 —— 抖音是挑着接口加保护的,
挑中的恰好是「批量拉作品列表」这个最敏感的动作。
2. 改走页面截获也不行:CDP 浏览器打开博主主页会落到「验证码中间页」(当天大量探测的
代价,过几小时要重测)。
所以现在的边界是:**已知作品的指标刷新能做,自动发现新作品做不了**。
**排查中控住变量后得到的两条事实**(都写进注释了):
· `Accept` / `Accept-Language` / `Referer` 才是主页接口能返回真数据的原因 —— 只有
UA+client hints+Cookie 时是 200 但仅 121 字节的空壳,补上这三个头变 7074 字节。
(我先前猜的 sec-ch-ua 不是关键。)
· 因此 UA 与 client hints 必须**成套地取自同一个浏览器**,所以 BrowserIdentity 一次
从 CDP 取齐 cookie + UA + hints,而不是各自写死。
「200 + 空 body 必须当场报错」也是刻意写死的:放过去它会在下游变成「这个博主没作品」,
把一次失败伪装成一条正常结果 —— 爬虫那条路正是这么栽的,还被翻译成「账号被封」。
测试 +11:cookie 解析、请求头成套性(含 uifid 缺失/回退)、产物键名与 store 对齐、
以及 _get 的三条失败路径(空 body / 403 带网关原话 / 正常返回)。
142 lines
5.2 KiB
Python
142 lines
5.2 KiB
Python
"""
|
||
directors.llm — 导演层共用的轻量 LLM 客户端
|
||
=============================================
|
||
|
||
配置优先级:环境变量 DEEPSEEK_* > agent-local local.yaml(llm) > 09_ave/config.yaml(llm)
|
||
(与评论生成 AIGenerator 一致的取配置方式,但独立实现,避免跨模块耦合)
|
||
|
||
仅提供最简 chat 调用 + JSON 解析,供各导演实现使用。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import logging
|
||
import os
|
||
import re
|
||
import urllib.error
|
||
import urllib.request
|
||
from pathlib import Path
|
||
from typing import Any
|
||
|
||
import yaml
|
||
from lib.http import urlopen as _noproxy_urlopen # 禁代理+重试
|
||
|
||
log = logging.getLogger("directors.llm")
|
||
|
||
_SCRIPTS = Path(__file__).resolve().parent.parent # 09_ave/scripts/
|
||
_AVE_DIR = _SCRIPTS.parent # 09_ave/
|
||
_AGENT_LOCAL = Path(os.environ.get("AGENT_LOCAL") or (_AVE_DIR.parents[2] / "agent-local"))
|
||
_LOCAL_CFG = _AGENT_LOCAL / "tools" / "ave" / "config" / "local.yaml"
|
||
_AVE_CFG = _AVE_DIR / "config.yaml"
|
||
|
||
|
||
def load_llm_config() -> dict:
|
||
"""三级优先级读取 LLM 配置"""
|
||
cfg: dict = {}
|
||
# ③ 09_ave/config.yaml
|
||
if _AVE_CFG.exists():
|
||
try:
|
||
raw = yaml.safe_load(_AVE_CFG.read_text(encoding="utf-8")) or {}
|
||
cfg.update(raw.get("llm") or {})
|
||
except Exception:
|
||
pass
|
||
# ② 配置中心 local.yaml → llm
|
||
if _LOCAL_CFG.exists():
|
||
try:
|
||
raw = yaml.safe_load(_LOCAL_CFG.read_text(encoding="utf-8")) or {}
|
||
cfg.update(raw.get("llm") or {})
|
||
except Exception:
|
||
pass
|
||
# ① 环境变量
|
||
if os.environ.get("DEEPSEEK_API_KEY"):
|
||
cfg["api_key"] = os.environ["DEEPSEEK_API_KEY"]
|
||
if os.environ.get("DEEPSEEK_BASE_URL"):
|
||
cfg["base_url"] = os.environ["DEEPSEEK_BASE_URL"]
|
||
if os.environ.get("DEEPSEEK_MODEL"):
|
||
cfg["model"] = os.environ["DEEPSEEK_MODEL"]
|
||
cfg.setdefault("base_url", "https://api.deepseek.com/v1")
|
||
cfg.setdefault("model", "deepseek-v4-flash")
|
||
return cfg
|
||
|
||
|
||
class DirectorLLM:
|
||
"""导演层 LLM 客户端(单次同步调用)"""
|
||
|
||
def __init__(self, timeout: int = 90):
|
||
cfg = load_llm_config()
|
||
self.api_key = (cfg.get("api_key") or "").strip()
|
||
self.base_url = (cfg.get("base_url") or "https://api.deepseek.com/v1").rstrip("/")
|
||
self.model = cfg.get("model") or "deepseek-v4-flash"
|
||
self.timeout = timeout
|
||
self._fails = 0
|
||
|
||
@property
|
||
def available(self) -> bool:
|
||
return bool(self.api_key) and self._fails < 3
|
||
|
||
def chat(self, prompt: str, system: str = "", temperature: float = 0.7,
|
||
max_tokens: int = 3000) -> str:
|
||
"""单次对话(失败返回空串,连续 3 次失败熔断)"""
|
||
if not self.available:
|
||
log.warning(" ⚠️ LLM 不可用(未配 key 或已熔断)")
|
||
return ""
|
||
messages = []
|
||
if system:
|
||
messages.append({"role": "system", "content": system})
|
||
messages.append({"role": "user", "content": prompt})
|
||
body = json.dumps({
|
||
"model": self.model,
|
||
"messages": messages,
|
||
"temperature": temperature,
|
||
"max_tokens": max_tokens,
|
||
# 关思考模式:分镜生成不需要长推理,省 token 且更快
|
||
"thinking": {"type": "disabled"},
|
||
}).encode("utf-8")
|
||
req = urllib.request.Request(
|
||
f"{self.base_url}/chat/completions", data=body,
|
||
headers={"Content-Type": "application/json",
|
||
"Authorization": f"Bearer {self.api_key}"})
|
||
try:
|
||
with _noproxy_urlopen(req, timeout=self.timeout) as r:
|
||
data = json.loads(r.read().decode("utf-8"))
|
||
content = (data.get("choices") or [{}])[0].get("message", {}).get("content", "")
|
||
self._fails = 0
|
||
return (content or "").strip()
|
||
except urllib.error.HTTPError as e:
|
||
detail = e.read().decode("utf-8", errors="ignore")[:200]
|
||
log.error(" ❌ LLM HTTP %s: %s", e.code, detail)
|
||
self._fails += 1
|
||
return ""
|
||
except Exception as e:
|
||
log.error(" ❌ LLM 调用失败: %s", e)
|
||
self._fails += 1
|
||
return ""
|
||
|
||
# ── JSON 解析(容错)──────────────────────────────
|
||
|
||
@staticmethod
|
||
def parse_json(text: str) -> Any:
|
||
"""从 LLM 输出里提取 JSON(容忍 markdown 围栏 / 前后废话)"""
|
||
if not text:
|
||
return None
|
||
t = text.strip()
|
||
# 去 markdown 围栏
|
||
m = re.search(r"```(?:json)?\s*(.+?)\s*```", t, re.S)
|
||
if m:
|
||
t = m.group(1).strip()
|
||
# 直接解析
|
||
try:
|
||
return json.loads(t)
|
||
except Exception:
|
||
pass
|
||
# 找第一个 { 或 [ 到最后一个 } 或 ]
|
||
for open_c, close_c in (("{", "}"), ("[", "]")):
|
||
i, j = t.find(open_c), t.rfind(close_c)
|
||
if i >= 0 and j > i:
|
||
try:
|
||
return json.loads(t[i:j + 1])
|
||
except Exception:
|
||
continue
|
||
log.warning(" ⚠️ LLM 输出无法解析为 JSON(前 120 字: %s)", t[:120])
|
||
return None
|