developer28 commited on
Commit
172f5c9
Β·
verified Β·
1 Parent(s): 08f8506

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
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
- # βœ… Extract video metadata only (no download)
24
 
25
  def extract_metadata(url, cookies_file=None):
26
- try:
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
- first_line = f.readline().strip()
39
- if "# Netscape HTTP Cookie File" in first_line:
40
  ydl_opts['cookiefile'] = cookies_file
41
- print("βœ… Valid cookies format detected")
42
  else:
43
  print("⚠️ Invalid cookies format. Skipping cookies.")
44
  else:
45
- print("⚠️ No cookies file provided or path invalid.")
46
-
47
- print("πŸ” yt-dlp options:", ydl_opts)
48
 
49
  with YoutubeDL(ydl_opts) as ydl:
50
- info = ydl.extract_info(url, download=False)
 
 
 
 
 
 
 
 
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