455 lines
15 KiB
Python
455 lines
15 KiB
Python
from __future__ import annotations
|
|
|
|
"""
|
|
Flow control main helpers (本地模块化实现,基础版)
|
|
================================================
|
|
|
|
当前只实现与你"基础抢福袋、不启用预约"相关的核心流程:
|
|
- handle_stage_after_diagnosis:记录阶段信息(暂不做复杂跳转)
|
|
- find_and_open_fudai_detail:通过像素检测福袋图标并点击打开详情
|
|
- handle_no_fudai_situation:在没有福袋时做简单的等待/上滑切换
|
|
- filter_fudai_detail:根据 config.json 对福袋进行基础筛选
|
|
"""
|
|
|
|
from typing import Any, List, Tuple
|
|
import time
|
|
import os
|
|
|
|
from douyin_detection import check_have_fudai_with_ready_screenshot
|
|
from douyin_fudai_core import should_skip_fudai_by_config
|
|
import douyin_reservation_logic
|
|
import path_config
|
|
from PIL import Image
|
|
|
|
|
|
def handle_stage_after_diagnosis(
|
|
analyser: Any,
|
|
stage: str,
|
|
seen: str,
|
|
loop_count: int,
|
|
) -> bool:
|
|
"""记录阶段诊断结果,当前不改变主流程走向。"""
|
|
try:
|
|
analyser._log_kv(
|
|
"流程",
|
|
"handle_stage_after_diagnosis",
|
|
stage=stage,
|
|
seen=seen,
|
|
loop_count=loop_count,
|
|
)
|
|
except Exception:
|
|
pass
|
|
return False
|
|
|
|
|
|
def find_and_open_fudai_detail(
|
|
analyser: Any,
|
|
room_pos_ref: List[int],
|
|
) -> bool:
|
|
"""在当前直播间查找福袋图标并尝试点击打开详情(基础版,不做预约)。"""
|
|
device_id = getattr(analyser, "device_id", "")
|
|
|
|
# 0. 先检查是否已经在福袋详情页,如果是则直接返回True,避免重复点击
|
|
try:
|
|
import douyin_pixel
|
|
import douyin_detection
|
|
|
|
# 先刷新截图,确保检测准确
|
|
try:
|
|
analyser.operation.get_screenshot(device_id)
|
|
except Exception:
|
|
pass
|
|
|
|
# 使用像素检测检查是否在详情页
|
|
try:
|
|
pic_path = path_config.get_screenshot_path()
|
|
if os.path.exists(pic_path):
|
|
pic = Image.open(pic_path)
|
|
pic.load()
|
|
pic_rgb = pic.convert("RGB")
|
|
|
|
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
|
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
|
y_pianyi = int(getattr(analyser, "y_pianyi", 0) or 0)
|
|
|
|
is_in_detail = douyin_pixel.detect_fudai_detail_page(
|
|
pic_rgb, rx, ry, y_pianyi
|
|
)
|
|
|
|
if is_in_detail:
|
|
try:
|
|
analyser._log_kv(
|
|
"流程",
|
|
"find_and_open_fudai_detail:已在福袋详情页,跳过查找和点击",
|
|
)
|
|
except Exception:
|
|
pass
|
|
return True
|
|
except Exception:
|
|
pass
|
|
|
|
# 使用 check_in_fudai_detail 作为补充检查
|
|
try:
|
|
if douyin_detection.check_in_fudai_detail(analyser):
|
|
try:
|
|
analyser._log_kv(
|
|
"流程",
|
|
"find_and_open_fudai_detail:已在福袋详情页(通过check_in_fudai_detail),跳过查找和点击",
|
|
)
|
|
except Exception:
|
|
pass
|
|
return True
|
|
except Exception:
|
|
pass
|
|
except Exception:
|
|
pass
|
|
|
|
# 1. 利用像素检测福袋图标的 X 坐标(1080 基准)
|
|
x = check_have_fudai_with_ready_screenshot(analyser, screenshot_ready=False)
|
|
if not x:
|
|
try:
|
|
analyser._log_kv("流程", "find_and_open_fudai_detail:未检测到福袋图标", x=int(x or 0))
|
|
except Exception:
|
|
pass
|
|
return False
|
|
|
|
# 点击坐标:用检测到的 X,加一个固定基准 Y(图标所在高度)
|
|
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
|
|
|
|
base_y = 403 # 与检测时使用的同一水平线附近
|
|
click_x = int(x * rx / 1080)
|
|
click_y = int(base_y * ry / 2400)
|
|
|
|
try:
|
|
analyser._log_kv(
|
|
"流程",
|
|
"find_and_open_fudai_detail:检测到福袋图标并点击",
|
|
fudai_x=int(x),
|
|
click_x=click_x,
|
|
click_y=click_y,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 优先走底层 operation.click,失败再尝试 analyser.click
|
|
try:
|
|
analyser.operation.click(device_id, click_x, click_y)
|
|
analyser.operation.delay(1.5) # 增加延迟,等待页面加载
|
|
|
|
# 点击后验证:检测是否误点击到嘉年华页面
|
|
try:
|
|
analyser.operation.get_screenshot(device_id)
|
|
pic_path = path_config.get_screenshot_path()
|
|
if os.path.exists(pic_path):
|
|
pic = Image.open(pic_path)
|
|
pic.load()
|
|
|
|
# 检测嘉年华页面:通过OCR识别"嘉年华"文字
|
|
try:
|
|
# 截取顶部标题区域进行OCR检测
|
|
analyser.cut_pic(400, 100, 800, 200, "", "检测嘉年华标题")
|
|
jiahua_text = str(analyser.operation.analyse_pic_word("检测嘉年华标题", 0) or "")
|
|
|
|
# 检查是否包含嘉年华相关关键词
|
|
jiahua_keywords = ("嘉年华", "抖音嘉年华", "狂欢", "活动")
|
|
if jiahua_text and any(kw in jiahua_text for kw in jiahua_keywords):
|
|
try:
|
|
analyser._log_kv(
|
|
"流程",
|
|
"find_and_open_fudai_detail:误点击到嘉年华页面,返回",
|
|
detected_x=int(x),
|
|
click_x=click_x,
|
|
click_y=click_y,
|
|
ocr_text=jiahua_text[:50],
|
|
)
|
|
except Exception:
|
|
pass
|
|
# 返回直播间
|
|
analyser.operation.click_back(device_id)
|
|
analyser.operation.delay(1.0)
|
|
# 清除历史位置,避免下次继续误点击
|
|
try:
|
|
analyser.last_fudai_x = 0
|
|
except Exception:
|
|
pass
|
|
return False
|
|
except Exception:
|
|
pass
|
|
|
|
# 验证是否成功打开福袋详情页
|
|
pic_rgb = pic.convert("RGB")
|
|
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
|
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
|
y_pianyi = int(getattr(analyser, "y_pianyi", 0) or 0)
|
|
|
|
import douyin_pixel
|
|
is_in_detail = douyin_pixel.detect_fudai_detail_page(
|
|
pic_rgb, rx, ry, y_pianyi
|
|
)
|
|
|
|
if not is_in_detail:
|
|
# 如果不在福袋详情页,可能是误点击,返回
|
|
try:
|
|
analyser._log_kv(
|
|
"流程",
|
|
"find_and_open_fudai_detail:点击后未检测到福袋详情页,可能误点击",
|
|
detected_x=int(x),
|
|
click_x=click_x,
|
|
click_y=click_y,
|
|
)
|
|
except Exception:
|
|
pass
|
|
# 返回直播间
|
|
analyser.operation.click_back(device_id)
|
|
analyser.operation.delay(1.0)
|
|
# 清除历史位置,避免下次继续误点击
|
|
try:
|
|
analyser.last_fudai_x = 0
|
|
except Exception:
|
|
pass
|
|
return False
|
|
except Exception:
|
|
pass
|
|
|
|
return True
|
|
except Exception:
|
|
try:
|
|
analyser.click(x, base_y)
|
|
analyser.operation.delay(1.5)
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
|
|
def handle_no_fudai_situation(
|
|
analyser: Any,
|
|
x: int,
|
|
swipe_times: int,
|
|
wait_times_ref: List[int],
|
|
needswitch: bool,
|
|
reservation_focus_until_ts: float,
|
|
reservations: list,
|
|
room_pos: int,
|
|
post_switch_delay_s: float,
|
|
swipe_up_switch_callback,
|
|
swipe_down_back_callback,
|
|
sort_reservations_callback,
|
|
clear_reservations_callback,
|
|
write_state_callback,
|
|
back_to_list_callback,
|
|
into_list_callback,
|
|
reservation_focus_until_ts_ref: List[float],
|
|
reset_to_list_after_reservations_ref: List[bool],
|
|
swipe_times_ref: List[int],
|
|
) -> bool:
|
|
"""当前直播间没有福袋时的基础处理。
|
|
|
|
本地版实现:
|
|
- 先让预约模块有机会“抢占”流程(例如到点需要回退到预约直播间);
|
|
- 若未触发预约回退,则执行简单的等待 + 上滑切换。
|
|
"""
|
|
try:
|
|
analyser._log_kv(
|
|
"流程",
|
|
"handle_no_fudai_situation",
|
|
x=x,
|
|
swipe_times=swipe_times,
|
|
room_pos=room_pos,
|
|
reservations=len(reservations),
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# --- 优先让“预约回退窗口”有机会执行 ---
|
|
try:
|
|
if getattr(analyser, "high_value_fudai_enabled", False) and reservations:
|
|
# 构造回调:下滑回退一个直播间
|
|
def _swipe_down_once() -> None:
|
|
try:
|
|
if swipe_down_back_callback:
|
|
swipe_down_back_callback()
|
|
except Exception:
|
|
pass
|
|
|
|
def _sort_reservations() -> None:
|
|
try:
|
|
# 使用统一核心逻辑排序
|
|
douyin_reservation_logic._sort_reservations_inplace(reservations) # type: ignore[attr-defined]
|
|
except Exception:
|
|
pass
|
|
|
|
def _write_state() -> None:
|
|
try:
|
|
if write_state_callback:
|
|
write_state_callback()
|
|
except Exception:
|
|
pass
|
|
|
|
# 调用预约窗口处理:若返回 True,表示已触发回退,本轮不再上滑
|
|
def _seek_reservation_room(item: dict) -> bool:
|
|
"""委托给 analyser._seek_reservation_room,避免简化为恒 True。"""
|
|
try:
|
|
fn = getattr(analyser, "_seek_reservation_room", None)
|
|
if not fn:
|
|
return False
|
|
return bool(fn(item))
|
|
except Exception:
|
|
return False
|
|
|
|
preempted = douyin_reservation_logic.process_reservation_attend_window(
|
|
analyser=analyser,
|
|
reservations=reservations,
|
|
room_pos=room_pos,
|
|
reservation_focus_until_ts_ref=reservation_focus_until_ts_ref,
|
|
reservation_after_action_pending_ref=[False],
|
|
reservation_back_swipe_delay_s=float(
|
|
getattr(analyser, "reservation_back_swipe_delay", 0.6) or 0.6
|
|
),
|
|
swipe_down_back_callback=_swipe_down_once,
|
|
seek_reservation_room_callback=_seek_reservation_room,
|
|
sort_reservations_callback=_sort_reservations,
|
|
write_state_callback=_write_state,
|
|
)
|
|
if preempted:
|
|
return True
|
|
except Exception:
|
|
# 预约处理异常不影响基础切换逻辑
|
|
pass
|
|
|
|
# 简单策略:轻微等待 + 上滑切换
|
|
try:
|
|
analyser.operation.delay(1.0)
|
|
except Exception:
|
|
time.sleep(1.0)
|
|
|
|
if needswitch and swipe_up_switch_callback:
|
|
swipe_up_switch_callback()
|
|
try:
|
|
analyser.operation.delay(post_switch_delay_s or 1.2)
|
|
except Exception:
|
|
time.sleep(post_switch_delay_s or 1.2)
|
|
|
|
return False
|
|
|
|
|
|
def filter_fudai_detail(
|
|
analyser: Any,
|
|
detail: Tuple,
|
|
x: int,
|
|
needswitch: bool,
|
|
post_switch_delay_s: float,
|
|
close_detail_callback,
|
|
swipe_up_switch_callback,
|
|
add_reservation_callback,
|
|
) -> bool:
|
|
"""福袋详情过滤逻辑:根据 config.json 对福袋进行基础筛选。"""
|
|
skip, reason = should_skip_fudai_by_config(analyser, detail)
|
|
try:
|
|
content = str(detail[0] or "") if isinstance(detail, tuple) and len(detail) >= 1 else ""
|
|
analyser._log_kv(
|
|
"流程",
|
|
"filter_fudai_detail",
|
|
x=x,
|
|
needswitch=needswitch,
|
|
skip=int(bool(skip)),
|
|
reason=reason,
|
|
content_preview=content[:60] if content else "",
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
if not skip:
|
|
return False
|
|
|
|
try:
|
|
close_detail_callback(analyser)
|
|
except Exception:
|
|
try:
|
|
analyser.operation.click_back(analyser.device_id)
|
|
except Exception:
|
|
pass
|
|
|
|
if needswitch and swipe_up_switch_callback:
|
|
try:
|
|
swipe_up_switch_callback()
|
|
analyser.operation.delay(post_switch_delay_s or 1.2)
|
|
except Exception:
|
|
try:
|
|
time.sleep(post_switch_delay_s or 1.2)
|
|
except Exception:
|
|
pass
|
|
return True
|
|
|
|
return False
|
|
|
|
# 简单策略:轻微等待 + 上滑切换
|
|
try:
|
|
analyser.operation.delay(1.0)
|
|
except Exception:
|
|
time.sleep(1.0)
|
|
|
|
if needswitch and swipe_up_switch_callback:
|
|
swipe_up_switch_callback()
|
|
try:
|
|
analyser.operation.delay(post_switch_delay_s or 1.2)
|
|
except Exception:
|
|
time.sleep(post_switch_delay_s or 1.2)
|
|
|
|
return False
|
|
|
|
|
|
def filter_fudai_detail(
|
|
analyser: Any,
|
|
detail: Tuple,
|
|
x: int,
|
|
needswitch: bool,
|
|
post_switch_delay_s: float,
|
|
close_detail_callback,
|
|
swipe_up_switch_callback,
|
|
add_reservation_callback,
|
|
) -> bool:
|
|
"""福袋详情过滤逻辑:根据 config.json 对福袋进行基础筛选。"""
|
|
skip, reason = should_skip_fudai_by_config(analyser, detail)
|
|
try:
|
|
content = str(detail[0] or "") if isinstance(detail, tuple) and len(detail) >= 1 else ""
|
|
analyser._log_kv(
|
|
"流程",
|
|
"filter_fudai_detail",
|
|
x=x,
|
|
needswitch=needswitch,
|
|
skip=int(bool(skip)),
|
|
reason=reason,
|
|
content_preview=content[:60] if content else "",
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
if not skip:
|
|
return False
|
|
|
|
try:
|
|
close_detail_callback(analyser)
|
|
except Exception:
|
|
try:
|
|
analyser.operation.click_back(analyser.device_id)
|
|
except Exception:
|
|
pass
|
|
|
|
if needswitch and swipe_up_switch_callback:
|
|
try:
|
|
swipe_up_switch_callback()
|
|
analyser.operation.delay(post_switch_delay_s or 1.2)
|
|
except Exception:
|
|
try:
|
|
time.sleep(post_switch_delay_s or 1.2)
|
|
except Exception:
|
|
pass
|
|
return True
|
|
|
|
return False
|
|
|