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

Compare commits

...

4 Commits

5 changed files with 10 additions and 104 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
-6
View File
@@ -44,19 +44,13 @@ class LibLogout(LibOperator):
if self.__driver is None:
self._showTrace("未提供有效 WebDriver 实例 !")
return False
try:
self.__driver.find_element(
By.XPATH, "//a[@href='/logout']"
).click()
self._showTrace(f"用户 {username} 注销成功 !")
return True
except Exception as e:
self._showTrace(f"用户 {username} 注销失败 ! : {e}")
return False
-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
-6
View File
@@ -127,10 +127,6 @@ class ALMainWindow(QMainWindow, Ui_ALMainWindow):
self.setupUi(self)
self.__input_queue = queue.Queue()
self.__output_queue = queue.Queue()
self.__auto_lib = AutoLib(
self.__input_queue,
self.__output_queue,
)
self.__config_paths = {
"system":
f"{QDir.toNativeSeparators(QFileInfo(sys.executable).absoluteDir().absoluteFilePath("system.json"))}",
@@ -172,8 +168,6 @@ class ALMainWindow(QMainWindow, Ui_ALMainWindow):
if self.__timer and self.__timer.isActive():
self.__timer.stop()
if self.__auto_lib:
self.__auto_lib.close()
if self.__alConfigWidget:
self.__alConfigWidget.close()
super().closeEvent(event)
+7
View File
@@ -0,0 +1,7 @@
Copyright 2025 KenanZhu
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.