danilotpnta commited on
Commit
2faab13
·
1 Parent(s): f4549cb
Files changed (2) hide show
  1. app.py +1 -2
  2. download_video.py +23 -14
app.py CHANGED
@@ -5,8 +5,7 @@ import os
5
  import warnings
6
  warnings.filterwarnings("ignore", category=FutureWarning, module="torch")
7
 
8
-
9
- from download_video import download_mp3_selenium # Assuming you saved your Selenium code in 'selenium_download.py'
10
 
11
  # Function to download the audio, title, and thumbnail from YouTube
12
  def download_video_info(url):
 
5
  import warnings
6
  warnings.filterwarnings("ignore", category=FutureWarning, module="torch")
7
 
8
+ from download_video import download_mp3_selenium
 
9
 
10
  # Function to download the audio, title, and thumbnail from YouTube
11
  def download_video_info(url):
download_video.py CHANGED
@@ -1,8 +1,11 @@
1
  from selenium import webdriver
2
  from selenium.webdriver.common.by import By
3
  from selenium.webdriver.common.keys import Keys
 
 
 
 
4
  import requests
5
- import time
6
 
7
  def download_mp3_selenium(youtube_url):
8
  # Set up the Selenium WebDriver
@@ -10,31 +13,40 @@ def download_mp3_selenium(youtube_url):
10
  options.add_argument("--headless")
11
  options.add_argument("--no-sandbox")
12
  options.add_argument('--disable-dev-shm-usage')
 
 
 
 
 
13
  driver = webdriver.Chrome(options=options)
14
 
 
 
 
15
  # Open the YouTube video page
16
  driver.get(youtube_url)
17
- time.sleep(2) # Wait for the page to load
18
-
 
19
  # Scrape the title
20
  title = driver.title # This gives you the video title
21
 
22
- # Scrape the thumbnail (YouTube page has a meta tag for the thumbnail)
23
- thumbnail_meta = driver.find_element(By.XPATH, "//meta[@property='og:image']")
24
  thumbnail_url = thumbnail_meta.get_attribute('content')
25
 
26
  # Open the YouTube downloader site
27
  driver.get("https://yt1d.com/en/")
28
- time.sleep(2) # Wait for the page to load
 
29
 
30
  # Input the YouTube URL into the downloader
31
  input_box = driver.find_element(By.ID, "txt-url")
32
  input_box.send_keys(youtube_url)
33
  input_box.send_keys(Keys.RETURN)
34
- time.sleep(2) # Wait for the download options to load
35
-
36
  # Wait for the MP3 download button to appear
37
- mp3_download_button = driver.find_element(By.CSS_SELECTOR, "button[data-ftype='mp3']")
38
  onclick_attr = mp3_download_button.get_attribute("onclick")
39
 
40
  # Extract parameters from the JavaScript function call
@@ -42,11 +54,8 @@ def download_mp3_selenium(youtube_url):
42
  if len(params) >= 7:
43
  mp3_download_url = params[1] # Extracted base download URL
44
 
45
- # Wait for the JavaScript to modify the link
46
- time.sleep(2) # Allow time for the page to modify the link
47
-
48
- # Get the final download URL after JavaScript modifications
49
- final_link = driver.find_element(By.CSS_SELECTOR, "a[href*='googlevideo.com/videoplayback']")
50
  mp3_download_url = final_link.get_attribute("href")
51
  print(f"Final MP3 Download URL: {mp3_download_url}")
52
 
 
1
  from selenium import webdriver
2
  from selenium.webdriver.common.by import By
3
  from selenium.webdriver.common.keys import Keys
4
+ from selenium.webdriver.support.ui import WebDriverWait
5
+ from selenium.webdriver.support import expected_conditions as EC
6
+ from selenium.webdriver.chrome.service import Service
7
+ from webdriver_manager.chrome import ChromeDriverManager
8
  import requests
 
9
 
10
  def download_mp3_selenium(youtube_url):
11
  # Set up the Selenium WebDriver
 
13
  options.add_argument("--headless")
14
  options.add_argument("--no-sandbox")
15
  options.add_argument('--disable-dev-shm-usage')
16
+
17
+ # Use WebDriverManager to handle the ChromeDriver version
18
+ driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
19
+
20
+
21
  driver = webdriver.Chrome(options=options)
22
 
23
+ # Set up WebDriverWait (with a timeout of 10 seconds)
24
+ wait = WebDriverWait(driver, 10)
25
+
26
  # Open the YouTube video page
27
  driver.get(youtube_url)
28
+ # Wait for the title to be available
29
+ wait.until(EC.title_contains("YouTube"))
30
+
31
  # Scrape the title
32
  title = driver.title # This gives you the video title
33
 
34
+ # Wait for the thumbnail to load and scrape it
35
+ thumbnail_meta = wait.until(EC.presence_of_element_located((By.XPATH, "//meta[@property='og:image']")))
36
  thumbnail_url = thumbnail_meta.get_attribute('content')
37
 
38
  # Open the YouTube downloader site
39
  driver.get("https://yt1d.com/en/")
40
+ # Wait until the page is loaded completely by checking an element presence
41
+ wait.until(EC.presence_of_element_located((By.ID, "txt-url")))
42
 
43
  # Input the YouTube URL into the downloader
44
  input_box = driver.find_element(By.ID, "txt-url")
45
  input_box.send_keys(youtube_url)
46
  input_box.send_keys(Keys.RETURN)
47
+
 
48
  # Wait for the MP3 download button to appear
49
+ mp3_download_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-ftype='mp3']")))
50
  onclick_attr = mp3_download_button.get_attribute("onclick")
51
 
52
  # Extract parameters from the JavaScript function call
 
54
  if len(params) >= 7:
55
  mp3_download_url = params[1] # Extracted base download URL
56
 
57
+ # Wait for the final download URL to be available after JavaScript modifications
58
+ final_link = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "a[href*='googlevideo.com/videoplayback']")))
 
 
 
59
  mp3_download_url = final_link.get_attribute("href")
60
  print(f"Final MP3 Download URL: {mp3_download_url}")
61