buctAuthentication/Authtication.py
2026-01-09 00:46:18 +08:00

93 lines
3.4 KiB
Python

import datetime
import logging
import os
import time
import requests
import selenium.common.exceptions
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
logging.basicConfig(
level=logging.INFO,
format="%(levelno)s - %(levelname)s - %(asctime)s - %(name)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
filename=f"logs/{datetime.date.today()}.log",
filemode="a"
)
class BUCTAU:
def __init__(self):
load_dotenv()
self.USER_USERNAME = os.environ.get('BUCT_AUTHENTICATION_USERNAME')
self.USER_PASSWORD = os.environ.get('BUCT_AUTHENTICATION_PASSWORD')
self.REMOTE_ADDR = os.environ.get('SELENIUM_DRIVER_ADDR')
logging.info(f"BUCTAU initialized with username:{self.USER_USERNAME}")
logging.info(f"BUCTAU initialized with password:{self.USER_PASSWORD}")
def init_driver_chrome(self):
"""Initialize the Selenium WebDriver with Chrome options.
Returns:
WebDriver: The initialized Selenium WebDriver instance.
"""
from selenium.webdriver.chrome.options import Options
options = Options()
# options.add_argument("--headless") # Run in headless mode
options.add_argument("--no-sandbox")
# options.add_experimental_option("detach", True)
driver = webdriver.Remote(command_executor=self.REMOTE_ADDR, options=options)
driver.implicitly_wait(5) # Implicit wait for elements to load
return driver
def login(self, driver):
"""open the page and login"""
logging.info("LOGIN")
driver.get("https://tree.buct.edu.cn/index_20.html")
driver.maximize_window()
try:
driver.find_element(By.XPATH, '/html/body/main/section/div[1]/div[2]/input').send_keys(self.USER_USERNAME)
driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(self.USER_PASSWORD)
login_button = driver.find_element(By.XPATH, '/html/body/main/section/div[1]/div[8]/button[1]')
driver.execute_script("arguments[0].click();", login_button)
# driver.find_element(By.ID,"login-account").click()
return True
except selenium.common.exceptions.NoSuchElementException as e:
logging.info("NoSuchElementException: ", e)
return False
@staticmethod
def detect_net_requests():
# noinspection PyBroadException
try:
detector = requests.get(url="https://www.baidu.com", timeout=5)
if detector.status_code == 200:
logging.info("Network is available")
return True
else:
logging.info("Network is not available")
return False
except requests.ConnectionError:
logging.info("ConnectionError: False")
return False
except Exception:
return False
@staticmethod
def detect_net(driver):
# noinspection PyBroadException
try:
driver.get("https://tree.buct.edu.cn/index_20.html")
driver.maximize_window()
time.sleep(3) # Wait for the page to load
driver.find_element(By.XPATH, '/html/body/main/section/div[1]/div[7]/button[1]')
logging.info("Have logged in")
return True # Not logged in
except Exception:
logging.warning("Not logged in")
return False