prasanth.thangavel commited on
Commit
edc98e3
·
1 Parent(s): 0313094

Add logger and env flag to enable/disable cookies

Browse files
Files changed (1) hide show
  1. app.py +28 -2
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  from yt_dlp import YoutubeDL
3
  import os
@@ -7,6 +8,19 @@ import tempfile
7
  import shutil
8
  from pathlib import Path
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def cookies_to_env(cookie_file_path: str) -> str:
11
  """Convert cookie file content to environment variable format"""
12
  try:
@@ -93,6 +107,7 @@ def create_temp_cookie_file():
93
  def download_for_browser(url, mode='audio', quality='high'):
94
  if not url:
95
  return None, "Please enter a valid URL"
 
96
 
97
  # Create temporary directory that persists until file is served
98
  temp_dir = Path(tempfile.mkdtemp())
@@ -135,9 +150,14 @@ def download_for_browser(url, mode='audio', quality='high'):
135
  })
136
 
137
  # Add cookies
138
- opts["cookiefile"] = "firefox-cookies.txt" #create_temp_cookie_file()
 
 
 
 
139
 
140
  # Download file
 
141
  with YoutubeDL(opts) as ydl:
142
  info = ydl.extract_info(url, download=True)
143
 
@@ -156,7 +176,13 @@ def download_for_browser(url, mode='audio', quality='high'):
156
  except Exception as e:
157
  if temp_dir.exists():
158
  shutil.rmtree(temp_dir)
159
- return None, f"Error during download: {str(e)}"
 
 
 
 
 
 
160
 
161
  def create_browser_ui():
162
 
 
1
+ import logging
2
  import gradio as gr
3
  from yt_dlp import YoutubeDL
4
  import os
 
8
  import shutil
9
  from pathlib import Path
10
 
11
+ # Configure logging
12
+ logging.basicConfig(
13
+ level=logging.INFO,
14
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
15
+ handlers=[
16
+ logging.FileHandler('youtube_downloader.log'),
17
+ logging.StreamHandler()
18
+ ]
19
+ )
20
+
21
+ # Create logger instance
22
+ logger = logging.getLogger('youtube_downloader')
23
+
24
  def cookies_to_env(cookie_file_path: str) -> str:
25
  """Convert cookie file content to environment variable format"""
26
  try:
 
107
  def download_for_browser(url, mode='audio', quality='high'):
108
  if not url:
109
  return None, "Please enter a valid URL"
110
+ logger.info(f"Downloading {url} in {mode} mode with {quality} quality")
111
 
112
  # Create temporary directory that persists until file is served
113
  temp_dir = Path(tempfile.mkdtemp())
 
150
  })
151
 
152
  # Add cookies
153
+ load_dotenv()
154
+ USE_FIREFOX_COOKIES = os.getenv("USE_FIREFOX_COOKIES", "False")
155
+ logger.info(f"Using Firefox cookies: {USE_FIREFOX_COOKIES}")
156
+ if USE_FIREFOX_COOKIES == "True":
157
+ opts["cookiefile"] = "firefox-cookies.txt" #create_temp_cookie_file()
158
 
159
  # Download file
160
+ logger.info(f"Downloading {url} with options: {opts}")
161
  with YoutubeDL(opts) as ydl:
162
  info = ydl.extract_info(url, download=True)
163
 
 
176
  except Exception as e:
177
  if temp_dir.exists():
178
  shutil.rmtree(temp_dir)
179
+ # Return raw error message without formatting
180
+ error_msg = str(e)
181
+ if "ERROR: [youtube]" in error_msg:
182
+ # Extract everything after "ERROR: [youtube]"
183
+ error_msg = error_msg.split("ERROR: [youtube]")[1].strip()
184
+ logger.error(f"Download error: {error_msg}")
185
+ return None, error_msg
186
 
187
  def create_browser_ui():
188