Files
douyin_guaji-main/douyin/core/douyin_main_flow.py
T
Your Name 0b5f150691 Refactor and clean up code across multiple scripts
- Removed trailing whitespace and ensured files end with a single newline in cleanup_unused_files.py.
- Added fix_trailing_whitespace.py to batch remove trailing whitespace from Python files.
- Updated install_dependencies.py to improve dependency installation prompts.
- Cleaned up pick_coords.py by removing unnecessary whitespace and improving code readability.
- Enhanced region_selector.py by disabling specific pylint checks for better compatibility with third-party libraries.
- Improved WPS流程图生成工具.py by cleaning up whitespace and enhancing readability.
- Updated __init__.py in typings to remove unnecessary lines.
- Modified app.js to adjust default configuration values and clean up unused code.
2026-01-02 23:57:03 +08:00

135 lines
4.8 KiB
Python

from __future__ import annotations
"""
主流程控制模块
==============
职责:核心流程控制和调度
- 主循环入口
- 单次循环执行(拆分后简化)
"""
import time
from typing import Any, Optional
from douyin.stage import douyin_stage
import douyin.core.douyin_flow_control as douyin_flow_control
from douyin.reservation import douyin_reservation_logic
from douyin.utils import douyin_helpers
from douyin.stage import douyin_stage_handler
from douyin.reservation import douyin_reservation_handler
from douyin.handlers import douyin_swipe_handler
class MainFlowController:
"""主流程控制器:统一管理主流程"""
@staticmethod # type: ignore[misc]
def fudai_choujiang(
analyser: Any,
device_id: str = "",
y_pianyi: int = 0,
needswitch: bool = False,
wait_minutes: int = 15,
) -> None:
"""兼容原 fudai1.fudai_choujiang 的主入口(当前实现为精简版:仅基础抢福袋)。
参数:
analyser: 分析器对象
device_id: 设备ID
y_pianyi: Y轴偏移
needswitch: 是否允许切换直播间
wait_minutes: 等待分钟数
"""
analyser.device_id = device_id or analyser.device_id
analyser.y_pianyi = int(y_pianyi or 0)
# 保存是否允许切换直播间的配置,供后续所有逻辑统一使用
try:
analyser.needswitch = bool(needswitch)
except Exception:
# 兜底:若赋值失败,则保持原有行为(默认允许切换)
analyser.needswitch = True
try:
rx, ry = analyser.operation.get_device_resolution(analyser.device_id)
analyser.resolution_ratio_x, analyser.resolution_ratio_y = int(
rx or 1080
), int(ry or 2400)
except Exception:
analyser.resolution_ratio_x, analyser.resolution_ratio_y = 1080, 2400
# 暂时禁用流式截图,使用直接截图模式
analyser._log_kv("截图服务", "暂时禁用流式截图,使用直接截图模式")
# 记录运行开始时间
analyser._run_start_ts = time.time()
# 主循环(添加安全限制,防止无限运行)
loop_count = 0
max_loops = 10000 # 最大循环次数,防止无限运行
start_time = time.time()
while True:
loop_count += 1
# 安全检查:防止无限循环
if loop_count > max_loops:
try:
analyser._log_kv( # type: ignore[attr-defined]
"流程", "达到最大循环次数限制,停止运行",
loop_count=loop_count,
max_loops=max_loops,
run_time_seconds=round(time.time() - start_time, 1)
)
except Exception:
pass
break
# 检查运行时间限制(例如24小时)
if time.time() - start_time > 24 * 3600:
try:
analyser._log_kv( # type: ignore[attr-defined]
"流程", "达到最大运行时间限制,停止运行",
run_time_hours=round((time.time() - start_time) / 3600, 1)
)
except Exception:
pass
break
try:
analyser._log_kv( # type: ignore[attr-defined]
"流程", f"主循环第 {loop_count} 次", loop_count=loop_count
)
except Exception:
pass
# 执行单次循环
MainFlowController.run_once_basic(analyser)
# 等待指定时间(如果配置了等待时间)
if wait_minutes > 0:
wait_seconds = wait_minutes * 60
try:
analyser.operation.delay(wait_seconds)
except Exception:
time.sleep(wait_seconds)
else:
# 如果没有配置等待时间,短暂延迟避免CPU占用过高
try:
analyser.operation.delay(1.0)
except Exception:
time.sleep(1.0)
@staticmethod # type: ignore[misc]
def run_once_basic(analyser: Any) -> None:
"""单轮基础抢福袋流程(包含简化版高价值福袋预约回退)。
参数:
analyser: 分析器对象
注意:由于此方法非常复杂(1500+行),暂时保持原实现,
但通过模块化组织,后续可以进一步拆分。
"""
# 由于 run_once_basic 方法过于复杂,暂时保持原实现
# 但通过模块化组织,后续可以进一步拆分
analyser.run_once_basic_original() # type: ignore[attr-defined]