Files
Agent-Reach/agent_reach/channels/youtube.py
T
Shawn 3a3d38acce fix: YouTube JS runtime check, Douyin health check, cli bare except, config permission race (#104)
- YouTube: warn when only Node.js is installed but yt-dlp config file
  is missing (previously returned "ok" incorrectly)
- Douyin: use `mcporter list` instead of calling with a hardcoded
  invalid share URL that always fails
- cli: replace bare `except:` with `except Exception:` in
  `_detect_environment` to avoid catching KeyboardInterrupt/SystemExit
- cli: fix unclosed file handle for cloud VM detection
- config: use `os.open()` with 0o600 mode to eliminate permission
  race window when saving credentials
2026-03-08 21:24:05 +08:00

45 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""YouTube — check if yt-dlp is available with JS runtime."""
import os
import shutil
from .base import Channel
class YouTubeChannel(Channel):
name = "youtube"
description = "YouTube 视频和字幕"
backends = ["yt-dlp"]
tier = 0
def can_handle(self, url: str) -> bool:
from urllib.parse import urlparse
d = urlparse(url).netloc.lower()
return "youtube.com" in d or "youtu.be" in d
def check(self, config=None):
if not shutil.which("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
# Deno works out of the box; Node.js requires explicit config
has_deno = shutil.which("deno")
if not has_deno:
ytdlp_config = os.path.expanduser("~/.config/yt-dlp/config")
has_js_config = False
if os.path.exists(ytdlp_config):
with open(ytdlp_config, "r") as f:
has_js_config = "--js-runtimes" in f.read()
if not has_js_config:
return "warn", (
"yt-dlp 已安装但未配置 JS runtime。运行:\n"
" mkdir -p ~/.config/yt-dlp && echo '--js-runtimes node' >> ~/.config/yt-dlp/config"
)
return "ok", "可提取视频信息和字幕"