-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
129 lines (111 loc) · 4.31 KB
/
Copy pathsetup.py
File metadata and controls
129 lines (111 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""First-run setup — guide user through cookies configuration (Factor #11).
Usage:
python cli.py setup # Interactive setup
python cli.py setup --check # Check if cookies are valid
"""
import subprocess
import sys
from pathlib import Path
COOKIES_PATH = Path(__file__).parent / "yt_cookies.txt"
DIYHUB_COOKIES = Path.home() / "diyhub" / "yt_cookies.txt"
def cmd_setup(args):
"""First-run setup: configure YouTube cookies."""
if args.check:
return check_cookies()
print("=" * 60)
print(" AI Crawler v2 — 首次设置")
print("=" * 60)
print()
# Step 1: Check if cookies already exist
if check_cookies():
print("\nCookies 已配置且可用,无需重新设置。")
print(f"如需刷新,删除 {COOKIES_PATH} 后重新运行 setup。")
return
print("YouTube 字幕提取需要 cookies 认证(云服务器 IP 被 YouTube 封锁)。")
print()
print("请选择 cookies 来源:")
print()
print(" [1] 从本地浏览器导出(推荐)")
print(" 在你的电脑上运行:")
print(" yt-dlp --cookies-from-browser chrome --cookies yt_cookies.txt \\")
print(" 'https://www.youtube.com' --skip-download")
print(" 然后上传到服务器:")
print(f" scp cookies.txt <user>@<server>:{COOKIES_PATH}")
print()
print(" [2] 从 Firefox 导出")
print(" yt-dlp --cookies-from-browser firefox --cookies yt_cookies.txt \\")
print(" 'https://www.youtube.com' --skip-download")
print()
print(" [3] 手动提供 cookies 文件路径")
print()
choice = input("选择 (1/2/3) 或直接粘贴文件路径: ").strip()
if choice == "1" or choice == "2":
browser = "chrome" if choice == "1" else "firefox"
print(f"\n请在你的电脑上运行:")
print(f" yt-dlp --cookies-from-browser {browser} --cookies yt_cookies.txt \\")
print(f" 'https://www.youtube.com' --skip-download")
print(f"\n然后上传到服务器:")
print(f" scp cookies.txt <user>@<server>:{COOKIES_PATH}")
print(f"\n上传完成后,运行: python cli.py setup --check")
elif choice == "3":
path = input("输入 cookies 文件路径: ").strip()
src = Path(path).expanduser()
if not src.exists():
print(f"文件不存在: {src}")
sys.exit(1)
import shutil
shutil.copy2(src, COOKIES_PATH)
print(f"已复制到 {COOKIES_PATH}")
check_cookies()
else:
# Treat as file path
src = Path(choice).expanduser()
if src.exists():
import shutil
shutil.copy2(src, COOKIES_PATH)
print(f"已复制到 {COOKIES_PATH}")
check_cookies()
else:
print(f"无效选择或文件不存在: {choice}")
sys.exit(1)
def check_cookies() -> bool:
"""Check if cookies exist and are valid."""
# Check v2 local
candidates = [
COOKIES_PATH,
Path.home() / "yt_cookies.txt",
DIYHUB_COOKIES,
]
found = None
for c in candidates:
if c.exists() and c.stat().st_size > 200:
found = c
break
if not found:
print("❌ 未找到 YouTube cookies 文件")
print(f" 期望位置: {COOKIES_PATH}")
return False
# Quick validation: try yt-dlp
print(f"🔍 测试 cookies: {found}")
try:
result = subprocess.run(
["yt-dlp", "--cookies", str(found), "--skip-download",
"--print", "title", "https://www.youtube.com/watch?v=aircAruvnKk"],
capture_output=True, text=True, timeout=30,
)
if result.returncode == 0 and result.stdout.strip():
title = result.stdout.strip()
print(f"✅ Cookies 有效!测试视频: {title}")
# Copy to v2 location if not already there
if found != COOKIES_PATH:
import shutil
shutil.copy2(found, COOKIES_PATH)
print(f" 已复制到 {COOKIES_PATH}")
return True
else:
print(f"❌ Cookies 无效或已过期")
print(f" 错误: {result.stderr[:200]}")
return False
except Exception as e:
print(f"❌ 测试失败: {e}")
return False