- 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.
425 lines
17 KiB
Python
425 lines
17 KiB
Python
from __future__ import annotations
|
||
|
||
"""
|
||
滑动操作模块
|
||
============
|
||
|
||
职责:滑动操作和导航
|
||
- 上滑切换直播间
|
||
- 下滑返回
|
||
- 滑动次数重置
|
||
- 返回推荐页并重新进入
|
||
"""
|
||
|
||
import time
|
||
import subprocess
|
||
from typing import Any
|
||
from douyin.logging.action_tracker import track_action
|
||
|
||
from douyin.handlers import douyin_swipe_ops
|
||
|
||
|
||
class SwipeHandler:
|
||
"""滑动处理器:统一管理滑动操作"""
|
||
|
||
@staticmethod
|
||
@track_action("swipe_up_to_next_live")
|
||
def swipe_up_to_next_live(analyser: Any) -> None:
|
||
"""上滑切换直播间(简单版封装)。
|
||
|
||
注意:如果处于预约锁定期内(reservation_focus_until_ts > 当前时间),
|
||
则不允许上划离开预约直播间,直接返回。
|
||
|
||
参数:
|
||
analyser: 分析器对象
|
||
"""
|
||
# 检查是否在预约锁定期内
|
||
try:
|
||
focus_until = float(
|
||
getattr(analyser, "reservation_focus_until_ts", 0.0) or 0.0
|
||
)
|
||
now_ts = time.time()
|
||
if focus_until > 0 and now_ts < focus_until:
|
||
# 在锁定期内,不允许上划离开预约直播间
|
||
remain_seconds = int(focus_until - now_ts)
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 锁定期内不允许上划离开预约直播间",
|
||
focus_until=int(focus_until),
|
||
remain_seconds=remain_seconds,
|
||
queue_full_waiting=getattr(
|
||
analyser, "queue_full_waiting_at_earliest", False
|
||
),
|
||
)
|
||
except Exception:
|
||
pass
|
||
return # 直接返回,不执行上划操作
|
||
except Exception:
|
||
# 如果检查锁定期时出错,继续执行上划(避免因异常导致无法切换)
|
||
pass
|
||
|
||
start_time = time.time()
|
||
douyin_swipe_ops.swipe_up_switch_live(analyser)
|
||
# 记录直播间位置:上滑 +1
|
||
try:
|
||
analyser.room_pos += 1
|
||
except Exception:
|
||
analyser.room_pos = 0
|
||
elapsed = time.time() - start_time
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"操作", "上滑切换直播间", elapsed_seconds=round(elapsed, 2)
|
||
)
|
||
|
||
# 记录滑动次数(用于滑动次数重置功能)
|
||
try:
|
||
if not hasattr(analyser, "_swipe_times"):
|
||
analyser._swipe_times = 0
|
||
analyser._swipe_times += 1
|
||
except Exception:
|
||
pass
|
||
|
||
# 检查是否需要重置(滑动次数达到阈值)
|
||
try:
|
||
SwipeHandler.maybe_reset_after_swipes(analyser)
|
||
except Exception:
|
||
pass
|
||
|
||
# 设置标志:刚刚切换了直播间,阶段检测后需要等待福袋加载
|
||
try:
|
||
analyser._just_switched_room = True
|
||
except Exception:
|
||
pass
|
||
|
||
# 清除第一次读取标志,确保新直播间的福袋会进行完整的OCR识别
|
||
try:
|
||
analyser._fudai_detail_first_read = False
|
||
analyser._last_fudai_detail_tuple = None
|
||
except Exception:
|
||
pass
|
||
|
||
# 记录当前直播间的标题和位置到运行时映射表(用于回退校验)
|
||
# 如果是在回退过程中(_seek_reservation_room),跳过记录,因为之前已经记录过了
|
||
try:
|
||
skip_record = bool(
|
||
getattr(analyser, "_skip_record_title_during_seek", False)
|
||
)
|
||
if not skip_record:
|
||
analyser._record_room_title_and_pos() # type: ignore[attr-defined]
|
||
except Exception:
|
||
pass
|
||
|
||
@staticmethod
|
||
@track_action("swipe_down_back_from_live")
|
||
def swipe_down_back_from_live(analyser: Any) -> None:
|
||
"""上划回退到上一个直播间(供预约/无福袋逻辑使用)。
|
||
|
||
参数:
|
||
analyser: 分析器对象
|
||
"""
|
||
start_time = time.time()
|
||
try:
|
||
douyin_swipe_ops.swipe_down_back_live(analyser)
|
||
except Exception:
|
||
# 若子模块未实现,则退化为点击返回键
|
||
try:
|
||
analyser.operation.click_back(analyser.device_id)
|
||
except Exception:
|
||
pass
|
||
# 记录直播间位置:下滑/回退 -1
|
||
try:
|
||
analyser.room_pos -= 1
|
||
except Exception:
|
||
analyser.room_pos = 0
|
||
|
||
# 记录当前直播间的标题和位置到运行时映射表(用于回退校验)
|
||
# 如果是在回退过程中(_seek_reservation_room),跳过记录,因为之前已经记录过了
|
||
try:
|
||
skip_record = bool(
|
||
getattr(analyser, "_skip_record_title_during_seek", False)
|
||
)
|
||
if not skip_record:
|
||
analyser._record_room_title_and_pos() # type: ignore[attr-defined]
|
||
except Exception:
|
||
pass
|
||
|
||
@staticmethod # type: ignore[misc]
|
||
def maybe_reset_after_swipes(analyser: Any) -> int:
|
||
"""达到切换阈值后,在当前直播间页面回退到第一个直播间。
|
||
|
||
参数:
|
||
analyser: 分析器对象
|
||
|
||
返回:
|
||
重置后的滑动次数(通常为0)
|
||
"""
|
||
try:
|
||
needswitch = bool(getattr(analyser, "needswitch", True))
|
||
except Exception:
|
||
needswitch = True
|
||
if not needswitch:
|
||
return getattr(analyser, "_swipe_times", 0)
|
||
|
||
if not bool(getattr(analyser, "reset_after_swipes_enabled", False)):
|
||
return getattr(analyser, "_swipe_times", 0)
|
||
|
||
try:
|
||
swipe_times = int(getattr(analyser, "_swipe_times", 0) or 0)
|
||
except Exception:
|
||
swipe_times = 0
|
||
|
||
threshold = max(1, int(getattr(analyser, "reset_after_swipes", 0) or 0))
|
||
if swipe_times < threshold:
|
||
return swipe_times
|
||
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"流程",
|
||
"已切换直播间{}次,达到阈值{}:向上回退到第一个直播间".format(
|
||
swipe_times, threshold
|
||
),
|
||
swipe_times=swipe_times,
|
||
threshold=threshold,
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 回退次数按当前已切换次数来(更符合"回到本轮第一个直播间"的语义)
|
||
back_times = int(swipe_times)
|
||
# 防止无限循环:最大回退次数限制为100
|
||
if back_times > 100:
|
||
back_times = 100
|
||
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
|
||
|
||
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)
|
||
try:
|
||
analyser._swipe_times = 0
|
||
analyser.room_pos = 0
|
||
except Exception:
|
||
pass
|
||
return 0
|
||
|
||
@staticmethod
|
||
@track_action("return_to_recommend_and_reenter_live")
|
||
def return_to_recommend_and_reenter_live(analyser: Any) -> None:
|
||
"""返回推荐页面并重新进入直播间。
|
||
|
||
通过多次点击返回键返回到推荐页面,然后点击关注标签并进入直播间。
|
||
|
||
参数:
|
||
analyser: 分析器对象
|
||
"""
|
||
analyser._log_kv("超时", "开始返回推荐页面") # type: ignore[attr-defined]
|
||
|
||
# 多次点击返回键,直到到达推荐页面
|
||
max_back_clicks = analyser.MAX_BACK_CLICKS_TO_RECOMMEND
|
||
for i in range(max_back_clicks):
|
||
try:
|
||
# 截图并诊断当前阶段
|
||
ok = analyser.operation.get_screenshot(analyser.device_id, use_service=False)
|
||
if not ok:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时", f"获取截图失败 ({i+1}/{max_back_clicks})"
|
||
)
|
||
# 即使截图失败,也尝试点击返回键
|
||
analyser.operation.click_back(analyser.device_id)
|
||
analyser.operation.delay(analyser.BACK_KEY_CLICK_DELAY)
|
||
continue
|
||
|
||
# 诊断当前阶段
|
||
stage, seen = analyser._diagnose_stage_from_current_screen() # type: ignore[attr-defined]
|
||
|
||
# 如果已经在推荐页面,跳出循环
|
||
if stage == "RECOMMEND_PAGE":
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时", f"已到达推荐页面 ({i+1}次返回键)"
|
||
)
|
||
break
|
||
|
||
# 点击返回键
|
||
analyser.operation.click_back(analyser.device_id)
|
||
analyser.operation.delay(analyser.BACK_KEY_CLICK_DELAY)
|
||
|
||
if i == max_back_clicks - 1:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时",
|
||
f"已点击{max_back_clicks}次返回键,可能未到达推荐页面",
|
||
)
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时",
|
||
f"返回推荐页面时出错 ({i+1}/{max_back_clicks})",
|
||
error=str(e),
|
||
)
|
||
except Exception:
|
||
pass
|
||
# 继续尝试点击返回键
|
||
try:
|
||
analyser.operation.click_back(analyser.device_id)
|
||
analyser.operation.delay(analyser.BACK_KEY_CLICK_DELAY)
|
||
except Exception as e2:
|
||
# 记录异常但继续执行
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时",
|
||
"点击返回键失败但继续",
|
||
error=str(e2),
|
||
error_type=type(e2).__name__,
|
||
)
|
||
except Exception:
|
||
pass # 日志系统本身出错时忽略
|
||
|
||
# 等待页面稳定
|
||
analyser.operation.delay(analyser.PAGE_STABLE_DELAY)
|
||
|
||
# 再次截图确认当前阶段
|
||
try:
|
||
ok = analyser.operation.get_screenshot(analyser.device_id)
|
||
if ok:
|
||
stage, seen = analyser._diagnose_stage_from_current_screen() # type: ignore[attr-defined]
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时", "确认当前阶段", stage=stage, seen=seen
|
||
)
|
||
except Exception as e:
|
||
# 记录异常但继续执行(阶段诊断失败不影响主流程)
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时",
|
||
"确认阶段时出错但继续",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
)
|
||
except Exception:
|
||
pass # 日志系统本身出错时忽略
|
||
|
||
# 点击关注标签并进入直播间(复用现有逻辑)
|
||
try:
|
||
# 点击关注标签 (709, 169) - 基于1080x2400分辨率
|
||
follow_tab_x = int(709 * analyser.resolution_ratio_x // 1080)
|
||
follow_tab_y = int(169 * analyser.resolution_ratio_y // 2400)
|
||
analyser.operation.click(analyser.device_id, follow_tab_x, follow_tab_y)
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时", "点击关注标签", x=follow_tab_x, y=follow_tab_y
|
||
)
|
||
analyser.operation.delay(analyser.FOLLOW_TAB_CLICK_DELAY)
|
||
|
||
# 点击进入直播间的位置 (103, 385),连点两次
|
||
enter_live_x = int(103 * analyser.resolution_ratio_x // 1080)
|
||
enter_live_y = int(385 * analyser.resolution_ratio_y // 2400)
|
||
analyser.operation.click(analyser.device_id, enter_live_x, enter_live_y)
|
||
analyser.operation.delay(analyser.ENTER_LIVE_CLICK_DELAY)
|
||
analyser.operation.click(analyser.device_id, enter_live_x, enter_live_y)
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时",
|
||
"点击进入直播间位置",
|
||
x=enter_live_x,
|
||
y=enter_live_y,
|
||
次数=2,
|
||
)
|
||
analyser.operation.delay(analyser.ENTER_LIVE_FINAL_DELAY)
|
||
|
||
analyser._log_kv("超时", "已重新进入直播间") # type: ignore[attr-defined]
|
||
|
||
# 标记刚刚切换进入直播间,后续读取标题时会等待标题区域出现
|
||
try:
|
||
analyser._just_switched_room = True
|
||
except Exception:
|
||
pass
|
||
|
||
# 重置超时相关时间戳(在成功重新进入直播间后重置,避免立即再次触发)
|
||
try:
|
||
analyser._last_successful_attend_ts = time.time()
|
||
analyser._run_start_ts = time.time()
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时",
|
||
"重置时间戳时出错",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
)
|
||
except Exception:
|
||
pass # 日志系统本身出错时忽略
|
||
except Exception as e:
|
||
# 记录异常但继续执行
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时",
|
||
"重新进入直播间时出错",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
)
|
||
except Exception:
|
||
pass # 日志系统本身出错时忽略
|
||
# 如果出错,尝试回退
|
||
try:
|
||
analyser.operation.click_back(analyser.device_id)
|
||
analyser.operation.delay(analyser.BACK_KEY_CLICK_DELAY)
|
||
except Exception as e2:
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"超时",
|
||
"回退操作失败",
|
||
error=str(e2),
|
||
error_type=type(e2).__name__,
|
||
)
|
||
except Exception:
|
||
pass # 日志系统本身出错时忽略
|