|
| 1 | +# Driver |
| 2 | +# ====== |
| 3 | +# |
| 4 | +# This is the base module that all scripts will import from. Basically, we just |
| 5 | +# set up the web driver in here, along with an desired options (e.g. Firefox or, |
| 6 | +# Chrome, headless, height and width, etc) |
| 7 | +# |
| 8 | +import selenium |
| 9 | +import selenium.webdriver |
| 10 | + |
| 11 | +from selenium.webdriver.firefox.options import Options |
| 12 | +from selenium.webdriver.common.keys import Keys |
| 13 | +# See: https://selenium-python.readthedocs.io/waits.html#explicit-waits |
| 14 | +from selenium.webdriver.common.by import By |
| 15 | +from selenium.webdriver.support.ui import WebDriverWait |
| 16 | +from selenium.webdriver.support import expected_conditions as Condition |
| 17 | + |
| 18 | +import time |
| 19 | +import random |
| 20 | +import os |
| 21 | + |
| 22 | +# Setup headless browser using Firefox |
| 23 | +driver_options = Options() |
| 24 | +driver_options.headless = False |
| 25 | + |
| 26 | +# It's important we set height and width arguments otherwise page content won't |
| 27 | +# render correctly and we can't do things like scroll the full page height! |
| 28 | +# |
| 29 | +# Actually, this doesn't do anything for Firefox... see below |
| 30 | +driver_options.add_argument('--height 900') |
| 31 | +driver_options.add_argument('--width 1600') |
| 32 | + |
| 33 | +# Gets current path |
| 34 | +current_path = os.getcwd() |
| 35 | + |
| 36 | +driver = selenium.webdriver.Firefox(options=driver_options) |
| 37 | + |
| 38 | +# Installs youtube nonstop firefox extension to succesfully autoplay videos |
| 39 | +driver.install_addon(current_path + '/streaming/youtube_nonstop-0.8.2-fx.xpi', temporary=True) |
| 40 | + |
| 41 | +# We can set the height here |
| 42 | +driver.set_window_size(height=900, width=1600) |
| 43 | + |
| 44 | +# Set an implicit wait for all elements not immediately found |
| 45 | +driver.implicitly_wait(10) |
| 46 | +wait = WebDriverWait(driver, 60) |
| 47 | + |
| 48 | + |
| 49 | +def current(): |
| 50 | + print(f'At {driver.current_url}') |
| 51 | + |
| 52 | +def is_ad(): |
| 53 | + ad_status = driver.execute_script( |
| 54 | + "return document.getElementById('movie_player').getAdState()" |
| 55 | + ) |
| 56 | + return ad_status != -1 |
| 57 | + |
| 58 | +def ensure_playing(): |
| 59 | + |
| 60 | + # Wait for the video player to be accessible, then make sure it's playing |
| 61 | + player = wait.until( |
| 62 | + Condition.presence_of_element_located((By.ID, 'movie_player')) |
| 63 | + ) |
| 64 | + # If the video hasn't started playing we can hit the play button! |
| 65 | + player_status = driver.execute_script( |
| 66 | + "return document.getElementById('movie_player').getPlayerState()" |
| 67 | + ) |
| 68 | + # A status of -1 means it hasn't started yet, 2 means it's paused. 0 means it |
| 69 | + # has ended. |
| 70 | + if player_status in [-1, 2] and not is_ad(): |
| 71 | + player.click() |
| 72 | + elif player_status == 0: |
| 73 | + driver.execute_scripts( |
| 74 | + "document.getElementById('movie_player').nextVideo()" |
| 75 | + ) |
| 76 | + |
| 77 | +# def click_player(): |
| 78 | + |
| 79 | +# player = wait.until( |
| 80 | +# Condition.presence_of_element_located((By.ID, 'movie_player')) |
| 81 | +# ) |
| 82 | + |
| 83 | +# # -1 means no ad. If it's anything else, don't click! It could pull us to |
| 84 | +# # a new tab and really mess things up. |
| 85 | +# if not is_ad(): |
| 86 | +# player.click() |
| 87 | +# else: |
| 88 | +# pass |
| 89 | + |
| 90 | + |
| 91 | +baseurl = 'https://www.youtube.com/watch?v=DqbOgx_FCbw&list=PL6aq1PBlrtR5D4xslD4VBos7zk0GSTuBb' |
| 92 | +driver.get(baseurl) |
| 93 | + |
| 94 | +time.sleep(5) |
| 95 | + |
| 96 | +ensure_playing() |
| 97 | + |
| 98 | +time.sleep(5) |
| 99 | + |
| 100 | +while True: |
| 101 | + |
| 102 | + current() |
| 103 | + # click_player() # pause |
| 104 | + time.sleep(1) |
| 105 | + ensure_playing() # play |
| 106 | + time.sleep(random.randrange(5,18)*60) |
| 107 | + |
0 commit comments