diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index e9a1759..f2b6044 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -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 diff --git a/douyin/detection/douyin_pixel.py b/douyin/detection/douyin_pixel.py index dc9daa2..d3a7899 100644 --- a/douyin/detection/douyin_pixel.py +++ b/douyin/detection/douyin_pixel.py @@ -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 diff --git a/douyin/other/douyin_high_value_room.py b/douyin/other/douyin_high_value_room.py index 4202146..b7395ff 100644 --- a/douyin/other/douyin_high_value_room.py +++ b/douyin/other/douyin_high_value_room.py @@ -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( diff --git a/douyin/other/douyin_popups.py b/douyin/other/douyin_popups.py index f47db92..f79efea 100644 --- a/douyin/other/douyin_popups.py +++ b/douyin/other/douyin_popups.py @@ -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 diff --git a/douyin/reservation/douyin_reservation_handler.py b/douyin/reservation/douyin_reservation_handler.py index fa02ec7..0d44bd6 100644 --- a/douyin/reservation/douyin_reservation_handler.py +++ b/douyin/reservation/douyin_reservation_handler.py @@ -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 进行回退,靠近目标直播间。 diff --git a/gui/gui_core.py b/gui/gui_core.py index ebb93df..8b253e0 100644 --- a/gui/gui_core.py +++ b/gui/gui_core.py @@ -985,13 +985,25 @@ class Runner: if params.high_value_room_revisit_only_current_run: cmd.append("--high-value-room-revisit-only-current-run") if params.high_value_room_revisit_min_interval_seconds != 300: - cmd += ["--high-value-room-revisit-min-interval-seconds", str(params.high_value_room_revisit_min_interval_seconds)] + cmd += [ + "--high-value-room-revisit-min-interval-seconds", + str(params.high_value_room_revisit_min_interval_seconds), + ] if params.high_value_room_revisit_max_per_run != 5: - cmd += ["--high-value-room-revisit-max-per-run", str(params.high_value_room_revisit_max_per_run)] + cmd += [ + "--high-value-room-revisit-max-per-run", + str(params.high_value_room_revisit_max_per_run), + ] if params.high_value_room_revisit_min_prize_value > 0: - cmd += ["--high-value-room-revisit-min-prize-value", str(params.high_value_room_revisit_min_prize_value)] + cmd += [ + "--high-value-room-revisit-min-prize-value", + str(params.high_value_room_revisit_min_prize_value), + ] if params.high_value_room_revisit_max_age_days != 30: - cmd += ["--high-value-room-revisit-max-age-days", str(params.high_value_room_revisit_max_age_days)] + cmd += [ + "--high-value-room-revisit-max-age-days", + str(params.high_value_room_revisit_max_age_days), + ] # 预约回退校验 if params.reservation_back_verify_enabled: diff --git a/test_shopping_redpacket_detection.py b/test_shopping_redpacket_detection.py index 3117fa5..0c3183a 100644 --- a/test_shopping_redpacket_detection.py +++ b/test_shopping_redpacket_detection.py @@ -12,7 +12,11 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from douyin.detection.douyin_pixel import detect_redpacket_popup_background from douyin.other.douyin_popups import check_shopping_redpacket_popup -from douyin.fudai.core.coords import SHOPPING_REDPACKET_AREA, STAGE_RED_PACKET_OCR, DIAG_SHOPPING_REDPACKET +from douyin.fudai.core.coords import ( + SHOPPING_REDPACKET_AREA, + STAGE_RED_PACKET_OCR, + DIAG_SHOPPING_REDPACKET, +) def test_pixel_detection(): @@ -20,12 +24,12 @@ def test_pixel_detection(): print("=" * 60) print("测试1: 像素检测函数") print("=" * 60) - + # 创建测试图像:模拟购物红包(红色背景) width, height = 1080, 2400 - test_img = Image.new('RGB', (width, height), color=(255, 255, 255)) + test_img = Image.new("RGB", (width, height), color=(255, 255, 255)) pixels = test_img.load() - + # 在中央区域添加红色像素(模拟购物红包) red_color = (230, 80, 80) # 红色 for y in range(400, 1200): @@ -33,12 +37,12 @@ def test_pixel_detection(): # 80%的像素为红色,20%为其他颜色 if (x + y) % 5 != 0: pixels[x, y] = red_color - + result = detect_redpacket_popup_background(test_img) print(f"红色像素检测结果: {result}") print(f"预期: True (因为有超过15%的红色像素)") print() - + return result @@ -47,23 +51,23 @@ def test_coordinates(): print("=" * 60) print("测试2: 坐标定义") print("=" * 60) - + print(f"SHOPPING_REDPACKET_AREA: {SHOPPING_REDPACKET_AREA}") print(f"STAGE_RED_PACKET_OCR: {STAGE_RED_PACKET_OCR}") print(f"DIAG_SHOPPING_REDPACKET: {DIAG_SHOPPING_REDPACKET}") - + # 验证坐标范围 x1, y1, x2, y2 = DIAG_SHOPPING_REDPACKET print(f"\n诊断区域范围:") print(f" X: {x1} - {x2} (宽度: {x2-x1})") print(f" Y: {y1} - {y2} (高度: {y2-y1})") - + # 检查坐标是否有效 if x1 < x2 and y1 < y2: print("✓ 坐标范围有效") else: print("✗ 坐标范围无效") - + print() @@ -72,7 +76,7 @@ def test_ocr_keywords(): print("=" * 60) print("测试3: OCR关键词检查") print("=" * 60) - + # 测试文本 test_texts = [ "邀你搶購物紅包", @@ -88,25 +92,34 @@ def test_ocr_keywords(): "最高领取", "抢", ] - + # 关键词列表(与douyin_popups.py中的保持一致) keywords = [ - '邀你搶購物紅包', '搶購物紅包', '購物紅包', '邀你搶', - '搶紅包', '搶', '最高領取', - '邀你抢', '购物红包', '抢红包', '抢', '最高领取' + "邀你搶購物紅包", + "搶購物紅包", + "購物紅包", + "邀你搶", + "搶紅包", + "搶", + "最高領取", + "邀你抢", + "购物红包", + "抢红包", + "抢", + "最高领取", ] - + print(f"定义的关键词数量: {len(keywords)}") print("关键词列表:") for kw in keywords: print(f" - {kw}") - + print("\n测试文本匹配:") for text in test_texts: matched = any(kw in text for kw in keywords) status = "✓" if matched else "✗" print(f" {status} '{text}'") - + print() @@ -115,27 +128,27 @@ def create_redpacket_test_image(): print("=" * 60) print("创建测试图像") print("=" * 60) - + # 创建1080x2400的图像 - img = Image.new('RGB', (1080, 2400), color=(255, 255, 255)) + img = Image.new("RGB", (1080, 2400), color=(255, 255, 255)) pixels = img.load() - + # 在中央画一个红色矩形(模拟购物红包) red_color = (230, 80, 80) for y in range(400, 1400): for x in range(300, 780): pixels[x, y] = red_color - + # 保存图像 - test_img_path = os.path.join(os.path.dirname(__file__), 'test_redpacket.png') + test_img_path = os.path.join(os.path.dirname(__file__), "test_redpacket.png") img.save(test_img_path) print(f"✓ 测试图像已保存: {test_img_path}") print() - + return test_img_path -if __name__ == '__main__': +if __name__ == "__main__": print("\n") print("╔" + "=" * 58 + "╗") print("║" + " " * 58 + "║") @@ -143,13 +156,13 @@ if __name__ == '__main__': print("║" + " " * 58 + "║") print("╚" + "=" * 58 + "╝") print() - + # 运行所有测试 test_coordinates() test_ocr_keywords() result = test_pixel_detection() test_img_path = create_redpacket_test_image() - + # 总结 print("=" * 60) print("测试总结")