From 982357927e400cf372afce386356af99ad3c0b74 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 5 Jan 2026 00:38:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=9B=9E=E9=80=80?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E9=85=8D=E7=BD=AE=E9=80=89=E9=A1=B9=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=BB=91=E5=8A=A8=E5=92=8C=E6=8C=89=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E9=94=AE=E5=9B=9E=E9=80=80=EF=BC=9B=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=B8=85=E7=90=86=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.json | 2 +- douyin/handlers/douyin_swipe_handler.py | 115 ++++++++++++++---------- douyin/logging/douyin_logging.py | 47 +++++++++- run_cli.py | 11 +++ 4 files changed, 125 insertions(+), 50 deletions(-) diff --git a/config/config.json b/config/config.json index a32d23b..4dea57b 100644 --- a/config/config.json +++ b/config/config.json @@ -186,7 +186,7 @@ "debug_captures": { "enabled": false, "keep_days": 3, - "max_files": 2000, + "max_files": 200, "auto_cleanup": true, "async_save": false, "log_on_complete": false diff --git a/douyin/handlers/douyin_swipe_handler.py b/douyin/handlers/douyin_swipe_handler.py index d1dd60a..a2ea1c2 100644 --- a/douyin/handlers/douyin_swipe_handler.py +++ b/douyin/handlers/douyin_swipe_handler.py @@ -201,64 +201,83 @@ class SwipeHandler: if back_times <= 0: return 0 - # 向上回退:从上往下滑动(与上划切换相反方向) - try: - rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) - ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) - except Exception: - rx, ry = 1080, 2400 + # 根据配置选择回退方式:'swipe'=滑动回退(默认),'back'=按返回键回退 + mode = str(getattr(analyser, "reset_after_swipes_action", "swipe") or "swipe").lower() + if mode not in ("swipe", "back"): + mode = "swipe" - for i in range(back_times): - try: - # 向上回退:从上往下滑动(start_y < end_y) - start_x = 760 * rx // 1080 - start_y = 800 * ry // 2400 # 上方起始位置 - end_x = 760 * rx // 1080 - end_y = 1600 * ry // 2400 # 下方结束位置 - # 直接使用 ADB 内部滑动命令 - # 使用配置的滑动持续时间,如果没有配置则使用300ms(比之前的200ms更快) - swipe_duration_ms = int( - getattr(analyser, "swipe_duration_ms", 300) or 300 - ) + if mode == "back": + # 使用返回键回退(兼容性更好,适用于无法通过滑动稳定回退的设备) + for i in range(back_times): try: - if hasattr(analyser.operation, "_adb"): - adb_cmd = analyser.operation._adb() - else: - import os - - adb_cmd = os.environ.get("ADB_EXECUTABLE", "adb") or "adb" - if not adb_cmd.startswith('"') and not adb_cmd.endswith('"'): - adb_cmd = f'"{adb_cmd}"' - cmd = ( - f"{adb_cmd} -s {analyser.device_id} shell input swipe " - f"{start_x} {start_y} {end_x} {end_y} {swipe_duration_ms}" - ) - subprocess.run( - cmd, - shell=True, - timeout=5.0, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) + analyser.operation.click_back(analyser.device_id) except Exception: - # 回退到 operation.swipe - analyser.operation.swipe( - analyser.device_id, - start_x, - start_y, - end_x, - end_y, - swipe_duration_ms, - ) + pass try: analyser._log_kv( # type: ignore[attr-defined] - "流程", "回退直播间 {}/{}".format(i + 1, back_times) + "流程", "回退直播间 {}/{} (使用返回键)".format(i + 1, back_times) ) except Exception: pass analyser.operation.delay(2) + else: + try: + rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) + ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) except Exception: - pass + rx, ry = 1080, 2400 + + for i in range(back_times): + try: + # 向上回退:从上往下滑动(start_y < end_y) + start_x = 760 * rx // 1080 + start_y = 800 * ry // 2400 # 上方起始位置 + end_x = 760 * rx // 1080 + end_y = 1600 * ry // 2400 # 下方结束位置 + # 直接使用 ADB 内部滑动命令 + # 使用配置的滑动持续时间,如果没有配置则使用300ms(比之前的200ms更快) + swipe_duration_ms = int( + getattr(analyser, "swipe_duration_ms", 300) or 300 + ) + try: + if hasattr(analyser.operation, "_adb"): + adb_cmd = analyser.operation._adb() + else: + import os + + adb_cmd = os.environ.get("ADB_EXECUTABLE", "adb") or "adb" + if not adb_cmd.startswith('"') and not adb_cmd.endswith('"'): + adb_cmd = f'"{adb_cmd}"' + cmd = ( + f"{adb_cmd} -s {analyser.device_id} shell input swipe " + f"{start_x} {start_y} {end_x} {end_y} {swipe_duration_ms}" + ) + subprocess.run( + cmd, + shell=True, + timeout=5.0, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + except Exception: + # 回退到 operation.swipe + analyser.operation.swipe( + analyser.device_id, + start_x, + start_y, + end_x, + end_y, + swipe_duration_ms, + ) + try: + analyser._log_kv( # type: ignore[attr-defined] + "流程", "回退直播间 {}/{}".format(i + 1, back_times) + ) + except Exception: + pass + analyser.operation.delay(2) + except Exception: + pass # 回到第一个后,计数归零 analyser.operation.delay(3) diff --git a/douyin/logging/douyin_logging.py b/douyin/logging/douyin_logging.py index a740774..25dff39 100644 --- a/douyin/logging/douyin_logging.py +++ b/douyin/logging/douyin_logging.py @@ -978,6 +978,9 @@ class LogRotationMonitor: self._thread: Optional[threading.Thread] = None self._last_rotation_count = 0 self._last_check_time = datetime.now() + # 清理状态,避免并发触发大量清理操作 + self._cleanup_lock = threading.Lock() + self._cleanup_running = False def start(self) -> None: """启动监控器""" @@ -1034,7 +1037,11 @@ class LogRotationMonitor: self.alert_callback("WARNING", message) else: print(f"[日志监控] WARNING: {message}") - + # 异步尝试触发清理以释放空间(非阻塞监控线程) + try: + self._schedule_cleanup_async() + except Exception: + pass # 检查轮转频率 current_time = datetime.now() if ( @@ -1057,6 +1064,44 @@ class LogRotationMonitor: if self.alert_callback: self.alert_callback("ERROR", f"日志检查失败: {e}") + def _schedule_cleanup_async(self) -> None: + """如果没有正在运行的清理任务,则在后台线程中启动一次清理。""" + # 快速检查,避免频繁加锁 + if self._cleanup_running: + return + + def _worker(): + try: + with self._cleanup_lock: + self._cleanup_running = True + # 使用模块内的 cleanup_old_logs 执行清理,采用默认策略(保留30天、10GB上限) + try: + cleanup_old_logs( + logs_dir=self.logs_dir, + base_name=self.base_name, + keep_days=30, + max_total_size_gb=10.0, + compress_after_days=7, + compress=True, + ) + except Exception as e: + try: + if self.alert_callback: + self.alert_callback("ERROR", f"日志自动清理失败: {e}") + else: + print(f"[日志监控] 日志自动清理失败: {e}") + except Exception: + pass + finally: + try: + with self._cleanup_lock: + self._cleanup_running = False + except Exception: + self._cleanup_running = False + + t = threading.Thread(target=_worker, daemon=True) + t.start() + def start_log_monitor( logs_dir: str, diff --git a/run_cli.py b/run_cli.py index 7cc9fa3..a5db8e1 100644 --- a/run_cli.py +++ b/run_cli.py @@ -182,6 +182,12 @@ def parse_args(argv: list[str]) -> argparse.Namespace: action="store_true", help="关闭切换次数阈值回退到第一个直播间", ) + p.add_argument( + "--reset-after-swipes-action", + choices=["swipe", "back"], + default=None, + help="回退方式:swipe=滑动回退(默认),back=按返回键回退。", + ) p.add_argument( "--swipe-down-back-count", @@ -471,6 +477,11 @@ def main(argv: list[str]) -> int: analyser.reset_after_swipes_enabled = True if args.reset_after_swipes_disabled: analyser.reset_after_swipes_enabled = False + if getattr(args, "reset_after_swipes_action", None) is not None: + try: + analyser.reset_after_swipes_action = str(args.reset_after_swipes_action or "swipe") + except Exception: + pass # 下滑后返回并重新进入直播间的命令行覆盖(优先级高于 config.json) if getattr(args, "swipe_down_back_count", None) is not None: try: