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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -17
app.py CHANGED
@@ -18,7 +18,9 @@ from moviepy.editor import *
18
  from pytube import YouTube
19
  from youtube_transcript_api import YouTubeTranscriptApi
20
  from utils import *
21
-
 
 
22
  def download_video(url):
23
  print("Downloading...")
24
  local_file = (
@@ -29,33 +31,36 @@ def download_video(url):
29
  )
30
  print("Downloaded")
31
  return local_file
32
-
33
  def validate_youtube(url):
34
- # This creates a YouTube object
35
  try:
36
- yt = YouTube(url)
37
  except Exception as e:
38
- print("Invalid URL or network issue:", e)
39
  return True
40
 
41
- # Fetch video details safely
42
- video_details = yt.vid_info.get('videoDetails', {})
43
- length_seconds = video_details.get('lengthSeconds')
 
 
 
44
 
45
- # Ensure lengthSeconds is not None before converting to int
46
- if length_seconds is None:
47
- print("Error: Could not retrieve video length.")
48
- return True
49
-
50
- video_length = int(length_seconds)
 
 
51
 
 
52
  if video_length > 600:
53
  print("Your video is longer than 10 minutes")
54
  return True
55
  else:
56
  print("Your video is less than 10 minutes")
57
  return False
58
-
59
  def validate_url(url):
60
  import validators
61
  if not validators.url(url):
@@ -63,8 +68,6 @@ def validate_url(url):
63
  return True
64
  else:
65
  return False
66
-
67
-
68
  def cleanup():
69
  import pathlib
70
  import glob
 
18
  from pytube import YouTube
19
  from youtube_transcript_api import YouTubeTranscriptApi
20
  from utils import *
21
+ import json
22
+ import re
23
+ from pytube import YouTube
24
  def download_video(url):
25
  print("Downloading...")
26
  local_file = (
 
31
  )
32
  print("Downloaded")
33
  return local_file
 
34
  def validate_youtube(url):
 
35
  try:
36
+ yt = YouTube(url)
37
  except Exception as e:
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:
59
  print("Your video is longer than 10 minutes")
60
  return True
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):
 
68
  return True
69
  else:
70
  return False
 
 
71
  def cleanup():
72
  import pathlib
73
  import glob