Merge pull request #48 from Panniantong/fix/yt-bilibili-cookie-docs

fix: YouTube 需要 JS runtime — install 自动配置 Node.js
This commit is contained in:
Pnant
2026-02-27 18:46:32 +08:00
committed by GitHub
3 changed files with 52 additions and 4 deletions
+21 -3
View File
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""YouTube — check if yt-dlp is available.""" """YouTube — check if yt-dlp is available with JS runtime."""
import os
import shutil import shutil
from .base import Channel from .base import Channel
@@ -17,6 +18,23 @@ class YouTubeChannel(Channel):
return "youtube.com" in d or "youtu.be" in d return "youtube.com" in d or "youtu.be" in d
def check(self, config=None): def check(self, config=None):
if shutil.which("yt-dlp"): if not shutil.which("yt-dlp"):
return "ok", "可提取视频信息和字幕"
return "off", "yt-dlp 未安装。安装:pip install yt-dlp" return "off", "yt-dlp 未安装。安装:pip install yt-dlp"
# Check JS runtime
has_js = shutil.which("deno") or shutil.which("node")
if not has_js:
return "warn", (
"yt-dlp 已安装但缺少 JS runtimeYouTube 必须)。\n"
" 安装 Node.js 或 deno,然后运行:agent-reach install"
)
# Check yt-dlp config for --js-runtimes
ytdlp_config = os.path.expanduser("~/.config/yt-dlp/config")
has_deno = shutil.which("deno")
if not has_deno and os.path.exists(ytdlp_config):
with open(ytdlp_config, "r") as f:
if "--js-runtimes" not in f.read():
return "warn", (
"yt-dlp 已安装但未配置 JS runtime。运行:\n"
" mkdir -p ~/.config/yt-dlp && echo '--js-runtimes node' >> ~/.config/yt-dlp/config"
)
return "ok", "可提取视频信息和字幕"
+19
View File
@@ -419,6 +419,25 @@ def _install_system_deps():
except Exception: except Exception:
print(" ⬜ undici install failed (optional — xreach may not work behind proxies)") print(" ⬜ undici install failed (optional — xreach may not work behind proxies)")
# ── yt-dlp JS runtime config (YouTube requires external JS runtime) ──
if shutil.which("node"):
ytdlp_config_dir = os.path.expanduser("~/.config/yt-dlp")
ytdlp_config = os.path.join(ytdlp_config_dir, "config")
needs_config = True
if os.path.exists(ytdlp_config):
with open(ytdlp_config, "r") as f:
if "--js-runtimes" in f.read():
needs_config = False
print(" ✅ yt-dlp JS runtime already configured")
if needs_config:
try:
os.makedirs(ytdlp_config_dir, exist_ok=True)
with open(ytdlp_config, "a") as f:
f.write("--js-runtimes node\n")
print(" ✅ yt-dlp configured to use Node.js as JS runtime (YouTube)")
except Exception:
print(" ⬜ Could not configure yt-dlp JS runtime (YouTube may not work)")
def _install_system_deps_safe(): def _install_system_deps_safe():
"""Safe mode: check what's installed, print instructions for what's missing.""" """Safe mode: check what's installed, print instructions for what's missing."""
+11
View File
@@ -93,6 +93,9 @@ xreach tweets @username --json -n 20
### YouTube (yt-dlp) ### YouTube (yt-dlp)
> ⚠️ yt-dlp 需要 JS runtime 才能下载 YouTube。`agent-reach install` 会自动配置 Node.js 作为 runtime。
> 如果遇到 "Sign in to confirm you're not a bot",是 IP 被 YouTube 反爬,换代理或加 cookies。
```bash ```bash
# Get video metadata # Get video metadata
yt-dlp --dump-json "https://www.youtube.com/watch?v=xxx" yt-dlp --dump-json "https://www.youtube.com/watch?v=xxx"
@@ -103,16 +106,24 @@ yt-dlp --write-sub --write-auto-sub --sub-lang "zh-Hans,zh,en" --skip-download -
# Search (yt-dlp ytsearch) # Search (yt-dlp ytsearch)
yt-dlp --dump-json "ytsearch5:query" yt-dlp --dump-json "ytsearch5:query"
# If "no JS runtime" warning: ensure Node.js is installed, then run:
# mkdir -p ~/.config/yt-dlp && echo "--js-runtimes node" >> ~/.config/yt-dlp/config
``` ```
### Bilibili (yt-dlp) ### Bilibili (yt-dlp)
> ⚠️ 服务器 IP 可能被 Bilibili 拦截(412 错误)。建议通过代理访问,或加 `--cookies-from-browser chrome`。
```bash ```bash
# Get video metadata # Get video metadata
yt-dlp --dump-json "https://www.bilibili.com/video/BVxxx" yt-dlp --dump-json "https://www.bilibili.com/video/BVxxx"
# Download subtitles # Download subtitles
yt-dlp --write-sub --write-auto-sub --sub-lang "zh-Hans,zh,en" --convert-subs vtt --skip-download -o "/tmp/%(id)s" "URL" yt-dlp --write-sub --write-auto-sub --sub-lang "zh-Hans,zh,en" --convert-subs vtt --skip-download -o "/tmp/%(id)s" "URL"
# If blocked (412 / login required):
yt-dlp --cookies-from-browser chrome --dump-json "URL"
``` ```
### Reddit (JSON API) ### Reddit (JSON API)