Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# β
Gemini-Based Stock Recommendation Extractor (Debug-Enhanced)
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
|
@@ -20,34 +20,40 @@ def configure_gemini(api_key):
|
|
| 20 |
except Exception as e:
|
| 21 |
return f"β Gemini configuration failed: {str(e)}"
|
| 22 |
|
| 23 |
-
# β
|
| 24 |
|
| 25 |
def extract_metadata(url, cookies_file=None):
|
| 26 |
-
|
| 27 |
ydl_opts = {
|
| 28 |
'quiet': False,
|
| 29 |
'skip_download': True,
|
| 30 |
'noplaylist': True,
|
| 31 |
-
'extract_flat': False
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
-
|
| 34 |
-
if cookies_file and os.path.exists(cookies_file):
|
| 35 |
-
print(f"π Using cookies file: {cookies_file}")
|
| 36 |
-
# Validate Netscape format header
|
| 37 |
with open(cookies_file, "r", encoding="utf-8", errors="ignore") as f:
|
| 38 |
-
|
| 39 |
-
if "# Netscape HTTP Cookie File" in
|
| 40 |
ydl_opts['cookiefile'] = cookies_file
|
| 41 |
-
print("β
|
| 42 |
else:
|
| 43 |
print("β οΈ Invalid cookies format. Skipping cookies.")
|
| 44 |
else:
|
| 45 |
-
print("
|
| 46 |
-
|
| 47 |
-
print("π yt-dlp options:", ydl_opts)
|
| 48 |
|
| 49 |
with YoutubeDL(ydl_opts) as ydl:
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
print("β
Metadata fetched successfully")
|
| 53 |
|
|
|
|
| 1 |
+
# β
Gemini-Based Stock Recommendation Extractor (Debug-Enhanced with Fallback)
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
return f"β Gemini configuration failed: {str(e)}"
|
| 22 |
|
| 23 |
+
# β
Robust metadata extraction with fallback
|
| 24 |
|
| 25 |
def extract_metadata(url, cookies_file=None):
|
| 26 |
+
def run_yt_dlp(with_cookies):
|
| 27 |
ydl_opts = {
|
| 28 |
'quiet': False,
|
| 29 |
'skip_download': True,
|
| 30 |
'noplaylist': True,
|
| 31 |
+
'extract_flat': False,
|
| 32 |
+
'force_ipv4': True,
|
| 33 |
+
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
|
| 34 |
+
'referer': 'https://www.youtube.com/'
|
| 35 |
}
|
| 36 |
+
if with_cookies and cookies_file and os.path.exists(cookies_file):
|
|
|
|
|
|
|
|
|
|
| 37 |
with open(cookies_file, "r", encoding="utf-8", errors="ignore") as f:
|
| 38 |
+
header = f.readline().strip()
|
| 39 |
+
if "# Netscape HTTP Cookie File" in header:
|
| 40 |
ydl_opts['cookiefile'] = cookies_file
|
| 41 |
+
print("β
Using valid cookies file")
|
| 42 |
else:
|
| 43 |
print("β οΈ Invalid cookies format. Skipping cookies.")
|
| 44 |
else:
|
| 45 |
+
print("π Proceeding without cookies")
|
|
|
|
|
|
|
| 46 |
|
| 47 |
with YoutubeDL(ydl_opts) as ydl:
|
| 48 |
+
return ydl.extract_info(url, download=False)
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
# Try with cookies first
|
| 52 |
+
try:
|
| 53 |
+
info = run_yt_dlp(with_cookies=True)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print("β οΈ First attempt with cookies failed, retrying without cookies...", e)
|
| 56 |
+
info = run_yt_dlp(with_cookies=False)
|
| 57 |
|
| 58 |
print("β
Metadata fetched successfully")
|
| 59 |
|