shukdevdatta123 commited on
Commit
2dbd25a
·
verified ·
1 Parent(s): 60c6961

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -32
app.py CHANGED
@@ -55,40 +55,48 @@ def transcribe_audio(audio_file):
55
  except sr.RequestError:
56
  return "Could not request results from Google Speech Recognition service."
57
 
58
- # Function to get HTML content for extracting video URL
59
  def gethtml(url):
60
  headers = {
61
  "cache-Control": "no-cache",
62
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
63
  "accept-encoding": "gzip, deflate, br",
64
- "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
65
  "content-type": "application/x-www-form-urlencoded",
66
- "cookie": "lang=en; country=CN; uid=fd94a82a406a8dd4; sfHelperDist=72; reference=14;",
67
- "origin": "https://en.savefrom.net",
68
- "referer": "https://en.savefrom.net/1-youtube-video-downloader-4/",
69
  "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
70
  }
71
- kv = {"sf_url": url, "sf_submit": "", "new": "1", "lang": "en", "app": "", "country": "cn", "os": "Windows", "browser": "Chrome"}
 
 
 
 
 
 
 
72
  r = requests.post(url="https://en.savefrom.net/savefrom.php", headers=headers, data=kv)
73
  r.raise_for_status()
74
  return r.text
75
 
76
- # Function to extract the video download URL with better error handling
77
  def extract_video_url(youtube_url):
78
  try:
79
- # Get HTML content for the provided URL
80
  reo = gethtml(youtube_url)
81
-
82
- # Extract the script containing the video download info
83
- reo = reo.split("<script type=\"text/javascript\">")[1].split("</script>")[0]
 
 
 
 
 
84
  reo = reo.replace("(function(){", "(function(){\nthis.alert=function(){};")
85
  reA = reo.split("\n")
86
 
87
- # Ensure that the necessary script part is found
88
  if len(reA) < 3:
89
  raise ValueError("Could not extract valid script data from the YouTube page.")
90
 
91
- # Extract the video URL
92
  name = reA[len(reA) - 3].split(";")[0] + ";"
93
  addition = """
94
  const jsdom = require("jsdom");
@@ -101,24 +109,25 @@ def extract_video_url(youtube_url):
101
  ct = execjs.compile(addition + reo, cwd=r'C:\Users\19308\AppData\Roaming\npm\node_modules')
102
  text = ct.eval(name.split("=")[1].replace(";", ""))
103
 
104
- # Validate the extraction of the JSON data
105
- result = re.search('show\((.*?)\);;', text, re.I | re.M)
106
- if not result:
107
- raise ValueError("No video download URL found in the script data.")
108
-
109
- result = result.group(0).replace("show(", "").replace(");;", "")
110
-
111
- # Parse the result as JSON
112
- j = json.loads(result)
113
-
114
- # Ensure that the URL data exists
115
- if "url" not in j or len(j["url"]) <= 1:
116
- raise ValueError("No valid download links found for this video.")
 
 
 
117
 
118
- # Return the download URL for the video
119
- num = 1 # Get the second URL from the available options
120
- downurl = j["url"][num]["url"]
121
- return downurl
122
 
123
  except Exception as e:
124
  raise ValueError(f"Error occurred while extracting the download URL: {e}")
@@ -247,10 +256,13 @@ elif tab == "Audio":
247
  mime="audio/wav"
248
  )
249
 
250
- # Streamlit UI for YouTube download
251
  elif tab == "YouTube":
 
 
 
252
  youtube_url = st.text_input("Enter YouTube Video URL", "https://www.youtube.com/watch?v=YPvtz1lHRiw")
253
-
254
  if st.button("Get Download Link"):
255
  if youtube_url:
256
  try:
 
55
  except sr.RequestError:
56
  return "Could not request results from Google Speech Recognition service."
57
 
58
+ # Function to get the HTML of the page
59
  def gethtml(url):
60
  headers = {
61
  "cache-Control": "no-cache",
62
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
63
  "accept-encoding": "gzip, deflate, br",
64
+ "accept-language": "en-US,en;q=0.9",
65
  "content-type": "application/x-www-form-urlencoded",
 
 
 
66
  "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
67
  }
68
+ kv = {"sf_url": url,
69
+ "sf_submit": "",
70
+ "new": "1",
71
+ "lang": "en",
72
+ "app": "",
73
+ "country": "us",
74
+ "os": "Windows",
75
+ "browser": "Chrome"}
76
  r = requests.post(url="https://en.savefrom.net/savefrom.php", headers=headers, data=kv)
77
  r.raise_for_status()
78
  return r.text
79
 
80
+ # Function to extract the video download URL
81
  def extract_video_url(youtube_url):
82
  try:
83
+ # Get the HTML content of the YouTube page
84
  reo = gethtml(youtube_url)
85
+
86
+ # Try extracting the relevant script tag containing download information
87
+ try:
88
+ reo = reo.split("<script type=\"text/javascript\">")[1].split("</script>")[0]
89
+ except IndexError:
90
+ raise ValueError("Could not find the script containing video data in the HTML response.")
91
+
92
+ # Modify the script to allow extraction
93
  reo = reo.replace("(function(){", "(function(){\nthis.alert=function(){};")
94
  reA = reo.split("\n")
95
 
 
96
  if len(reA) < 3:
97
  raise ValueError("Could not extract valid script data from the YouTube page.")
98
 
99
+ # Extract the JSON object containing the video download URLs
100
  name = reA[len(reA) - 3].split(";")[0] + ";"
101
  addition = """
102
  const jsdom = require("jsdom");
 
109
  ct = execjs.compile(addition + reo, cwd=r'C:\Users\19308\AppData\Roaming\npm\node_modules')
110
  text = ct.eval(name.split("=")[1].replace(";", ""))
111
 
112
+ # Extract and parse the JSON
113
+ try:
114
+ result = re.search('show\((.*?)\);;', text, re.I | re.M)
115
+ if result is None:
116
+ raise ValueError("No valid video download URL found in the extracted data.")
117
+
118
+ result = result.group(0).replace("show(", "").replace(");;", "")
119
+ j = json.loads(result)
120
+
121
+ # Ensure the JSON contains the expected download URLs
122
+ if "url" not in j or len(j["url"]) == 0:
123
+ raise ValueError("No valid download links found in the extracted data.")
124
+
125
+ # Assuming the first video URL is what we want (or try a different index if necessary)
126
+ downurl = j["url"][0]["url"]
127
+ return downurl
128
 
129
+ except (IndexError, KeyError, json.JSONDecodeError) as e:
130
+ raise ValueError(f"Error occurred while extracting the download URL: {e}")
 
 
131
 
132
  except Exception as e:
133
  raise ValueError(f"Error occurred while extracting the download URL: {e}")
 
256
  mime="audio/wav"
257
  )
258
 
259
+ # Streamlit UI for YouTube video download
260
  elif tab == "YouTube":
261
+ st.title("YouTube Video Downloader")
262
+ st.write("""This app allows you to download YouTube videos in various formats. Simply enter the YouTube URL below and click "Get Download Link".""")
263
+
264
  youtube_url = st.text_input("Enter YouTube Video URL", "https://www.youtube.com/watch?v=YPvtz1lHRiw")
265
+
266
  if st.button("Get Download Link"):
267
  if youtube_url:
268
  try: