diff --git a/Underlying_Operations.py b/Underlying_Operations.py index e3ec6a0..2eb7195 100644 --- a/Underlying_Operations.py +++ b/Underlying_Operations.py @@ -3087,7 +3087,7 @@ class underlying_operations: time_module.sleep(0.25) self.restore_ime(device_id, prev) - def paste_text_at(self, device_id: str, x: int, y: int, text: str) -> bool: + def paste_text_at(self, device_id: str, x: int, y: int, text: str, ensure_focus: bool = True) -> bool: """最简单的“粘贴到输入框”方法。 流程:点击输入框获取焦点 -> 设置剪贴板 -> 发送 KEYCODE_PASTE(279)。 @@ -3099,23 +3099,24 @@ class underlying_operations: # 先确保输入框获得焦点 try: - try: - print( - f"[Underlying] paste_text_at: clicking to focus device_id={device_id} coord=({x},{y})" - ) - except Exception: - pass - self.click(device_id, int(x), int(y)) - time_module.sleep(0.2) - # 有些输入框第一次点不进,补一次轻点 - try: - print( - f"[Underlying] paste_text_at: clicking again to ensure focus device_id={device_id} coord=({x},{y})" - ) - except Exception: - pass - self.click(device_id, int(x), int(y)) - time_module.sleep(0.15) + if ensure_focus: + try: + print( + f"[Underlying] paste_text_at: clicking to focus device_id={device_id} coord=({x},{y})" + ) + except Exception: + pass + self.click(device_id, int(x), int(y)) + time_module.sleep(0.2) + # 有些输入框第一次点不进,补一次轻点 + try: + print( + f"[Underlying] paste_text_at: clicking again to ensure focus device_id={device_id} coord=({x},{y})" + ) + except Exception: + pass + self.click(device_id, int(x), int(y)) + time_module.sleep(0.15) except Exception: pass diff --git a/douyin/other/douyin_yanghao.py b/douyin/other/douyin_yanghao.py index 79b193d..6e99df9 100644 --- a/douyin/other/douyin_yanghao.py +++ b/douyin/other/douyin_yanghao.py @@ -43,7 +43,9 @@ def _do_comment(analyser) -> None: ix, iy = analyser._scale_from_base(input_x, input_y) ok_input = bool( - analyser.operation.paste_text_at(analyser.device_id, ix, iy, selected_text) + analyser.operation.paste_text_at( + analyser.device_id, ix, iy, selected_text, ensure_focus=False + ) ) print(f"[养号][评论] paste_text_at -> ok={ok_input} scaled_coord=({ix},{iy})") analyser.operation.delay(2.0) @@ -222,6 +224,17 @@ def yanghao_idle(analyser, total_seconds: int) -> None: end_ts = time.time() + int(max(1, total_seconds)) print(f"[养号] 开始养号模式,总时长 {total_seconds}s") + # 连续未检测到任何模板时的自救阈值(超过此次数尝试按返回键一次) + consecutive_miss = 0 + miss_recover_threshold = int( + getattr(analyser, "yanghao_miss_recover_threshold", 6) or 6 + ) + # 周期性桌面检测(若返回桌面则尝试自救恢复到直播间) + last_desktop_check_ts = 0.0 + desktop_check_interval = float( + getattr(analyser, "yanghao_desktop_check_interval", 5.0) or 5.0 + ) + while time.time() < end_ts: try: # 切换到下一个直播间 @@ -262,10 +275,74 @@ def yanghao_idle(analyser, total_seconds: int) -> None: # 检查是否检测到爱心或收藏任意一个 action_detected = like_ok or fav_ok + + # 自救:若连续多次未检测到任何模板,则尝试按一次返回键脱离卡死状态 + try: + if not action_detected: + consecutive_miss += 1 + print(f"[养号] 连续未检测到模板次数={consecutive_miss}/{miss_recover_threshold}") + else: + consecutive_miss = 0 + + if consecutive_miss >= miss_recover_threshold: + print( + "[养号] 连续多次未检测到模板,尝试按一次返回键进行自救" + ) + try: + analyser.operation.keyevent(analyser.device_id, 4) + analyser.operation.delay(1.0) + except Exception as e: + print(f"[养号] 自救按返回键失败: {e}") + consecutive_miss = 0 + except Exception: + # 任何自救逻辑异常都不应影响主循环 + consecutive_miss = 0 # 跟踪是否实际触发了 点爱心/点收藏 动作(只有在对应概率命中并执行点击时为 True) + # 同时周期性检测是否返回桌面并尝试恢复 did_like = False did_fav = False + try: + now = time.time() + if now - last_desktop_check_ts >= desktop_check_interval: + last_desktop_check_ts = now + try: + top_pkg = ( + analyser.operation.get_top_package(analyser.device_id) or "" + ) + is_douyin_pkg = top_pkg and ( + "aweme" in top_pkg + or "douyin" in top_pkg + or "com.ss.android.ugc" in top_pkg + ) + if top_pkg and not is_douyin_pkg: + print(f"[养号] 自救: 检测到不在抖音前台,前台包名={top_pkg}") + try: + desktop_x = int(getattr(analyser, "desktop_icon_x", 153) or 153) + desktop_y = int(getattr(analyser, "desktop_icon_y", 260) or 260) + analyser.operation.click(analyser.device_id, desktop_x, desktop_y) + analyser.operation.delay(2) + + fallback_x = int(getattr(analyser, "yanghao_fallback_live_x", 540) or 540) + fallback_y = int(getattr(analyser, "yanghao_fallback_live_y", 2000) or 2000) + analyser.operation.click(analyser.device_id, fallback_x, fallback_y) + analyser.operation.delay(1.5) + + enter_x = int(getattr(analyser, "yanghao_fallback_enter_live_x", 540) or 540) + enter_y = int(getattr(analyser, "yanghao_fallback_enter_live_y", 1200) or 1200) + analyser.operation.click(analyser.device_id, enter_x, enter_y) + analyser.operation.delay(0.6) + analyser.operation.click(analyser.device_id, enter_x, enter_y) + analyser.operation.delay(2.0) + + print("[养号] 自救: 恢复操作完成") + except Exception as e: + print(f"[养号] 自救: 恢复操作失败: {e}") + except Exception as e: + print(f"[养号] 自救: 桌面检测异常: {e}") + except Exception: + pass + # 根据概率点击爱心 if ( like_ok diff --git a/gui/gui_app.py b/gui/gui_app.py index 37391fe..7d2ce81 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -1077,7 +1077,78 @@ def main(): width=8, ).pack(side=LEFT) - # 标签页4:养号设置 + # 新增:映射表标签页(用于查看运行时/持久化的 room_pos -> title 映射) + mapping_tab = ttk.Frame(left_notebook) + left_notebook.add(mapping_tab, text="映射表") + + map_box = ttk.LabelFrame(mapping_tab, text="运行时映射表(room_pos -> (title, title_raw))") + map_box.pack(fill=X, padx=10, pady=(10, 10)) + + map_ctrl = ttk.Frame(map_box) + map_ctrl.pack(fill=X, padx=10, pady=(8, 6)) + + map_auto_refresh_var = tk.BooleanVar(value=False) + ttk.Checkbutton(map_ctrl, text="自动刷新(5s)", variable=map_auto_refresh_var).pack( + side=LEFT + ) + ttk.Button(map_ctrl, text="刷新映射表", width=12).pack(side=LEFT, padx=(8, 6)) + + # 映射内容显示区 + map_view = ScrolledText(mapping_tab, wrap="none", height=12) + map_view.pack(fill=BOTH, expand=True, padx=10, pady=(6, 10)) + + # 加载映射表的函数(尝试多个可能的路径) + def _load_mapping_to_view(): + map_view.delete("1.0", "end") + possible_paths = [ + os.path.join(os.getcwd(), "douyin", "core", "state", "room_pos_map.json"), + os.path.join(os.getcwd(), "douyin", "core", "state", "reservations.json"), + os.path.join(os.getcwd(), "douyin", "state", "room_pos_map.json"), + ] + found = False + for p in possible_paths: + try: + if os.path.exists(p): + with open(p, "r", encoding="utf-8") as f: + data = f.read() + # 尝试格式化 JSON,否则原样显示 + try: + import json as _json + + parsed = _json.loads(data) + pretty = _json.dumps(parsed, ensure_ascii=False, indent=2) + map_view.insert("end", f"# 从: {p}\n") + map_view.insert("end", pretty) + except Exception: + map_view.insert("end", f"# 从: {p}(非JSON,原始内容)\n") + map_view.insert("end", data) + found = True + break + except Exception as e: + map_view.insert("end", f"读取 {p} 失败: {e}\n") + if not found: + map_view.insert( + "end", + "未找到映射表文件。可在运行端将 '_room_pos_to_title_map' 序列化到 'douyin/core/state/room_pos_map.json',然后点击刷新以查看内容。", + ) + + # 绑定刷新按钮与自动刷新 + btns = map_ctrl.winfo_children() + if btns and isinstance(btns[-1], ttk.Button): + btns[-1].config(command=_load_mapping_to_view) + + def _maybe_auto_refresh(): + if map_auto_refresh_var.get(): + try: + _load_mapping_to_view() + except Exception: + pass + root.after(5000, _maybe_auto_refresh) + + # 启动自动刷新轮询(无害,如果开关关闭不会频繁IO) + root.after(5000, _maybe_auto_refresh) + + # 养号设置标签(后续代码) yanghao_tab = ttk.Frame(left_notebook) left_notebook.add(yanghao_tab, text="养号设置") diff --git a/run_cli.py b/run_cli.py index 9d23160..e4f7fc7 100644 --- a/run_cli.py +++ b/run_cli.py @@ -397,7 +397,9 @@ def main(argv: list[str]) -> int: int(getattr(analyser, "yanghao_comment_input_x", 540)), int(getattr(analyser, "yanghao_comment_input_y", 1900)), ) - ok_input = bool(analyser.operation.paste_text_at(device_id, ix, iy, msg)) + ok_input = bool( + analyser.operation.paste_text_at(device_id, ix, iy, msg, ensure_focus=False) + ) print("[TEST-COMMENT] input {}".format("OK" if ok_input else "FAIL")) analyser.operation.delay(1) analyser._save_step_screenshot("test_comment_after_input")