Files
douyin_guaji-main/douyin/fudai/douyin_fudai_flow.py
T

214 lines
8.7 KiB
Python

from __future__ import annotations
"""
douyin_fudai_flow 代理模块(WebGUI 根目录)
=========================================
完整的流程辅助函数实现位于:
环境变量 EXTERNAL_DOUYIN_FUDAI_DIR 指向的目录下的 douyin_fudai_flow.py
本模块的作用:
1. 动态加载上述外部模块;
2. 将其中的公开符号(不以下划线开头的名称)导出到当前命名空间;
3. 确保 `stuck_guard_tick` 一定存在(若外部缺失则提供一个安全的空实现)。
这样,当 `axf/douyin_fudai.py` 执行 `import douyin_fudai_flow` 时,
会优先命中本目录的代理模块,避免由于外部模块导入不完整导致
`AttributeError: module 'douyin_fudai_flow' has no attribute 'stuck_guard_tick'`。
"""
import importlib.util
import os
import sys
from types import ModuleType
from typing import Any, Callable
_EXTERNAL_DIR = os.environ.get("EXTERNAL_DOUYIN_FUDAI_DIR", "").strip()
_EXTERNAL_PATH = (
os.path.join(_EXTERNAL_DIR, "douyin_fudai_flow.py") if _EXTERNAL_DIR else ""
)
def _load_external_flow_module() -> ModuleType:
if not _EXTERNAL_PATH or not os.path.isfile(_EXTERNAL_PATH):
# 如果外部文件不存在,创建一个空的模块
import types
empty_module = types.ModuleType("douyin_fudai_flow_external")
empty_module.__file__ = _EXTERNAL_PATH
empty_module.__doc__ = "Empty fallback module - external douyin_fudai_flow.py not found"
empty_module.__dict__["_is_empty_fallback"] = True
return empty_module
mod_name = "douyin_fudai_flow_external"
if mod_name in sys.modules:
return sys.modules[mod_name]
spec = importlib.util.spec_from_file_location(mod_name, _EXTERNAL_PATH)
if spec is None or spec.loader is None:
raise ImportError(f"无法创建外部模块 spec: {_EXTERNAL_PATH}")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore[arg-type]
sys.modules[mod_name] = module
return module
_ext_mod = _load_external_flow_module()
# 先将外部模块中所有公开符号导出到当前命名空间
# 只有当外部模块不是空模块时才导出符号
if not getattr(_ext_mod, "_is_empty_fallback", False):
# 外部文件存在且已加载,导出其符号
for _name, _value in list(vars(_ext_mod).items()): # type: ignore[arg-type]
if _name.startswith("_"):
continue
globals()[_name] = _value # type: ignore[assignment]
else:
# 外部文件不存在,创建一个基本的 FudaiFlow 类作为兜底
class FudaiFlow:
"""兜底的 FudaiFlow 类实现"""
@staticmethod
def handle_live_room_has_or_not_fudai(analyser):
"""兜底实现:执行基本的福袋查找和处理逻辑"""
try:
analyser._log_kv("流程", "FudaiFlow.handle_live_room_has_or_not_fudai:使用兜底实现")
# 执行基本的福袋查找逻辑
# 1. 检查是否已经在福袋详情页
if analyser.check_in_fudai_detail():
analyser._log_kv("流程", "已在福袋详情页,调用详情处理")
# 尝试进入详情并参与
self_detail = getattr(analyser, "_handle_in_fudai_detail", None)
if callable(self_detail):
# 防抖:避免在极短时间内重复触发进入福袋详情的兜底逻辑
try:
import time as _time
now = _time.time()
last_entry = getattr(analyser, "_last_fudai_detail_entry_ts", 0.0)
if now - last_entry < 0.5:
analyser._log_kv("流程", "兜底进入福袋详情重入跳过(短时间重复)")
else:
analyser._last_fudai_detail_entry_ts = now
# 重试次数控制,防止无限循环
retry_count = getattr(analyser, "_fudai_detail_retry_count", 0) + 1
setattr(analyser, "_fudai_detail_retry_count", retry_count)
if retry_count > 5:
analyser._log_kv("流程", "兜底进入福袋详情重试次数超过上限,放弃本次尝试")
else:
# 调用方法,不需要额外参数,因为 self_detail 已经是 analyser 对象的方法
self_detail()
except Exception:
# 兜底路径异常时也不阻塞主流程
try:
analyser._log_kv("流程", "兜底进入福袋详情调用异常,跳过重试")
except Exception:
pass
return
# 2. 查找福袋图标并尝试打开
analyser._log_kv("流程", "查找福袋图标")
try:
import douyin.core.douyin_flow_control as douyin_flow_control
room_pos_ref = [int(getattr(analyser, "room_pos", 0) or 0)]
opened = douyin_flow_control.find_and_open_fudai_detail(analyser, room_pos_ref)
if opened:
analyser._log_kv("流程", "成功找到并打开福袋详情")
analyser._handle_in_fudai_detail()
return
else:
analyser._log_kv("流程", "未找到福袋")
except Exception as e:
analyser._log_kv("流程", f"查找福袋失败: {e}")
# 3. 如果没有找到福袋,切换到下一个直播间
analyser._log_kv("流程", "未找到福袋,准备切换到下一个直播间")
try:
analyser.swipe_up_to_next_live()
analyser._log_kv("流程", "已切换到下一个直播间")
except Exception as e:
analyser._log_kv("流程", f"切换直播间失败: {e}")
except Exception as e:
try:
analyser._log_kv("错误", f"FudaiFlow 兜底实现出错: {e}")
except Exception:
pass
@staticmethod
def handle_in_fudai_detail(analyser):
"""在福袋详情页执行参与流程的兜底实现"""
try:
# 限制日志输出频率,避免频繁重复输出
import time
current_time = time.time()
last_log_time = getattr(analyser, "_last_fudai_detail_log_time", 0)
if current_time - last_log_time >= 1.0: # 至少间隔1秒才输出日志
analyser._log_kv("流程", "兜底进入福袋详情并尝试参与")
analyser._last_fudai_detail_log_time = current_time
if getattr(analyser, "_handle_in_fudai_detail", None):
analyser._handle_in_fudai_detail() # 调用现有的详情处理流程
else:
analyser._log_kv("警告", "_handle_in_fudai_detail 未实现,跳过参与")
return
except Exception as e:
try:
analyser._log_kv("错误", f"handle_in_fudai_detail 兜底失败: {e}")
except Exception:
pass
# 导出兜底类
globals()['FudaiFlow'] = FudaiFlow
def _noop_stuck_guard_tick(
analyser: Any,
stuck_last_hash_ref: list,
stuck_same_since_ref: list,
stuck_last_check_ts_ref: list,
stuck_last_back_ts_ref: list,
read_screen_hash_callback: Callable[[], str],
) -> None:
"""安全的空实现:不做任何操作,仅用于兜底避免 AttributeError。"""
try:
analyser._log_kv("自救", "stuck_guard_tick:当前未启用卡死检测")
except Exception:
pass
# 如果外部模块没有正确导出 stuck_guard_tick,则使用本地兜底实现
if "stuck_guard_tick" not in globals():
stuck_guard_tick = _noop_stuck_guard_tick # type: ignore[assignment]
# 清理局部变量,避免泄漏实现细节
# 注意:_name 和 _value 在 for 循环中定义,循环结束后会自动清理
# 但为了明确清理,我们显式删除
# 使用 exec 来避免 pyright 的未绑定变量检查
try:
exec("del _name")
except NameError:
pass
try:
exec("del _value")
except NameError:
pass
try:
del _ext_mod
except NameError:
pass
try:
del _load_external_flow_module
except NameError:
pass
try:
del _EXTERNAL_DIR
except NameError:
pass
try:
del _EXTERNAL_PATH
except NameError:
pass