diff --git a/apps/api/app/api/routes/scripts_ai.py b/apps/api/app/api/routes/scripts_ai.py index fc48dc10d..a1daf8179 100644 --- a/apps/api/app/api/routes/scripts_ai.py +++ b/apps/api/app/api/routes/scripts_ai.py @@ -101,7 +101,11 @@ else: DOUYIN_COOKIES_FILE_BAKED, ) -_DOUYIN_DEBUG_ERRORS = os.environ.get("DOUYIN_DEBUG_ERRORS", "").lower() in ("1", "true", "yes") +_DOUYIN_DEBUG_ERRORS = os.environ.get("DOUYIN_DEBUG_ERRORS", "").lower() in ( + "1", + "true", + "yes", +) or os.environ.get("APP_ENV", "").lower() in ("staging", "dev", "development", "test") _URL_EXTRACT_RE = re.compile(r"https?://\S+", re.IGNORECASE) _DOUYIN_HOST_RE = re.compile( @@ -307,7 +311,7 @@ def _fetch_ttwid(): # 方式一:直接调 ttwid 注册接口 try: with httpx.Client(timeout=8, follow_redirects=True, verify=False) as http: - http.post( + r = http.post( "https://ttwid.bytedance.com/ttwid/union/register/", json={ "region": "cn", @@ -442,6 +446,7 @@ def _ytdlp_extract_video_url(page_url, cookiefile=None, max_retries=10): import time as _time ttwid = _fetch_ttwid() + logger.info("ytdlp 直链提取开始: page_url=%s ttwid_ok=%s max_retries=%d", page_url, bool(ttwid), max_retries) with tempfile.TemporaryDirectory(prefix="dy_ytdlp_cookies_") as tmpdir: for attempt in range(max_retries): if attempt % 3 == 0: @@ -450,8 +455,9 @@ def _ytdlp_extract_video_url(page_url, cookiefile=None, max_retries=10): info, err = _ytdlp_try_extract(page_url, gen_cookie) if info: vurl = info.get("url") - logger.info("yt-dlp 合成 cookie 第%d次尝试成功", attempt + 1) + logger.info("yt-dlp 合成 cookie 第%d次尝试成功: vurl_domain=%s", attempt + 1, vurl.split("/")[2] if vurl and "/" in vurl else "?") return (vurl, float(info.get("duration") or 0.0)) if vurl else (None, 0.0) + logger.info("yt-dlp 合成 cookie 第%d次尝试失败: %s", attempt + 1, str(err)[:150]) _time.sleep(0.4) logger.info("yt-dlp 合成 cookie 全部 %d 次重试均失败", max_retries) @@ -717,7 +723,7 @@ def _ytdlp_download_and_local_asr(page_url, temp_dir, cookiefile=None): cookie_files_to_try.append(cookiefile) # 2. 合成 cookie 重试(复用 _ytdlp_extract 的 cookie 生成逻辑,8次重试) - import time as _time + import secrets, time as _time ttwid = _fetch_ttwid() for i in range(8): if i % 3 == 0: @@ -731,11 +737,21 @@ def _ytdlp_download_and_local_asr(page_url, temp_dir, cookiefile=None): vpath, duration = _download(cfile) if vpath and os.path.isfile(vpath) and os.path.getsize(vpath) > 0: break + logger.warning( + "ytdlp 下载返回路径无效或空文件: vpath=%s size=%s", + vpath, + os.path.getsize(vpath) if vpath and os.path.isfile(vpath) else "N/A", + ) except Exception as exc: # noqa: BLE001 last_err = exc + logger.warning("ytdlp 下载重试失败: %s", str(exc)[:200]) continue else: msg = str(last_err) if last_err else "unknown" + logger.warning( + "ytdlp 下载全部 %d 次 cookie 尝试均失败: ttwid_ok=%s last_err=%s", + len(cookie_files_to_try), bool(ttwid), msg[:300], + ) if _is_cookies_related_error(msg) or "Fresh cookies" in msg: _detail = "抖音链接解析暂时不可用,请稍后重试或手动输入文案" if _DOUYIN_DEBUG_ERRORS: