-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_youtube_config.py
More file actions
182 lines (145 loc) · 5.55 KB
/
Copy pathtest_youtube_config.py
File metadata and controls
182 lines (145 loc) · 5.55 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
"""
Test script for YouTube processor configuration and authentication.
"""
import os
import sys
from pathlib import Path
import tempfile
# Add src to path for imports
sys.path.insert(0, str(Path(__file__).parent / "src"))
from config import Config
from file_processors.youtube_processor import YouTubeProcessor
def test_config_loading():
"""Test that YouTube configuration is loaded properly."""
print("Testing configuration loading...")
# Test that config attributes exist
assert hasattr(Config, 'YOUTUBE_COOKIES_FILE')
assert hasattr(Config, 'YOUTUBE_COOKIES_BROWSER')
print("✅ Configuration attributes exist")
def test_processor_initialization():
"""Test YouTube processor can initialize."""
print("Testing processor initialization...")
try:
processor = YouTubeProcessor()
print("✅ YouTube processor initialized successfully")
return processor
except ValueError as e:
if "Google API key not found" in str(e):
print("❌ Google API key not configured (expected for testing)")
return None
else:
raise
def test_url_detection():
"""Test YouTube URL detection."""
print("Testing URL detection...")
# Test valid YouTube URLs
valid_urls = [
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"https://youtu.be/dQw4w9WgXcQ",
"https://m.youtube.com/watch?v=dQw4w9WgXcQ",
"https://youtube.com/embed/dQw4w9WgXcQ",
]
# Test invalid URLs
invalid_urls = [
"https://example.com/video.mp4",
"not_a_url",
"https://vimeo.com/123456",
]
for url in valid_urls:
assert YouTubeProcessor.is_youtube_url(url), f"Should detect: {url}"
for url in invalid_urls:
assert not YouTubeProcessor.is_youtube_url(url), f"Should not detect: {url}"
print("✅ URL detection working correctly")
def test_ydl_opts_generation():
"""Test yt-dlp options generation with different configurations."""
print("Testing yt-dlp options generation...")
# Mock different configuration scenarios
original_cookies_file = Config.YOUTUBE_COOKIES_FILE
original_cookies_browser = Config.YOUTUBE_COOKIES_BROWSER
try:
# Test with no cookies
Config.YOUTUBE_COOKIES_FILE = None
Config.YOUTUBE_COOKIES_BROWSER = None
# Create a processor instance (skip if no Google API key)
try:
processor = YouTubeProcessor()
except ValueError:
print("⚠️ Skipping yt-dlp options test (no Google API key)")
return
opts = processor._get_ydl_opts(download=False)
assert 'cookiefile' not in opts
assert 'cookiesfrombrowser' not in opts
print("✅ No cookies configuration works")
# Test with cookie file
Config.YOUTUBE_COOKIES_FILE = "/path/to/cookies.txt"
Config.YOUTUBE_COOKIES_BROWSER = None
opts = processor._get_ydl_opts(download=False)
assert opts.get('cookiefile') == "/path/to/cookies.txt"
print("✅ Cookie file configuration works")
# Test with browser cookies
Config.YOUTUBE_COOKIES_FILE = None
Config.YOUTUBE_COOKIES_BROWSER = "chrome"
opts = processor._get_ydl_opts(download=False)
assert opts.get('cookiesfrombrowser') == ("chrome",)
print("✅ Browser cookies configuration works")
# Test download options
opts = processor._get_ydl_opts(download=True)
assert 'format' in opts
assert 'postprocessors' in opts
assert opts['format'] == 'bestaudio/best'
print("✅ Download options configuration works")
finally:
# Restore original configuration
Config.YOUTUBE_COOKIES_FILE = original_cookies_file
Config.YOUTUBE_COOKIES_BROWSER = original_cookies_browser
def test_current_env_setup():
"""Test current environment setup."""
print("Testing current environment setup...")
# Load environment variables
from dotenv import load_dotenv
load_dotenv()
# Check API keys
openai_key = Config.OPENAI_API_KEY
google_key = Config.GOOGLE_API_KEY
print(f"OpenAI API Key: {'✅ Set' if openai_key else '❌ Missing'}")
print(f"Google API Key: {'✅ Set' if google_key else '❌ Missing'}")
# Check YouTube authentication
cookies_file = Config.YOUTUBE_COOKIES_FILE
cookies_browser = Config.YOUTUBE_COOKIES_BROWSER
if cookies_file:
file_exists = Path(cookies_file).exists() if cookies_file else False
print(f"YouTube Cookie File: {cookies_file} ({'✅ Exists' if file_exists else '❌ Not found'})")
elif cookies_browser:
print(f"YouTube Browser Cookies: {cookies_browser} ✅ Configured")
else:
print("YouTube Authentication: ❌ Not configured")
print("Run 'python setup_youtube_auth.py' to set up authentication")
def main():
"""Run all tests."""
print("=" * 60)
print("YouTube Processor Configuration Test")
print("=" * 60)
print()
try:
test_config_loading()
print()
test_url_detection()
print()
test_ydl_opts_generation()
print()
test_current_env_setup()
print()
print("=" * 60)
print("✅ All tests completed!")
print()
print("If YouTube authentication is not configured, run:")
print("python setup_youtube_auth.py")
print("=" * 60)
except Exception as e:
print(f"❌ Test failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()