Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
239 |
-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/
|
240 |
-
'Mozilla/5.0 (
|
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 |
-
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
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 |
-
'
|
|
|
|
|
|
|
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 |
-
**
|
|
|
|
|
394 |
|
395 |
-
|
396 |
-
|
397 |
-
-
|
398 |
-
-
|
399 |
|
400 |
-
2.
|
401 |
-
-
|
402 |
-
-
|
403 |
-
-
|
404 |
|
405 |
-
3.
|
406 |
-
-
|
407 |
-
-
|
408 |
-
-
|
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 |
""")
|