From 689629aa37ee931cd6ac02139a19125b5037394b Mon Sep 17 00:00:00 2001 From: insert Date: Sun, 4 Aug 2024 00:22:07 -0400 Subject: [PATCH] Drastic optimizations to youtube player --- bot.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/bot.py b/bot.py index c25522f..ab4d63c 100644 --- a/bot.py +++ b/bot.py @@ -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(