Files
douyin_guaji-main/fudai_detail_modules/fudai_detail_operations.py
T

181 lines
5.7 KiB
Python

from __future__ import annotations
"""
福袋详情页操作模块
==================
处理福袋详情页的打开和关闭操作。
"""
from typing import Any
import time
def close_fudai_detail(analyser: Any) -> None:
"""关闭福袋详情:使用点击关闭详情页的坐标,而不是返回键。"""
try:
# 获取分辨率比例(基准分辨率 1080x2400)
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
# 计算关闭详情页的坐标(基准坐标 500, 470)
close_x = int(500 * rx // 1080)
close_y = int(470 * ry // 2400)
# 坐标验证:检查坐标是否在合理范围内
# 获取屏幕尺寸(如果可用)
screen_width = rx
screen_height = ry
try:
if hasattr(analyser, "operation") and hasattr(
analyser.operation, "get_screen_size"
):
try:
screen_size = analyser.operation.get_screen_size(analyser.device_id)
if screen_size:
screen_width, screen_height = screen_size
except Exception:
pass
except Exception:
pass
# 验证坐标边界
if (
close_x < 0
or close_x >= screen_width
or close_y < 0
or close_y >= screen_height
):
try:
analyser._log_kv(
"验证",
"关闭详情页坐标超出屏幕范围",
x=close_x,
y=close_y,
screen_width=screen_width,
screen_height=screen_height,
base_x=500,
base_y=470,
log_type="DEBUG",
level="WARNING",
)
except Exception:
pass
# 修正坐标到屏幕范围内
close_x = max(0, min(close_x, screen_width - 1))
close_y = max(0, min(close_y, screen_height - 1))
analyser._log_kv(
"详情",
"close_fudai_detail:点击关闭详情页坐标",
log_type="OP",
x=close_x,
y=close_y,
base_x=500,
base_y=470,
)
except Exception as e:
# 如果获取分辨率失败,使用默认坐标
try:
analyser._log_kv(
"错误",
"获取分辨率失败,使用默认坐标",
error=str(e),
error_type=type(e).__name__,
log_type="ERR",
level="WARNING",
)
except Exception:
pass
close_x, close_y = 500, 470
try:
analyser._log_kv(
"详情", "close_fudai_detail:使用默认坐标关闭详情页", log_type="OP"
)
except Exception:
pass
try:
analyser.operation.click(analyser.device_id, close_x, close_y)
except Exception as e1:
# 兜底:尝试使用 analyser.click
try:
analyser._log_kv(
"错误",
"使用operation.click关闭详情页失败,尝试备用方法",
error=str(e1),
error_type=type(e1).__name__,
log_type="ERR",
level="WARNING",
)
except Exception:
pass
try:
analyser.click(close_x, close_y)
except Exception as e2:
# 最后兜底:使用返回键
try:
analyser._log_kv(
"错误",
"使用analyser.click关闭详情页失败,使用返回键",
error=str(e2),
error_type=type(e2).__name__,
log_type="ERR",
level="WARNING",
)
except Exception:
pass
try:
analyser.operation.click_back(analyser.device_id)
except Exception as e3:
try:
analyser._log_kv(
"错误",
"所有关闭详情页方法都失败",
error=str(e3),
error_type=type(e3).__name__,
log_type="ERR",
level="ERROR",
)
except Exception:
pass
try:
analyser.operation.delay(0.5)
except Exception as e:
try:
analyser._log_kv(
"错误",
"operation.delay失败,使用time.sleep",
error=str(e),
error_type=type(e).__name__,
log_type="ERR",
level="WARNING",
)
except Exception:
pass
time.sleep(0.5)
def ensure_fudai_detail_open(analyser: Any, fudai_x: int = 0) -> bool:
"""确保福袋详情已打开:当前实现只记录日志并假定已打开。
Args:
analyser: 分析器实例
fudai_x: 福袋的x坐标(可选,当前实现未使用)
"""
try:
analyser._log_kv(
"详情", "ensure_fudai_detail_open:假定已打开", fudai_x=fudai_x
)
except Exception as e:
try:
analyser._log_kv(
"错误",
"记录ensure_fudai_detail_open日志失败",
error=str(e),
error_type=type(e).__name__,
level="WARNING",
)
except Exception:
pass
return True