1
1
mirror of https://github.com/KenanZhu/AutoLibrary.git synced 2026-06-17 23:13:03 +08:00

chore(AutoLib, LibReserve): remove the 'canReserve' method in LibReserve and replace it with 'canReserve' of LibChecker in AutoLib

This commit is contained in:
2025-11-07 23:05:09 +08:00
parent f13d983124
commit dac87068ef
2 changed files with 3 additions and 92 deletions
+3 -1
View File
@@ -17,6 +17,7 @@ from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.edge.service import Service
from MsgBase import MsgBase
from LibChecker import LibChecker
from LibLogin import LibLogin
from LibLogout import LibLogout
from LibReserve import LibReserve
@@ -96,6 +97,7 @@ class AutoLib(MsgBase):
self._showTrace(f"浏览器驱动初始化失败: {e}")
return False
# init library operators
self.__lib_checker = LibChecker(self._input_queue, self._output_queue, self.__driver)
self.__lib_login = LibLogin(self._input_queue, self._output_queue, self.__driver)
self.__lib_logout = LibLogout(self._input_queue, self._output_queue, self.__driver)
self.__lib_reserve = LibReserve(self._input_queue, self._output_queue, self.__driver)
@@ -169,7 +171,7 @@ class AutoLib(MsgBase):
}
# reserve
if run_mode["auto_reserve"]:
if self.__lib_reserve.canReserve(reserve_info.get("date")):
if self.__lib_checker.canReserve(reserve_info.get("date")):
if self.__lib_reserve.reserve(reserve_info):
self._showTrace(f"用户 {username} 预约成功 !")
result = 0
-91
View File
@@ -489,97 +489,6 @@ class LibReserve(LibOperator):
return True
def canReserve(
self,
date: str
) -> bool:
if date is None:
self._showTrace("日期未指定, 无法检查预约状态")
return True
else:
self._showTrace(f"正在检查用户在日期 {date} 是否可预约......")
date_obj = datetime.strptime(date, "%Y-%m-%d").date()
try:
# we need to navigate to the history page to check if we can reserve
WebDriverWait(self.__driver, 5).until(
EC.element_to_be_clickable((By.XPATH, "//a[@href='/history?type=SEAT']"))
).click()
WebDriverWait(self.__driver, 2).until(
EC.presence_of_element_located((By.CLASS_NAME, "myReserveList"))
)
except:
self._showTrace("加载预约记录页面失败 !")
return False
checked_count = 0
max_attemots = 3 # we only check (3*4=)12 reservations
for _ in range(max_attemots):
try:
# check if there's any reservation on the date
reservations = self.__driver.find_elements(
By.CSS_SELECTOR, ".myReserveList dl"
)
except:
self._showTrace("加载预约记录失败 !")
return False
for i in range(checked_count, len(reservations) - 1): # the last one is load button
reservation = reservations[i]
try:
time_element = reservation.find_element(
By.CSS_SELECTOR, "dt"
)
status_elements = reservation.find_elements(
By.CSS_SELECTOR, "a"
)
is_reserved = any("已预约" in status.text for status in status_elements)
# process time element to get the date string
time_str = time_element.text.strip()
today = datetime.now().date()
if "明天" in time_str:
target_date = today + timedelta(days=1)
date_str = target_date.strftime("%Y-%m-%d")
elif "今天" in time_str:
target_date = today
date_str = target_date.strftime("%Y-%m-%d")
elif "昨天" in time_str:
target_date = today - timedelta(days=1)
date_str = target_date.strftime("%Y-%m-%d")
else:
date_match = re.search(r'(\d{4}-\d{1,2}-\d{1,2})', time_str)
if date_match:
date_str = date_match.group(1)
else:
continue
# reservation is earlier than the given date, can reserve
if datetime.strptime(date_str, "%Y-%m-%d").date() < date_obj:
self._showTrace(f"用户在 {date} 可预约")
return True
# reservation is later than the given date, check the next one
elif datetime.strptime(date_str, "%Y-%m-%d").date() > date_obj:
continue
# compare with the given date
if date_str == date and is_reserved:
self._showTrace(f"用户在 {date} 已存在有效预约, 无法预约")
return False
except:
self._showTrace(f"解析第 {i + 1} 条预约记录时发生未知错误 !")
continue
checked_count = len(reservations) - 1
# load new reservations if still not sure
try:
more_btn = self.__driver.find_element(By.ID, "moreBtn")
if more_btn.is_displayed() and more_btn.is_enabled():
self.__driver.execute_script("arguments[0].scrollIntoView(true);", more_btn)
self.__driver.execute_script("arguments[0].click();", more_btn)
else:
break
except:
break
self._showTrace(f"用户在 {date} 可预约")
return True
def reserve(
self,
reserve_info: dict