jpjp9292 commited on
Commit
68f2114
β€’
1 Parent(s): e9a55dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -27
app.py CHANGED
@@ -235,15 +235,14 @@ def get_random_user_agent():
235
  """랜덀 User-Agent 생성"""
236
  user_agents = [
237
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
238
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
239
- 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
240
- 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
241
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0'
242
  ]
243
  return random.choice(user_agents)
244
 
245
  def validate_cookies():
246
- """μΏ ν‚€ 파일 μœ νš¨μ„± 검사 및 κ°±μ‹ """
247
  if not os.path.exists(COOKIES_FILE):
248
  return False, "Cookie file not found"
249
 
@@ -256,6 +255,8 @@ def validate_cookies():
256
 
257
  def download_with_retry(url, ydl_opts, max_retries=3, delay=5):
258
  """μž¬μ‹œλ„ 둜직이 ν¬ν•¨λœ λ‹€μš΄λ‘œλ“œ ν•¨μˆ˜"""
 
 
259
  for attempt in range(max_retries):
260
  try:
261
  # 맀 μ‹œλ„λ§ˆλ‹€ μƒˆλ‘œμš΄ User-Agent μ‚¬μš©
@@ -267,19 +268,27 @@ def download_with_retry(url, ydl_opts, max_retries=3, delay=5):
267
  return filename, info
268
 
269
  except yt_dlp.utils.DownloadError as e:
 
270
  if "Sign in to confirm you're not a bot" in str(e):
271
  if attempt < max_retries - 1:
272
  st.warning(f"Bot detection encountered. Retrying in {delay} seconds... (Attempt {attempt + 1}/{max_retries})")
273
  time.sleep(delay)
274
  delay *= 2 # μ§€μˆ˜ λ°±μ˜€ν”„
275
  continue
276
- raise e
 
 
 
 
277
  except Exception as e:
 
278
  if attempt < max_retries - 1:
279
  st.warning(f"Error occurred. Retrying... (Attempt {attempt + 1}/{max_retries})")
280
  time.sleep(delay)
281
  continue
282
- raise e
 
 
283
 
284
  def download_video(url):
285
  try:
@@ -288,9 +297,8 @@ def download_video(url):
288
  'outtmpl': str(OUTPUT_DIR / '%(title)s.%(ext)s'),
289
  'merge_output_format': 'mp4',
290
 
291
- # μΏ ν‚€ 및 인증 μ„€μ •
292
  'cookiefile': COOKIES_FILE,
293
- 'cookiesfrombrowser': ('chrome',),
294
 
295
  # ν–₯μƒλœ μ˜΅μ…˜
296
  'quiet': True,
@@ -303,7 +311,7 @@ def download_video(url):
303
  # ν–₯μƒλœ 헀더
304
  'headers': {
305
  'User-Agent': get_random_user_agent(),
306
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
307
  'Accept-Language': 'en-US,en;q=0.5',
308
  'Accept-Encoding': 'gzip, deflate, br',
309
  'Connection': 'keep-alive',
@@ -312,14 +320,17 @@ def download_video(url):
312
  'Sec-Fetch-Mode': 'navigate',
313
  'Sec-Fetch-Site': 'none',
314
  'Sec-Fetch-User': '?1',
315
- 'Cache-Control': 'max-age=0',
 
 
 
316
  },
317
 
318
  # μΆ”κ°€ μ˜΅μ…˜
319
  'age_limit': None,
320
  'geo_bypass': True,
321
  'geo_bypass_country': 'US',
322
- 'sleep_interval': 2, # μš”μ²­ 사이 λŒ€κΈ° μ‹œκ°„
323
  'max_sleep_interval': 5,
324
  }
325
 
@@ -337,7 +348,7 @@ def download_video(url):
337
  status_text.text(f"Downloading... Speed: {d.get('speed_str', 'N/A')}")
338
  elif d['status'] == 'finished':
339
  progress_bar.progress(1.0)
340
- status_text.text("Processing...")
341
 
342
  ydl_opts['progress_hooks'] = [progress_hook]
343
 
@@ -389,21 +400,23 @@ if st.button("Download Video"):
389
 
390
  # Help μ„Ήμ…˜
391
  with st.expander("ℹ️ Troubleshooting Guide"):
392
- st.markdown("""
393
- **If you encounter 'Sign in to confirm you're not a bot' error:**
 
 
394
 
395
- 1. Cookie Solution:
396
- - Make sure you're logged into YouTube in your browser
397
- - Install 'Get cookies.txt' or similar extension
398
- - Export fresh cookies and replace the existing cookie file
399
 
400
- 2. General Tips:
401
- - Wait a few minutes between download attempts
402
- - Try using a different video URL
403
- - Clear your browser cache before exporting cookies
404
 
405
- 3. Advanced Tips:
406
- - The app automatically retries downloads with different settings
407
- - Each retry uses a different User-Agent to avoid detection
408
- - Built-in delays help avoid triggering YouTube's protection
409
  """)
 
235
  """랜덀 User-Agent 생성"""
236
  user_agents = [
237
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
238
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0',
239
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15',
240
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'
 
241
  ]
242
  return random.choice(user_agents)
243
 
244
  def validate_cookies():
245
+ """μΏ ν‚€ 파일 μœ νš¨μ„± 검사"""
246
  if not os.path.exists(COOKIES_FILE):
247
  return False, "Cookie file not found"
248
 
 
255
 
256
  def download_with_retry(url, ydl_opts, max_retries=3, delay=5):
257
  """μž¬μ‹œλ„ 둜직이 ν¬ν•¨λœ λ‹€μš΄λ‘œλ“œ ν•¨μˆ˜"""
258
+ last_error = None
259
+
260
  for attempt in range(max_retries):
261
  try:
262
  # 맀 μ‹œλ„λ§ˆλ‹€ μƒˆλ‘œμš΄ User-Agent μ‚¬μš©
 
268
  return filename, info
269
 
270
  except yt_dlp.utils.DownloadError as e:
271
+ last_error = e
272
  if "Sign in to confirm you're not a bot" in str(e):
273
  if attempt < max_retries - 1:
274
  st.warning(f"Bot detection encountered. Retrying in {delay} seconds... (Attempt {attempt + 1}/{max_retries})")
275
  time.sleep(delay)
276
  delay *= 2 # μ§€μˆ˜ λ°±μ˜€ν”„
277
  continue
278
+ elif "HTTP Error 429" in str(e):
279
+ if attempt < max_retries - 1:
280
+ st.warning(f"Rate limit detected. Waiting {delay} seconds... (Attempt {attempt + 1}/{max_retries})")
281
+ time.sleep(delay * 2) # 레이트 λ¦¬λ°‹μ˜ 경우 더 κΈ΄ λŒ€κΈ° μ‹œκ°„
282
+ continue
283
  except Exception as e:
284
+ last_error = e
285
  if attempt < max_retries - 1:
286
  st.warning(f"Error occurred. Retrying... (Attempt {attempt + 1}/{max_retries})")
287
  time.sleep(delay)
288
  continue
289
+
290
+ # λͺ¨λ“  μž¬μ‹œλ„ μ‹€νŒ¨ ν›„
291
+ raise last_error or Exception("Download failed after all retries")
292
 
293
  def download_video(url):
294
  try:
 
297
  'outtmpl': str(OUTPUT_DIR / '%(title)s.%(ext)s'),
298
  'merge_output_format': 'mp4',
299
 
300
+ # μΏ ν‚€ μ„€μ •
301
  'cookiefile': COOKIES_FILE,
 
302
 
303
  # ν–₯μƒλœ μ˜΅μ…˜
304
  'quiet': True,
 
311
  # ν–₯μƒλœ 헀더
312
  'headers': {
313
  'User-Agent': get_random_user_agent(),
314
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
315
  'Accept-Language': 'en-US,en;q=0.5',
316
  'Accept-Encoding': 'gzip, deflate, br',
317
  'Connection': 'keep-alive',
 
320
  'Sec-Fetch-Mode': 'navigate',
321
  'Sec-Fetch-Site': 'none',
322
  'Sec-Fetch-User': '?1',
323
+ 'DNT': '1',
324
+ 'Sec-GPC': '1',
325
+ 'Pragma': 'no-cache',
326
+ 'Cache-Control': 'no-cache',
327
  },
328
 
329
  # μΆ”κ°€ μ˜΅μ…˜
330
  'age_limit': None,
331
  'geo_bypass': True,
332
  'geo_bypass_country': 'US',
333
+ 'sleep_interval': 2,
334
  'max_sleep_interval': 5,
335
  }
336
 
 
348
  status_text.text(f"Downloading... Speed: {d.get('speed_str', 'N/A')}")
349
  elif d['status'] == 'finished':
350
  progress_bar.progress(1.0)
351
+ status_text.text("Processing downloaded files...")
352
 
353
  ydl_opts['progress_hooks'] = [progress_hook]
354
 
 
400
 
401
  # Help μ„Ήμ…˜
402
  with st.expander("ℹ️ Troubleshooting Guide"):
403
+ st.markdown(f"""
404
+ **Current Status:**
405
+ - Cookie File: {'βœ… Found' if os.path.exists(COOKIES_FILE) else '❌ Not Found'}
406
+ - Cookie Validity: {'βœ… Valid' if cookie_valid else '❌ Invalid'}
407
 
408
+ **Common Issues:**
409
+ 1. Bot Detection:
410
+ - The app will automatically retry with different settings
411
+ - Each retry uses a random User-Agent and headers
412
 
413
+ 2. Cookie Problems:
414
+ - Current cookie file: {COOKIES_FILE}
415
+ - Make sure the cookie file is up to date
416
+ - Export fresh cookies when issues persist
417
 
418
+ 3. Download Fails:
419
+ - Try again after a few minutes
420
+ - Check if the video is available in your region
421
+ - Verify that the video is public
422
  """)