refactor: 优化代码格式,提升可读性

This commit is contained in:
Your Name committed 2026-01-08 20:39:04 +08:00
1 parent fb0c07c91a
commit 6ecd9fa1ae
7 files changed
+125 -61

No files matched your search

+27 -9
View File
@@ -310,7 +310,9 @@ class fudai_analyse:
self._just_switched_room: bool = False # 是否刚刚切换了直播间
# 高价值房间重访相关状态
self._last_high_value_revisit_ts: float = time.time() # 最后一次重访时间戳,初始化为当前时间避免启动后立即重访
self._last_high_value_revisit_ts: float = (
time.time()
) # 最后一次重访时间戳,初始化为当前时间避免启动后立即重访
self._current_run_revisit_count: int = 0 # 本次运行的重访次数
self._current_run_high_value_rooms: list[dict[str, Any]] = (
[]
@@ -1604,7 +1606,9 @@ class fudai_analyse:
def _seek_reservation_room(item: dict) -> bool:
try:
return bool(self._seek_reservation_room(item, operation_type="预约"))
return bool(
self._seek_reservation_room(item, operation_type="预约")
)
except Exception:
return False
@@ -2046,8 +2050,12 @@ class fudai_analyse:
analyser=self,
revisit_enabled=revisit_enabled,
only_current_run=only_current_run,
min_interval_seconds=getattr(self, "high_value_room_revisit_min_interval_seconds", 300),
max_revisit_per_run=getattr(self, "high_value_room_revisit_max_per_run", 5),
min_interval_seconds=getattr(
self, "high_value_room_revisit_min_interval_seconds", 300
),
max_revisit_per_run=getattr(
self, "high_value_room_revisit_max_per_run", 5
),
)
if should_revisit:
@@ -2055,8 +2063,12 @@ class fudai_analyse:
candidates = douyin_high_value_room.get_revisit_candidates(
analyser=self,
only_current_run=only_current_run,
min_prize_value=getattr(self, "high_value_room_revisit_min_prize_value", 0.0),
max_age_days=getattr(self, "high_value_room_revisit_max_age_days", 30),
min_prize_value=getattr(
self, "high_value_room_revisit_min_prize_value", 0.0
),
max_age_days=getattr(
self, "high_value_room_revisit_max_age_days", 30
),
)
if candidates:
@@ -2522,7 +2534,9 @@ class fudai_analyse:
"""
return douyin_swipe_handler.SwipeHandler.maybe_reset_after_swipes(self)
def _seek_reservation_room(self, item: dict[str, Any], operation_type: str = "预约") -> bool:
def _seek_reservation_room(
self, item: dict[str, Any], operation_type: str = "预约"
) -> bool:
"""根据预约记录里的 pos_seen 与当前 room_pos 进行回退,靠近目标直播间。
说明:
@@ -2842,7 +2856,9 @@ class fudai_analyse:
# 注意:basic_join_decision 内部已经关闭详情并切换直播间,不需要再次关闭
try:
douyin_fudai_core.basic_join_decision(
self, detail, skip_result=(skip, skip_reason)
self,
detail,
skip_result=(skip, skip_reason),
)
except Exception as e:
try:
@@ -3277,7 +3293,9 @@ class fudai_analyse:
elapsed_seconds=round(elapsed, 2),
)
return
join_result = douyin_fudai_core.basic_join_decision(self, detail, skip_result=(False, "already_checked_in_step3"))
join_result = douyin_fudai_core.basic_join_decision(
self, detail, skip_result=(False, "already_checked_in_step3")
)
# 检查是否参与成功:如果 _last_successful_attend_ts 刚刚更新,说明参与成功
attend_after_ts = getattr(self, "_last_successful_attend_ts", 0.0) or 0.0
+13 -13
View File
@@ -174,63 +174,63 @@ def detect_coupon_popup_background(
def detect_redpacket_popup_background(pic_rgb: Image.Image) -> bool:
"""中央购物红包弹窗检测:检测中央大红包区域的红色占比。
购物红包特征:
- 中央大型红色弹窗
- 包含黄色圆形"抢"按钮
- 主要颜色为红色(RGB约 255, 100-150, 100-150)
检测区域(基准1080x2400):大约中央区域
- 左上:(300, 400)
- 右下:(780, 1200)
"""
try:
import numpy as np # type: ignore[import-untyped]
width, height = pic_rgb.size
scale_x = width / 1080.0
scale_y = height / 2400.0
# 购物红包通常出现在屏幕中央
# 基准坐标(1080x2400)
x1 = int(200 * scale_x)
y1 = int(300 * scale_y)
x2 = int(880 * scale_x)
y2 = int(1400 * scale_y)
# 边界检查
x1 = max(0, min(x1, width))
x2 = max(0, min(x2, width))
y1 = max(0, min(y1, height))
y2 = max(0, min(y2, height))
if x1 >= x2 or y1 >= y2:
return False
# 裁剪感兴趣区域
roi = pic_rgb.crop((x1, y1, x2, y2))
arr = np.array(roi)
if arr.size == 0:
return False
# 提取RGB通道
r = arr[:, :, 0] # type: ignore[index]
g = arr[:, :, 1] # type: ignore[index]
b = arr[:, :, 2] # type: ignore[index]
# 红色特征:R > 200, G < 180, B < 180
# 购物红包的红色通常很纯,R值很高
mask = (r >= 220) & (g <= 160) & (b <= 160)
# 计算红色像素占比
total_pixels = mask.size
red_pixels = np.sum(mask) # type: ignore[call-overload]
red_ratio = red_pixels / total_pixels if total_pixels > 0 else 0
# 阈值:至少15%的像素满足红色特征
return red_ratio > 0.15
except Exception:
return False
+3 -1
View File
@@ -362,7 +362,9 @@ def execute_room_revisit(analyser: Any, target_room: Dict[str, Any]) -> bool:
target_pos=pos_seen,
)
success = analyser._seek_reservation_room(target_room, operation_type="高价值房间")
success = analyser._seek_reservation_room(
target_room, operation_type="高价值房间"
)
if success:
analyser._log_kv(
+25 -4
View File
@@ -211,7 +211,7 @@ def check_shopping_redpacket_popup(analyser) -> bool:
pic = pic.convert("RGB")
has_popup_pixel = bool(douyin_pixel.detect_redpacket_popup_background(pic))
pixel_elapsed = time.time() - pixel_check_start
try:
analyser._log_kv(
"購物紅包檢測",
@@ -221,7 +221,7 @@ def check_shopping_redpacket_popup(analyser) -> bool:
)
except Exception:
pass
if not has_popup_pixel:
return False
except (OSError, IOError, SyntaxError):
@@ -304,7 +304,19 @@ def check_shopping_redpacket_popup(analyser) -> bool:
pass
return False
keywords = ("邀你搶購物紅包", "搶購物紅包", "購物紅包", "邀你搶", "搶紅包", "搶", "最高領取", "邀你抢购物红包", "抢购物红包", "购物红包", "邀你抢")
keywords = (
"邀你搶購物紅包",
"搶購物紅包",
"購物紅包",
"邀你搶",
"搶紅包",
"搶",
"最高領取",
"邀你抢购物红包",
"抢购物红包",
"购物红包",
"邀你抢",
)
if any(kw in hay for kw in keywords):
matched = [kw for kw in keywords if kw in hay]
total_elapsed = time.time() - check_start
@@ -380,7 +392,16 @@ def check_shopping_redpacket_popup(analyser) -> bool:
)
combined_text = " ".join(texts).strip()
final_keywords = ("購物紅包", "邀你搶", "搶紅包", "购物红包", "邀你抢", "抢红包", "最高領取", "最高领取")
final_keywords = (
"購物紅包",
"邀你搶",
"搶紅包",
"购物红包",
"邀你抢",
"抢红包",
"最高領取",
"最高领取",
)
result = any(kw in combined_text for kw in final_keywords)
if not result and has_popup_pixel:
result = True
@@ -302,9 +302,7 @@ class ReservationHandler:
@staticmethod # type: ignore[misc]
def seek_reservation_room(
analyser: Any,
item: Dict[str, Any],
operation_type: str = "预约"
analyser: Any, item: Dict[str, Any], operation_type: str = "预约"
) -> bool:
"""根据预约记录里的 pos_seen 与当前 room_pos 进行回退,靠近目标直播间。