ruslanmv commited on
Commit
e3b852f
·
verified ·
1 Parent(s): 7b7a7eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -13
app.py CHANGED
@@ -31,6 +31,11 @@ def download_video(url):
31
  )
32
  print("Downloaded")
33
  return local_file
 
 
 
 
 
34
  def validate_youtube(url):
35
  try:
36
  yt = YouTube(url)
@@ -38,21 +43,33 @@ def validate_youtube(url):
38
  print(f"Invalid URL or network issue: {e}")
39
  return True
40
 
41
- # Fetch video details using multiple fallback methods
42
  try:
43
- # Try getting length from `yt.length`
44
  video_length = yt.length
45
- if video_length is None:
46
- raise ValueError("yt.length returned None")
47
-
48
- except:
49
- # Fallback: Extract video details from `yt.watch_html`
50
- match = re.search(r'"lengthSeconds":"(\d+)"', yt.watch_html)
51
- if match:
52
- video_length = int(match.group(1))
53
- else:
54
- print("Error: Could not retrieve video length.")
55
- return True
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Check video length limit
58
  if video_length > 600:
@@ -61,6 +78,8 @@ def validate_youtube(url):
61
  else:
62
  print("Your video is less than 10 minutes")
63
  return False
 
 
64
  def validate_url(url):
65
  import validators
66
  if not validators.url(url):
 
31
  )
32
  print("Downloaded")
33
  return local_file
34
+
35
+ import json
36
+ import re
37
+ from pytube import YouTube
38
+
39
  def validate_youtube(url):
40
  try:
41
  yt = YouTube(url)
 
43
  print(f"Invalid URL or network issue: {e}")
44
  return True
45
 
 
46
  try:
47
+ # First try: Direct access (most reliable)
48
  video_length = yt.length
49
+
50
+ except Exception as e:
51
+ try:
52
+ # Fallback 1: Try extracting from embed HTML (more robust than watch_html)
53
+ embed_html = yt.embed_html
54
+ match = re.search(r'"lengthSeconds":"(\d+)"', embed_html)
55
+ if match:
56
+ video_length = int(match.group(1))
57
+ else:
58
+ raise ValueError("Could not find length in embed HTML") # Explicitly raise to trigger next fallback
59
+
60
+ except Exception as e:
61
+ try:
62
+ # Fallback 2: Try watch_html (less reliable, but a backup)
63
+ match = re.search(r'"lengthSeconds":"(\d+)"', yt.watch_html)
64
+ if match:
65
+ video_length = int(match.group(1))
66
+ else:
67
+ raise ValueError("Could not find length in watch HTML") # Explicitly raise to trigger final return
68
+
69
+ except Exception as e:
70
+ print(f"Error: Could not retrieve video length: {e}") # Print the actual exception
71
+ return True # Return True indicating failure
72
+
73
 
74
  # Check video length limit
75
  if video_length > 600:
 
78
  else:
79
  print("Your video is less than 10 minutes")
80
  return False
81
+
82
+
83
  def validate_url(url):
84
  import validators
85
  if not validators.url(url):