Drastic optimizations to youtube player

This commit is contained in:
insert 2024-08-04 00:22:07 -04:00
parent cb37e1c7b6
commit 689629aa37
Signed by: insert
GPG key ID: A70775C389ACF105

17
bot.py
View file

@ -2,6 +2,7 @@ from selenium.webdriver.firefox.options import Options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
import time
import disnake
@ -39,35 +40,31 @@ async def on_ready():
def play_video(videourl):
driver.get(videourl)
sleep(4)
try:
elem = driver.find_element(By.CLASS_NAME, "ytp-fullscreen-button")
elem = WebDriverWait(driver, 5, 0.2, None).until(lambda x: x.find_element(By.CLASS_NAME, "ytp-fullscreen-button"))
sleep(1)
elem.send_keys(Keys.RETURN)
except Exception:
return #if this errors there is no fullscreen options, such as playlists, so skip the link
#guess I don't need this
#sleep(1.5)
#elem = driver.find_element(By.XPATH, '//button[@aria-keyshortcuts="k"]')
#elem.send_keys(Keys.RETURN)
try:
elem = driver.find_element(By.XPATH, '//button[@data-tooltip-target-id="ytp-autonav-toggle-button"][@aria-label="Autoplay is on"]')
elem.send_keys(Keys.RETURN)
except Exception:
pass
sleep(10)
#TODO: see if this is actually needed
try:
elem = driver.find_element(By.XPATH, '//button[@aria-label="Dismiss"]')
elem = WebDriverWait(driver, 10, 0.2, (ElementNotVisibleException)).until(lambda x: x.find_element(By.XPATH, '//button[@aria-label="Dismiss"]').is_displayed())
elem.send_keys(Keys.RETURN)
except Exception:
pass
try:
endscreen = driver.find_element(By.CLASS_NAME, "html5-endscreen")
timeout = (float(os.getenv("MAX_MIN")) * 60) + time.time() - 10 #subtract 10 since 10 seconds of the video has played by this point
timeout = (float(os.getenv("MAX_MIN")) * 60) + time.time()
except Exception:
return #same as above
while str(endscreen.get_attribute('style')) == "display: none;" and time.time() < timeout:
pass
sleep(2)
sleep(1)
return
@bot.slash_command(