mirror of
https://github.com/KenanZhu/AutoLibrary.git
synced 2026-06-18 15:33:03 +08:00
fix(LibTimeSelector) style(LibReserve): 修复时间转换方法 _timeToMins 并重命名为 _timeStrToMins
- 之前的实现未严格限制传入参数为整形,导致在转换时间字符串时可能出现类型错误。 - 重命名为 _timeStrToMins 以明确表示该方法仅用于时间字符串转换。并更新相关调用。 - 重命名 __selectSeatTime 中的冗长局部变量,便于理解和维护。 - 删除多余的时间格式转换嗲用
This commit is contained in:
+16
-10
@@ -9,6 +9,8 @@ See the LICENSE file for details.
|
|||||||
"""
|
"""
|
||||||
import queue
|
import queue
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from base.LibOperator import LibOperator
|
from base.LibOperator import LibOperator
|
||||||
|
|
||||||
|
|
||||||
@@ -29,25 +31,33 @@ class LibTimeSelector(LibOperator):
|
|||||||
super().__init__(input_queue, output_queue)
|
super().__init__(input_queue, output_queue)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _timeToMins(
|
def _timeStrToMins(
|
||||||
time_str: str
|
time_str: str
|
||||||
) -> int:
|
) -> int:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Convert time string "HH:MM" to minutes since midnight.
|
Convert time string "HH:MM" to minutes since midnight.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
"10:00" -> 600
|
||||||
|
"13:30" -> 810
|
||||||
"""
|
"""
|
||||||
hour, minute = map(int, time_str.split(":"))
|
hour, minute = map(int, time_str.split(":"))
|
||||||
return hour*60 + minute
|
return hour*60 + minute
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _minsToTime(
|
def _minsToTimeStr(
|
||||||
mins: int
|
mins: int
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Convert minutes since midnight to time string "HH:MM".
|
Convert minutes since midnight to time string "HH:MM".
|
||||||
|
|
||||||
|
Example:
|
||||||
|
600 -> "10:00"
|
||||||
|
810 -> "13:30"
|
||||||
"""
|
"""
|
||||||
hour, minute = divmod(mins, 60)
|
hour, minute = divmod(int(mins), 60)
|
||||||
return f"{hour:02d}:{minute:02d}"
|
return f"{hour:02d}:{minute:02d}"
|
||||||
|
|
||||||
|
|
||||||
@@ -99,11 +109,11 @@ class LibTimeSelector(LibOperator):
|
|||||||
for time_opt in time_options:
|
for time_opt in time_options:
|
||||||
# Parse time value based on context
|
# Parse time value based on context
|
||||||
if is_reserve:
|
if is_reserve:
|
||||||
|
# Reservation context: parse 'time' attribute
|
||||||
time_attr = time_opt.get_attribute("time")
|
time_attr = time_opt.get_attribute("time")
|
||||||
if time_attr == "now":
|
if time_attr == "now":
|
||||||
from datetime import datetime
|
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
time_val = now.hour * 60 + now.minute
|
time_val = now.hour*60 + now.minute
|
||||||
elif time_attr and time_attr.isdigit():
|
elif time_attr and time_attr.isdigit():
|
||||||
time_val = int(time_attr)
|
time_val = int(time_attr)
|
||||||
else:
|
else:
|
||||||
@@ -114,9 +124,7 @@ class LibTimeSelector(LibOperator):
|
|||||||
if not (time_attr and time_attr.isdigit()):
|
if not (time_attr and time_attr.isdigit()):
|
||||||
continue
|
continue
|
||||||
time_val = int(time_attr)
|
time_val = int(time_attr)
|
||||||
|
free_times.append(time_opt.text.strip() if not is_reserve else self._minsToTimeStr(time_val))
|
||||||
free_times.append(time_opt.text.strip() if not is_reserve else self._minsToTime(time_val))
|
|
||||||
|
|
||||||
actual_diff = time_val - target_time
|
actual_diff = time_val - target_time
|
||||||
abs_diff = abs(actual_diff)
|
abs_diff = abs(actual_diff)
|
||||||
|
|
||||||
@@ -125,11 +133,9 @@ class LibTimeSelector(LibOperator):
|
|||||||
(abs_diff == best_time_diff and
|
(abs_diff == best_time_diff and
|
||||||
((prefer_earlier and actual_diff <= 0) or
|
((prefer_earlier and actual_diff <= 0) or
|
||||||
(not prefer_earlier and actual_diff >= 0)))):
|
(not prefer_earlier and actual_diff >= 0)))):
|
||||||
|
|
||||||
best_time_diff = abs_diff
|
best_time_diff = abs_diff
|
||||||
best_actual_diff = actual_diff
|
best_actual_diff = actual_diff
|
||||||
best_time_opt = time_opt
|
best_time_opt = time_opt
|
||||||
|
|
||||||
if best_time_opt is not None:
|
if best_time_opt is not None:
|
||||||
return (best_time_opt, best_time_opt.text.strip(), best_actual_diff, free_times)
|
return (best_time_opt, best_time_opt.text.strip(), best_actual_diff, free_times)
|
||||||
return (None, None, None, free_times)
|
return (None, None, None, free_times)
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ class LibRenew(LibTimeSelector):
|
|||||||
renew_info = reserve_info["renew_time"]
|
renew_info = reserve_info["renew_time"]
|
||||||
max_diff = renew_info["max_diff"]
|
max_diff = renew_info["max_diff"]
|
||||||
prefer_earlier = renew_info["prefer_early"]
|
prefer_earlier = renew_info["prefer_early"]
|
||||||
target_renew_mins = self._timeToMins(end_time) + renew_info["expect_duration"]*60
|
target_renew_mins = self._timeStrToMins(end_time) + renew_info["expect_duration"]*60
|
||||||
|
|
||||||
# Validate and adjust target renew time to library closing time
|
# Validate and adjust target renew time to library closing time
|
||||||
if not self.__validateAndAdjustRenewTime(end_time, target_renew_mins):
|
if not self.__validateAndAdjustRenewTime(end_time, target_renew_mins):
|
||||||
@@ -127,12 +127,12 @@ class LibRenew(LibTimeSelector):
|
|||||||
"""
|
"""
|
||||||
LIBRARY_CLOSE_TIME = 1410 # 23:30 in minutes
|
LIBRARY_CLOSE_TIME = 1410 # 23:30 in minutes
|
||||||
if target_renew_mins > LIBRARY_CLOSE_TIME:
|
if target_renew_mins > LIBRARY_CLOSE_TIME:
|
||||||
actual_renew_duration = LIBRARY_CLOSE_TIME - self._timeToMins(end_time)
|
actual_renew_duration = LIBRARY_CLOSE_TIME - self._timeStrToMins(end_time)
|
||||||
if actual_renew_duration <= 0:
|
if actual_renew_duration <= 0:
|
||||||
self._showTrace(f"当前结束时间 {end_time} 已接近闭馆时间,无法续约 !")
|
self._showTrace(f"当前结束时间 {end_time} 已接近闭馆时间,无法续约 !")
|
||||||
return False
|
return False
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
f"续约时间已调整至闭馆时间 {self._minsToTime(LIBRARY_CLOSE_TIME)},"
|
f"续约时间已调整至闭馆时间 {self._minsToTimeStr(LIBRARY_CLOSE_TIME)},"
|
||||||
f"实际续约时长为 {actual_renew_duration//60} 小时 {actual_renew_duration%60} 分钟"
|
f"实际续约时长为 {actual_renew_duration//60} 小时 {actual_renew_duration%60} 分钟"
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|||||||
+37
-32
@@ -190,10 +190,10 @@ class LibReserve(LibTimeSelector):
|
|||||||
if reserve_info.get("end_time") is None:
|
if reserve_info.get("end_time") is None:
|
||||||
reserve_info["end_time"] = {}
|
reserve_info["end_time"] = {}
|
||||||
if "time" not in reserve_info["end_time"]:
|
if "time" not in reserve_info["end_time"]:
|
||||||
end_mins = self._timeToMins(reserve_info["begin_time"]["time"])
|
end_mins = self._timeStrToMins(reserve_info["begin_time"]["time"])
|
||||||
end_mins = end_mins + int(reserve_info["expect_duration"]*60)
|
end_mins = end_mins + int(reserve_info["expect_duration"]*60)
|
||||||
reserve_info["end_time"] = {
|
reserve_info["end_time"] = {
|
||||||
"time": self._minsToTime(end_mins),
|
"time": self._minsToTimeStr(end_mins),
|
||||||
"max_diff": 30,
|
"max_diff": 30,
|
||||||
"prefer_early": False
|
"prefer_early": False
|
||||||
}
|
}
|
||||||
@@ -215,8 +215,8 @@ class LibReserve(LibTimeSelector):
|
|||||||
):
|
):
|
||||||
|
|
||||||
begin_time, end_time = reserve_info["begin_time"], reserve_info["end_time"]
|
begin_time, end_time = reserve_info["begin_time"], reserve_info["end_time"]
|
||||||
begin_mins = self._timeToMins(begin_time["time"])
|
begin_mins = self._timeStrToMins(begin_time["time"])
|
||||||
end_mins = self._timeToMins(end_time["time"])
|
end_mins = self._timeStrToMins(end_time["time"])
|
||||||
# if end time is earlier than begin_time, exchange them
|
# if end time is earlier than begin_time, exchange them
|
||||||
if end_mins < begin_mins:
|
if end_mins < begin_mins:
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
@@ -225,15 +225,15 @@ class LibReserve(LibTimeSelector):
|
|||||||
reserve_info["end_time"] = begin_time
|
reserve_info["end_time"] = begin_time
|
||||||
reserve_info["begin_time"] = end_time
|
reserve_info["begin_time"] = end_time
|
||||||
begin_time, end_time = reserve_info["begin_time"], reserve_info["end_time"]
|
begin_time, end_time = reserve_info["begin_time"], reserve_info["end_time"]
|
||||||
begin_mins = self._timeToMins(begin_time["time"])
|
begin_mins = self._timeStrToMins(begin_time["time"])
|
||||||
end_mins = self._timeToMins(end_time["time"])
|
end_mins = self._timeStrToMins(end_time["time"])
|
||||||
# ensure the end time is not later than 23:30
|
# ensure the end time is not later than 23:30
|
||||||
if end_mins > self._timeToMins("23:30"):
|
if end_mins > self._timeStrToMins("23:30"):
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
f"结束时间 {end_time['time']} 晚于 23:30, 自动设置为 23:30"
|
f"结束时间 {end_time['time']} 晚于 23:30, 自动设置为 23:30"
|
||||||
)
|
)
|
||||||
reserve_info["end_time"]["time"] = "23:30"
|
reserve_info["end_time"]["time"] = "23:30"
|
||||||
end_mins = self._timeToMins("23:30")
|
end_mins = self._timeStrToMins("23:30")
|
||||||
# ensure the duration is not longer than 8 hours
|
# ensure the duration is not longer than 8 hours
|
||||||
if reserve_info["satisfy_duration"]:
|
if reserve_info["satisfy_duration"]:
|
||||||
if reserve_info["expect_duration"] > 8:
|
if reserve_info["expect_duration"] > 8:
|
||||||
@@ -250,7 +250,7 @@ class LibReserve(LibTimeSelector):
|
|||||||
f"{float((end_mins - begin_mins)/60)} 小时 "
|
f"{float((end_mins - begin_mins)/60)} 小时 "
|
||||||
f"超出最大时长 8 小时, 自动设置为 8 小时"
|
f"超出最大时长 8 小时, 自动设置为 8 小时"
|
||||||
)
|
)
|
||||||
reserve_info["end_time"]["time"] = self._minsToTime(begin_mins + 8*60)
|
reserve_info["end_time"]["time"] = self._minsToTimeStr(begin_mins + 8*60)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -481,6 +481,9 @@ class LibReserve(LibTimeSelector):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
Select the nearest available time option.
|
Select the nearest available time option.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
int: The actual selected time value in minutes.
|
||||||
"""
|
"""
|
||||||
# Wait for time options to load
|
# Wait for time options to load
|
||||||
try:
|
try:
|
||||||
@@ -515,7 +518,7 @@ class LibReserve(LibTimeSelector):
|
|||||||
)
|
)
|
||||||
return target_time
|
return target_time
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
f"无法选择最近的 {time_type} {self._minsToTime(target_time)}, "
|
f"无法选择最近的 {time_type} {self._minsToTimeStr(target_time)}, "
|
||||||
f"所有可选时间与目标时间相差都超过 {max_time_diff} 分钟"
|
f"所有可选时间与目标时间相差都超过 {max_time_diff} 分钟"
|
||||||
)
|
)
|
||||||
self._showTrace(f"当前可供预约的 {time_type} 有: {free_times}")
|
self._showTrace(f"当前可供预约的 {time_type} 有: {free_times}")
|
||||||
@@ -530,47 +533,49 @@ class LibReserve(LibTimeSelector):
|
|||||||
satisfy_duration: bool = True
|
satisfy_duration: bool = True
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
|
||||||
"""Select seat begin and end time."""
|
"""
|
||||||
expect_begin_time = actual_begin_time = begin_time["time"]
|
Select seat begin and end time.
|
||||||
expect_end_time = actual_end_time = end_time["time"]
|
"""
|
||||||
expect_begin_mins = self._timeToMins(expect_begin_time)
|
exp_beg_tm_str = act_beg_tm_str = begin_time["time"]
|
||||||
actual_begin_mins = expect_begin_mins
|
exp_end_tm_str = act_end_tm_str = end_time["time"]
|
||||||
expect_end_mins = self._timeToMins(expect_end_time)
|
exp_beg_mins = self._timeStrToMins(exp_beg_tm_str)
|
||||||
|
act_beg_mins = exp_beg_mins
|
||||||
|
exp_end_mins = self._timeStrToMins(exp_end_tm_str)
|
||||||
|
act_end_mins = exp_end_mins
|
||||||
|
|
||||||
# Select begin time
|
# Select begin time
|
||||||
if self.__selectNearestTime(
|
if act_beg_mins := self.__selectNearestTime(
|
||||||
time_id="startTime",
|
time_id="startTime",
|
||||||
time_type="开始时间",
|
time_type="开始时间",
|
||||||
target_time=expect_begin_mins,
|
target_time=exp_beg_mins,
|
||||||
max_time_diff=begin_time["max_diff"],
|
max_time_diff=begin_time["max_diff"],
|
||||||
prefer_earlier=begin_time["prefer_early"]
|
prefer_earlier=begin_time["prefer_early"]
|
||||||
) == -1:
|
) and act_beg_mins == -1:
|
||||||
return False
|
return False
|
||||||
actual_begin_time = self._minsToTime(expect_begin_mins)
|
act_beg_tm_str = self._minsToTimeStr(act_beg_mins)
|
||||||
actual_begin_mins = self._timeToMins(actual_begin_time)
|
|
||||||
|
|
||||||
# If 'satisfy_duration' is True, select end time based on actual begin time
|
# If 'satisfy_duration' is True, select end time based on actual begin time
|
||||||
if satisfy_duration:
|
if satisfy_duration:
|
||||||
expect_end_mins = self.validateAndAdjustEndTime(actual_begin_mins, expct_duration)
|
exp_end_mins = int(self.validateAndAdjustEndTime(act_beg_mins, expct_duration))
|
||||||
expect_end_time = self._minsToTime(expect_end_mins)
|
exp_end_tm_str = self._minsToTimeStr(exp_end_mins)
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
f"需要满足期望预约持续时间: {expct_duration} 小时, "
|
f"需要满足期望预约持续时间: {expct_duration} 小时, "
|
||||||
f"根据开始时间 {actual_begin_time} 计算结束时间: {expect_end_time}"
|
f"根据开始时间 {act_beg_tm_str} 计算结束时间: {exp_end_tm_str}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Select end time
|
# Select end time
|
||||||
if self.__selectNearestTime(
|
if act_end_mins := self.__selectNearestTime(
|
||||||
time_id="endTime",
|
time_id="endTime",
|
||||||
time_type="结束时间",
|
time_type="结束时间",
|
||||||
target_time=expect_end_mins,
|
target_time=exp_end_mins,
|
||||||
max_time_diff=end_time["max_diff"],
|
max_time_diff=end_time["max_diff"],
|
||||||
prefer_earlier=end_time["prefer_early"]
|
prefer_earlier=end_time["prefer_early"]
|
||||||
) == -1:
|
) and act_end_mins == -1:
|
||||||
return False
|
return False
|
||||||
actual_end_time = self._minsToTime(expect_end_mins)
|
act_end_tm_str = self._minsToTimeStr(act_end_mins)
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
f"期望预约时间段: {expect_begin_time} - {expect_end_time}, "
|
f"期望预约时间段: {exp_beg_tm_str} - {exp_end_tm_str}, "
|
||||||
f"实际预约时间段: {actual_begin_time} - {actual_end_time}"
|
f"实际预约时间段: {act_beg_tm_str} - {act_end_tm_str}"
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -584,8 +589,8 @@ class LibReserve(LibTimeSelector):
|
|||||||
"""
|
"""
|
||||||
Validate and adjust reserve end time to library closing time if needed.
|
Validate and adjust reserve end time to library closing time if needed.
|
||||||
"""
|
"""
|
||||||
LIBRARY_CLOSE_TIME = self._timeToMins("23:30")
|
LIBRARY_CLOSE_TIME = self._timeStrToMins("23:30")
|
||||||
expect_end_mins = begin_mins + duration * 60
|
expect_end_mins = int(begin_mins + duration*60)
|
||||||
if expect_end_mins > LIBRARY_CLOSE_TIME:
|
if expect_end_mins > LIBRARY_CLOSE_TIME:
|
||||||
expect_end_mins = LIBRARY_CLOSE_TIME
|
expect_end_mins = LIBRARY_CLOSE_TIME
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
|
|||||||
Reference in New Issue
Block a user