netflix-clicker/browser.py
2024-03-31 19:48:36 +08:00

63 lines
2.1 KiB
Python

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from loguru import logger
from fake_useragent import UserAgent
from functools import cache
import time
class AbstractChrome:
def __init__(self, headless: bool = True):
self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.add_argument(f'user-agent={self._user_aget}')
self.chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
if headless:
self.chrome_options.add_argument('--mute-audio')
self.chrome_options.add_argument('--auto-open-devtools-for-tabs')
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--window-size=1440,900')
self.chrome_options.add_argument('--disable-gpu')
self.chrome_options.add_argument('--disable-dev-shm-usage')
self.chrome_options.add_argument("--log-level=3")
self.driver = webdriver.Chrome(
options=self.chrome_options
)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.driver.close()
@property
@cache
def _user_aget(self):
ua = UserAgent()
return ua.googlechrome
class PrimaryLocationClicker(AbstractChrome):
def click(self, auth_url: str):
self.driver.get(auth_url)
try:
element = self.driver.find_element(By.CSS_SELECTOR, '[data-uia="set-primary-location-action"]')
element.click()
except NoSuchElementException as e:
self.driver.save_screenshot(f'{time.time()}.png')
logger.error(e)
class TravelVerificationRetriever(AbstractChrome):
def get_code(self, auth_url: str) -> str:
self.driver.get(auth_url)
try:
element = self.driver.find_element(By.CSS_SELECTOR, '[data-uia="travel-verification-otp"]')
return element.text
except NoSuchElementException as e:
self.driver.save_screenshot(f'{time.time()}.png')
logger.error(e)