ruslanmv commited on
Commit
4ab7878
·
verified ·
1 Parent(s): e3b852f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -38
app.py CHANGED
@@ -32,54 +32,27 @@ def download_video(url):
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)
 
42
  except Exception as e:
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:
76
- print("Your video is longer than 10 minutes")
77
  return True
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):
 
32
  print("Downloaded")
33
  return local_file
34
 
 
 
 
 
35
  def validate_youtube(url):
36
+ """
37
+ Validates a YouTube URL, checks if the video exists, and returns whether its length exceeds 10 minutes.
38
+
39
+ :param url: str - YouTube video URL
40
+ :return: bool - True if the URL is invalid or video is longer than 10 minutes, otherwise False
41
+ """
42
  try:
43
  yt = YouTube(url)
44
+ video_length = yt.length # Video length in seconds
45
  except Exception as e:
46
+ print(f"Error: The provided URL is invalid or not accessible. ({e})")
47
+ return True # Return True since the URL is invalid
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  if video_length > 600:
50
+ print("Your video is longer than 10 minutes.")
51
  return True
52
  else:
53
+ print("Your video is 10 minutes or shorter.")
54
  return False
55
 
 
56
  def validate_url(url):
57
  import validators
58
  if not validators.url(url):