@@ -178,10 +178,65 @@ def split_message(message, max_length=3500):
178178async def download_audio (url , audio_path ):
179179 config = ConfigLoader .get_config ()
180180 ytdlp_settings = ConfigLoader .get_ytdlp_domain_settings ()
181- use_cookies = config .getboolean ('YTDLPSettings' , 'use_cookies' , fallback = False )
181+
182+ # --- Added verbose logging for cookie usage ---
183+ use_cookies_file = config .getboolean ('YTDLPSettings' , 'use_cookies_file' , fallback = False )
182184 cookies_file = config .get ('YTDLPSettings' , 'cookies_file' , fallback = 'config/cookies.txt' )
185+ if use_cookies_file :
186+ logger .info ("Cookie usage is enabled (use_cookies_file=True)." )
187+ logger .info (f"Expected cookies file path: { cookies_file } " )
188+ if os .path .exists (cookies_file ):
189+ logger .info ("Cookie file found and will be used for yt-dlp." )
190+ else :
191+ logger .warning (
192+ "Cookie file usage is enabled, but the specified cookies file "
193+ f"does not exist at: { cookies_file } . Please check config.ini."
194+ )
195+ else :
196+ logger .info ("Cookie file usage is disabled from config (use_cookies_file=False)." )
197+
198+ # read config options
199+ use_cookies_file = config .getboolean ('YTDLPSettings' , 'use_cookies_file' , fallback = False )
200+ cookies_file = config .get ('YTDLPSettings' , 'cookies_file' , fallback = 'config/cookies.txt' )
201+
202+ use_browser_cookies = config .getboolean ('YTDLPSettings' , 'use_browser_cookies' , fallback = False )
203+ browser_type = config .get ('YTDLPSettings' , 'browser_type' , fallback = 'firefox' )
204+ browser_cookies_profile = config .get ('YTDLPSettings' , 'browser_cookies_profile' , fallback = '' )
205+
206+ no_cache_dir = config .getboolean ('YTDLPSettings' , 'no_cache_dir' , fallback = False )
207+ custom_cache_dir = config .get ('YTDLPSettings' , 'custom_cache_dir' , fallback = '' )
183208 use_worst_video_quality = config .getboolean ('YTDLPSettings' , 'use_worst_video_quality' , fallback = True )
184-
209+
210+ # Log what was found
211+ logger .info (f"use_cookies_file={ use_cookies_file } , cookies_file={ cookies_file } " )
212+ logger .info (f"use_browser_cookies={ use_browser_cookies } , browser_type={ browser_type } , browser_cookies_profile={ browser_cookies_profile } " )
213+ logger .info (f"no_cache_dir={ no_cache_dir } , custom_cache_dir={ custom_cache_dir } , use_worst_video_quality={ use_worst_video_quality } " )
214+
215+ # -- Mutually exclusive check (optional) --
216+ if use_cookies_file and use_browser_cookies :
217+ # Decide which to prefer, or raise an error
218+ logger .warning ("Both 'use_cookies_file' and 'use_browser_cookies' are true! Defaulting to 'use_browser_cookies' and ignoring the cookies file." )
219+ # Or: raise Exception("Cannot use both cookie-file and browser-cookies at once.")
220+
221+ # Expand environment variable if browser_cookies_profile starts with '$'
222+ actual_browser_profile = browser_cookies_profile
223+ if use_browser_cookies and browser_cookies_profile .startswith ('$' ):
224+ env_var_name = browser_cookies_profile [1 :]
225+ env_val = os .getenv (env_var_name , '' )
226+ if env_val :
227+ logger .info (f"Resolved browser profile from env var { env_var_name } : { env_val } " )
228+ actual_browser_profile = env_val
229+ else :
230+ logger .warning (f"Environment variable { env_var_name } not set or empty; browser cookies may fail." )
231+ # Optionally raise or fallback
232+
233+ # Check for existence of cookies_file if we’re using a file
234+ if use_cookies_file :
235+ if os .path .exists (cookies_file ):
236+ logger .info (f"Cookies file found: { cookies_file } " )
237+ else :
238+ logger .warning (f"Cookies file not found: { cookies_file } " )
239+
185240 parsed_url = urlparse (url )
186241 domain = parsed_url .netloc .lower ()
187242 if domain .startswith ('www.' ):
@@ -198,8 +253,26 @@ async def download_audio(url, audio_path):
198253 "--dump-json" ,
199254 url
200255 ]
201- if use_cookies and os .path .exists (cookies_file ):
202- command .extend (["--cookies" , cookies_file ])
256+
257+ # Apply cache settings based on config
258+ if no_cache_dir :
259+ logger .info ("Disabling yt-dlp cache via config.ini (no_cache_dir=true)." )
260+ command .insert (1 , "--no-cache-dir" ) # Insert after 'yt-dlp'
261+ elif custom_cache_dir :
262+ logger .info (f"Using custom yt-dlp cache directory: { custom_cache_dir } " )
263+ command .insert (1 , f"--cache-dir={ custom_cache_dir } " )
264+
265+ # if use_cookies_file and os.path.exists(cookies_file):
266+ # command.extend(["--cookies", cookies_file])
267+
268+ # Add cookies (only if we do NOT also prefer the browser cookie approach)
269+ if use_browser_cookies and not use_cookies_file :
270+ # Add --cookies-from-browser
271+ command .extend (["--cookies-from-browser" , f"{ browser_type } :{ actual_browser_profile } " ])
272+ elif use_cookies_file :
273+ # Add --cookies <file>
274+ if os .path .exists (cookies_file ):
275+ command .extend (["--cookies" , cookies_file ])
203276
204277 process = await asyncio .create_subprocess_exec (
205278 * command ,
@@ -257,6 +330,7 @@ def get_format_sort_key(fmt):
257330 if not selected_format_id :
258331 raise Exception ("Could not determine selected format ID." )
259332
333+ logger .info (f"yt-dlp command: { command } " )
260334 logger .info (f"Selected format ID: { selected_format_id } " )
261335
262336 # Step 3: Download video using the selected format
@@ -270,9 +344,26 @@ def get_format_sort_key(fmt):
270344 "--output" , video_output_template ,
271345 url
272346 ]
273- if use_cookies and os .path .exists (cookies_file ):
274- command .extend (["--cookies" , cookies_file ])
275347
348+ # if use_cookies_file and os.path.exists(cookies_file):
349+ # command.extend(["--cookies", cookies_file])
350+
351+ # apply the cache logic
352+ if no_cache_dir :
353+ logger .info ("Disabling yt-dlp cache via config.ini (no_cache_dir=true)." )
354+ command .insert (1 , "--no-cache-dir" )
355+ elif custom_cache_dir :
356+ logger .info (f"Using custom yt-dlp cache directory: { custom_cache_dir } " )
357+ command .insert (1 , f"--cache-dir={ custom_cache_dir } " )
358+
359+ # cookies again
360+ if use_browser_cookies and not use_cookies_file :
361+ command .extend (["--cookies-from-browser" , f"{ browser_type } :{ actual_browser_profile } " ])
362+ elif use_cookies_file :
363+ if os .path .exists (cookies_file ):
364+ command .extend (["--cookies" , cookies_file ])
365+
366+ logger .info (f"Final yt-dlp command: { command } " )
276367 logger .info ("Downloading the selected quality video with audio..." )
277368 else :
278369 # Download audio-only as mp3
@@ -283,8 +374,25 @@ def get_format_sort_key(fmt):
283374 "--output" , audio_path ,
284375 url
285376 ]
286- if use_cookies and os .path .exists (cookies_file ):
377+
378+ # if use_cookies_file and os.path.exists(cookies_file):
379+ # command.extend(["--cookies", cookies_file])
380+
381+ # apply the cache logic
382+ if no_cache_dir :
383+ logger .info ("Disabling yt-dlp cache via config.ini (no_cache_dir=true)." )
384+ command .insert (1 , "--no-cache-dir" )
385+ elif custom_cache_dir :
386+ logger .info (f"Using custom yt-dlp cache directory: { custom_cache_dir } " )
387+ command .insert (1 , f"--cache-dir={ custom_cache_dir } " )
388+
389+ # cookies
390+ if use_browser_cookies and not use_cookies_file :
391+ command .extend (["--cookies-from-browser" , f"{ browser_type } :{ actual_browser_profile } " ])
392+ elif use_cookies_file and os .path .exists (cookies_file ):
287393 command .extend (["--cookies" , cookies_file ])
394+
395+ logger .info (f"Final yt-dlp command: { command } " )
288396 logger .info ("Downloading audio-only..." )
289397
290398 # Start the subprocess
@@ -821,9 +929,49 @@ def format_duration(duration):
821929
822930# Fetch details for videos
823931async def fetch_video_details (url , max_retries = 3 , base_delay = 5 , command_timeout = 30 ):
824- command = ["yt-dlp" , "--user-agent" ,
825- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" ,
826- "--dump-json" , url ]
932+ # command = ["yt-dlp", "--user-agent",
933+ # "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
934+ # "--dump-json", url]
935+
936+ config = ConfigLoader .get_config ()
937+
938+ # read your YTDLPSettings
939+ use_cookies_file = config .getboolean ('YTDLPSettings' , 'use_cookies_file' , fallback = False )
940+ cookies_file = config .get ('YTDLPSettings' , 'cookies_file' , fallback = 'config/cookies.txt' )
941+ use_browser_cookies = config .getboolean ('YTDLPSettings' , 'use_browser_cookies' , fallback = False )
942+ browser_type = config .get ('YTDLPSettings' , 'browser_type' , fallback = 'firefox' )
943+ browser_cookies_profile = config .get ('YTDLPSettings' , 'browser_cookies_profile' , fallback = '' )
944+
945+ # expand env var if needed
946+ actual_browser_profile = browser_cookies_profile
947+ if use_browser_cookies and browser_cookies_profile .startswith ('$' ):
948+ env_var_name = browser_cookies_profile [1 :]
949+ env_val = os .getenv (env_var_name , '' )
950+ if env_val :
951+ actual_browser_profile = env_val
952+
953+ command = [
954+ "yt-dlp" ,
955+ "--user-agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36..." ,
956+ "--dump-json" ,
957+ url
958+ ]
959+
960+ # if you prefer browser cookies over file
961+ if use_browser_cookies and not use_cookies_file :
962+ command .extend (["--cookies-from-browser" , f"{ browser_type } :{ actual_browser_profile } " ])
963+ elif use_cookies_file and os .path .exists (cookies_file ):
964+ command .extend (["--cookies" , cookies_file ])
965+
966+ # optional: handle no_cache_dir / custom_cache_dir too if you want
967+ no_cache_dir = config .getboolean ('YTDLPSettings' , 'no_cache_dir' , fallback = False )
968+ custom_cache_dir = config .get ('YTDLPSettings' , 'custom_cache_dir' , fallback = '' )
969+ if no_cache_dir :
970+ command .insert (1 , "--no-cache-dir" )
971+ elif custom_cache_dir :
972+ command .insert (1 , f"--cache-dir={ custom_cache_dir } " )
973+
974+ logger .info (f"fetch_video_details command: { command } " )
827975
828976 last_stderr_output = ""
829977
0 commit comments