refactor(fudai): 进一步拆 __init__ 到 4 个 helper(基础状态/日志基础设施/诊断超时状态/预约运行时状态)
将 __init__ 中剩余的 4 个独立初始化块抽到 helper: - init_basic_state:device/分辨率/计数器/operation/config_path(约 11 行) - init_log_infra:_run_id / _log_seq / _log_kv 闭包(约 24 行) - init_diag_timeout_state:阶段诊断 + 30分钟超时状态(约 11 行) - init_reservation_runtime_state:预约运行时状态字段(约 22 行) douyin_fudai.py: 775 → 712 行 (-63) fudai_init_defaults.py: 210 → 287 行 douyin_fudai.py 跌破 750 行(4121 → 712,本会话累计削减 3409 行 = -83%) __init__ 函数从 109 → 46 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
df8942e7b8
commit
48d78147b5
2 files changed
+94
-80
No files matched your search
+17
-80
@@ -53,97 +53,36 @@ class fudai_analyse:
|
||||
ENTER_LIVE_FINAL_DELAY = 2.0 # 进入直播间后的最终等待时间(秒)
|
||||
|
||||
def __init__(self) -> None:
|
||||
# 设备与分辨率
|
||||
self.device_id: str = "" # 设备ID,需要在运行时设置
|
||||
self.y_pianyi: int = 0
|
||||
self.resolution_ratio_x: int = 1080
|
||||
self.resolution_ratio_y: int = 2400
|
||||
|
||||
# 滑动次数计数器(用于滑动次数重置功能)
|
||||
self._swipe_times: int = 0
|
||||
|
||||
# 运行配置(只保留与你当前需求直接相关的主流程字段,细节统一放在各子模块内部)
|
||||
self.operation = underlying_operations()
|
||||
self.config_path = "config/config.json"
|
||||
|
||||
# 日志相关字段:完全独立的日志系统(不依赖原始代码)
|
||||
try:
|
||||
from datetime import datetime
|
||||
|
||||
self._run_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
except Exception as e:
|
||||
logging.debug("生成 run_id 失败,回退到时间戳: %s", e)
|
||||
self._run_id = str(int(time.time()))
|
||||
self._log_seq = 0
|
||||
self._log_kv_orig = None
|
||||
# 日志时间戳使用系统时间(不强制替换年份)
|
||||
self._use_real_timestamp = False
|
||||
# 使用增强版本(通过日志包装器)
|
||||
self._log_kv = lambda *args, **kwargs: douyin_logging_wrapper.LoggingWrapper.enhanced_log_kv(
|
||||
self, *args, **kwargs
|
||||
)
|
||||
# 用于跟踪上一个日志标签,以便检测阶段变化并添加分隔符
|
||||
self._last_log_tag = None
|
||||
# 预定义可在运行中动态创建的属性,避免 access-before-definition / attribute-defined-outside-init 警告
|
||||
self._std_logger = None # type: ignore[assignment]
|
||||
|
||||
# 初始化统一日志系统和 traceId
|
||||
from douyin.core.fudai_modules.fudai_init_defaults import (
|
||||
cleanup_old_room_pos_map_files,
|
||||
init_basic_state,
|
||||
init_default_config,
|
||||
init_diag_timeout_state,
|
||||
init_log_infra,
|
||||
init_logging_system,
|
||||
init_reservation_runtime_state,
|
||||
init_runtime_state_flags,
|
||||
)
|
||||
|
||||
# 设备与分辨率 + 滑动计数 + operation + config_path
|
||||
init_basic_state(self)
|
||||
|
||||
# 日志相关字段(_run_id / _log_seq / _log_kv 闭包等)
|
||||
init_log_infra(self)
|
||||
|
||||
# 初始化统一日志系统和 traceId
|
||||
init_logging_system(self)
|
||||
self._forced_reservation_detail = None
|
||||
self._forced_reservation_fudai_x = None
|
||||
|
||||
# 进入直播间后诊断与阶段流转所需的一些状态
|
||||
self._last_diag_stage: str = "INIT"
|
||||
self._last_diag_seen: str = "-"
|
||||
self._last_diag_ts: float = 0.0
|
||||
|
||||
# 30分钟超时相关:记录最后一次成功参与福袋的时间
|
||||
self._last_successful_attend_ts: float = 0.0
|
||||
self._no_attend_timeout_minutes: float = (
|
||||
30.0 # 30分钟没有参与成功则返回推荐页面(可通过config.json配置)
|
||||
)
|
||||
self._run_start_ts: float = (
|
||||
time.time()
|
||||
) # 记录运行开始时间,用于从未成功参与时的超时检查
|
||||
# 预约状态上次写盘时间(初始化以避免 attribute-defined-outside-init 警告)
|
||||
self._reservations_state_last_write_ts: float = 0.0
|
||||
self.last_fudai_x: int = 0 # 上次检测到的福袋图标X坐标(用于优化搜索)
|
||||
# 阶段诊断 + 30 分钟超时相关状态
|
||||
init_diag_timeout_state(self)
|
||||
|
||||
# 与配置相关的核心字段(默认值由 helper 统一初始化,load_config() 会用 config.json 覆盖)
|
||||
from douyin.core.fudai_modules.fudai_init_defaults import init_default_config
|
||||
|
||||
init_default_config(self)
|
||||
|
||||
# —— 预约运行时状态(单次运行内有效) ——
|
||||
# 高价值福袋预约队列
|
||||
self.reservations: list[dict[str, Any]] = []
|
||||
# 标记是否是第一次运行 run_once_basic(用于刷新预约队列)
|
||||
self._first_run_once_basic: bool = True
|
||||
# 当前"直播间位置"计数:上划 +1,下滑 -1(仅在本次连续切换过程中有效)
|
||||
self.room_pos: int = 0
|
||||
# 回退到预约直播间后,在该时间戳前不允许上划离开
|
||||
self.reservation_focus_until_ts: float = 0.0
|
||||
# 当前是否还有"完成某个预约后的后续动作"待执行
|
||||
self.reservation_after_action_pending: bool = False
|
||||
# 达到"无福袋切换阈值"后,是否在处理完预约后回到列表第一个直播间
|
||||
self.reset_to_list_after_reservations: bool = False
|
||||
# 队列满时是否已回退到最早开奖的预约直播间等待
|
||||
self.queue_full_waiting_at_earliest: bool = False
|
||||
|
||||
# 预约队列状态文件(供 WebGUI / 其他进程查看)
|
||||
try:
|
||||
from douyin.core import path_config as _pc
|
||||
self._reservations_state_path: str = _pc.get_reservations_path()
|
||||
except Exception:
|
||||
self._reservations_state_path = os.path.join(
|
||||
os.path.dirname(__file__), "state", "reservations.json"
|
||||
)
|
||||
# 预约运行时状态(单次运行内有效)
|
||||
init_reservation_runtime_state(self)
|
||||
|
||||
# 运行时直播间标题和位置映射表(用于回退校验)
|
||||
# 格式:{room_pos: {"title": str, "title_raw": str, "has_fudai": bool, "fudai_content": str, "fudai_detail": Any}}
|
||||
@@ -152,9 +91,7 @@ class fudai_analyse:
|
||||
cleanup_old_room_pos_map_files(self)
|
||||
|
||||
# 已记录统计的福袋跟踪(避免同一个直播间同一个福袋重复记录统计)
|
||||
self._recorded_fudai_stats: set[tuple[str, str]] = (
|
||||
set()
|
||||
) # (room_title, fudai_content)
|
||||
self._recorded_fudai_stats: set[tuple[str, str]] = set()
|
||||
|
||||
# 运行时状态标志(避免在外部定义属性的警告)
|
||||
init_runtime_state_flags(self)
|
||||
|
||||
@@ -7,6 +7,83 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def init_basic_state(self) -> None:
|
||||
"""初始化基础状态字段:device、分辨率、计数器、operation、config_path。"""
|
||||
from douyin.core.underlying_operations import underlying_operations
|
||||
|
||||
self.device_id: str = ""
|
||||
self.y_pianyi: int = 0
|
||||
self.resolution_ratio_x: int = 1080
|
||||
self.resolution_ratio_y: int = 2400
|
||||
self._swipe_times: int = 0
|
||||
self.operation = underlying_operations()
|
||||
self.config_path = "config/config.json"
|
||||
|
||||
|
||||
def init_log_infra(self) -> None:
|
||||
"""初始化日志相关字段:_run_id、_log_seq、_log_kv 闭包等。"""
|
||||
import logging
|
||||
import time as _time
|
||||
|
||||
try:
|
||||
from datetime import datetime
|
||||
|
||||
self._run_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
except Exception as e:
|
||||
logging.debug("生成 run_id 失败,回退到时间戳: %s", e)
|
||||
self._run_id = str(int(_time.time()))
|
||||
self._log_seq = 0
|
||||
self._log_kv_orig = None
|
||||
self._use_real_timestamp = False
|
||||
|
||||
from douyin.logging import douyin_logging_wrapper
|
||||
|
||||
self._log_kv = lambda *args, **kwargs: douyin_logging_wrapper.LoggingWrapper.enhanced_log_kv(
|
||||
self, *args, **kwargs
|
||||
)
|
||||
self._last_log_tag = None
|
||||
self._std_logger = None # type: ignore[assignment]
|
||||
|
||||
|
||||
def init_diag_timeout_state(self) -> None:
|
||||
"""初始化阶段诊断 + 30 分钟超时相关状态字段。"""
|
||||
import time as _time
|
||||
|
||||
self._last_diag_stage: str = "INIT"
|
||||
self._last_diag_seen: str = "-"
|
||||
self._last_diag_ts: float = 0.0
|
||||
|
||||
self._last_successful_attend_ts: float = 0.0
|
||||
self._no_attend_timeout_minutes: float = 30.0
|
||||
self._run_start_ts: float = _time.time()
|
||||
self._reservations_state_last_write_ts: float = 0.0
|
||||
self.last_fudai_x: int = 0
|
||||
|
||||
|
||||
def init_reservation_runtime_state(self) -> None:
|
||||
"""初始化预约运行时状态字段(单次运行内有效)。"""
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
self.reservations: list[dict[str, Any]] = []
|
||||
self._first_run_once_basic: bool = True
|
||||
self.room_pos: int = 0
|
||||
self.reservation_focus_until_ts: float = 0.0
|
||||
self.reservation_after_action_pending: bool = False
|
||||
self.reset_to_list_after_reservations: bool = False
|
||||
self.queue_full_waiting_at_earliest: bool = False
|
||||
|
||||
# 预约队列状态文件(供 WebGUI / 其他进程查看)
|
||||
try:
|
||||
from douyin.core import path_config as _pc
|
||||
|
||||
self._reservations_state_path: str = _pc.get_reservations_path()
|
||||
except Exception:
|
||||
self._reservations_state_path = os.path.join(
|
||||
os.path.dirname(__file__), "state", "reservations.json"
|
||||
)
|
||||
|
||||
|
||||
def init_logging_system(self) -> None:
|
||||
"""初始化统一日志系统 + traceId(MDC)。
|
||||
|
||||
|
||||
Reference in new issue
Block a user