Files
douyin_guaji-main/douyin_fudai_core.py
T
Your Name 836a31d817 refactor: Remove outdated files and enhance Web GUI functionality
- Deleted obsolete markdown files related to follow tab coordinates testing and results.
- Removed outdated batch scripts for launching the Web GUI.
- Updated `config.json` to improve reservation handling and adjust prize value settings.
- Enhanced logging and error handling in `web_gui.py` and `app.js` for better user experience.
- Added automatic screenshot functionality during Web GUI startup for improved monitoring.
2025-12-19 03:31:08 +08:00

745 lines
31 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
import re
import time
from typing import Tuple, Any
from douyin_utils import normalize_countdown_text, extract_prize_value_yuan
def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, float]:
"""获取福袋的内容和倒计时(从 `fudai_analyse.get_fudai_contain` 抽离)。"""
def _parse_mm_ss(text: str):
m = re.search(r"(\d{1,2})[::](\d{2})", str(text or ""))
if not m:
return None
try:
return int(m.group(1)), int(m.group(2))
except Exception:
return None
def _extract_2digits(text: str) -> str:
digits = "".join(ch for ch in str(text or "") if ch.isdigit())
if len(digits) >= 2:
return digits[:2]
if len(digits) == 1:
return "0" + digits
return ""
def _read_countdown_split(box: tuple[int, int, int, int]) -> str:
"""将倒计时区域按三等分切成“分钟段/秒段/后开奖段”,分别OCR分钟与秒,组合为 mm:ss。"""
try:
x1, y1, x2, y2 = box
w = max(1, int(x2 - x1))
seg = max(1, w // 3)
# 前 1/3:分钟;中 1/3:秒(第三段通常包含“后开奖”)
analyser.cut_pic(x1, y1, x1 + seg, y2, "", "福袋倒计时_分钟", analyser.y_pianyi)
analyser.cut_pic(x1 + seg, y1, x1 + 2 * seg, y2, "", "福袋倒计时_秒", analyser.y_pianyi)
mm_raw = analyser.operation.analyse_pic_word("福袋倒计时_分钟", 0)
ss_raw = analyser.operation.analyse_pic_word("福袋倒计时_秒", 0)
mm = _extract_2digits(mm_raw)
ss = _extract_2digits(ss_raw)
if mm and ss:
return f"{mm}:{ss}"
except Exception:
return ""
return ""
def _extract_participants_and_clean(text: str):
if not text:
return 0, 0, ""
compact = re.sub(r"\s+", "", str(text))
# 例如:共1份1173人已参与
copies = 0
participants = 0
m = re.search(r"共(\d+)份", compact)
if m:
copies = int(m.group(1))
m = re.search(r"(\d+)人已参与", compact)
if m:
participants = int(m.group(1))
# 清理掉份数/参与人数等尾巴,避免干扰关键词匹配
cleaned = re.sub(r"共\d+份", "", compact)
cleaned = re.sub(r"\d+人已参与", "", cleaned)
return participants, copies, cleaned
countdown_box = None
if renwu == 2: # 如果是2个任务的
# 优化:稍微缩小宽度(1000->980),增加高度(1460->1470),减少右侧干扰,确保内容完整
analyser.cut_pic(390, 1240, 980, 1470, "", "福袋内容", analyser.y_pianyi) # 福袋内容详情
# 优化:稍微扩大高度(1220->1230),确保包含完整的"后开奖"文字
countdown_box = (390, 1110, 690, 1230)
analyser.cut_pic(*countdown_box, "", "福袋倒计时", analyser.y_pianyi) # 完整福袋详情倒计时
# 优化:增加高度(1560->1580),确保包含"元"等单位文字
analyser.cut_pic(390, 1460, 1000, 1580, "", "福袋价值", analyser.y_pianyi) # 奖品价值/价格
elif renwu == 1: # 如果是1个任务的
# 优化:稍微缩小宽度,增加高度,减少干扰
analyser.cut_pic(390, 1300, 980, 1530, "", "福袋内容", analyser.y_pianyi) # 福袋内容详情
# 优化:稍微扩大高度,确保完整
countdown_box = (390, 1180, 690, 1300)
analyser.cut_pic(*countdown_box, "", "福袋倒计时", analyser.y_pianyi) # 完整福袋详情倒计时
# 优化:增加高度,包含单位
analyser.cut_pic(390, 1520, 1000, 1630, "", "福袋价值", analyser.y_pianyi) # 奖品价值/价格
elif renwu == 3: # 如果是3个任务的
# 优化:稍微缩小宽度,增加高度
analyser.cut_pic(390, 1160, 980, 1390, "", "福袋内容", analyser.y_pianyi) # 福袋内容详情
# 优化:稍微扩大高度
countdown_box = (390, 1010, 690, 1130)
analyser.cut_pic(*countdown_box, "", "福袋倒计时", analyser.y_pianyi) # 完整福袋详情倒计时
# 优化:增加高度
analyser.cut_pic(390, 1380, 1000, 1490, "", "福袋价值", analyser.y_pianyi) # 奖品价值/价格
else:
# 优化:稍微缩小宽度,增加高度
analyser.cut_pic(390, 1600, 980, 1830, "", "福袋内容", analyser.y_pianyi) # 福袋内容详情
# 优化:稍微扩大高度
countdown_box = (390, 1460, 690, 1580)
analyser.cut_pic(*countdown_box, "", "福袋倒计时", analyser.y_pianyi) # 完整福袋详情倒计时
# 优化:增加高度
analyser.cut_pic(390, 1820, 1000, 1930, "", "福袋价值", analyser.y_pianyi) # 奖品价值/价格
fudai_content_raw = analyser.operation.analyse_pic_word("福袋内容", 0)
participants, copies, fudai_content_text = _extract_participants_and_clean(fudai_content_raw)
analyser._log_kv("福袋", "识别福袋内容", level="INFO", content=fudai_content_text[:60] if fudai_content_text else "未识别到")
# 记录进入直播间到识别到福袋的耗时(仅记录一次)
try:
if getattr(analyser, "_enter_live_ts", 0.0) and not getattr(analyser, "_enter_live_fudai_logged", False):
try:
secs = float(time.time() - float(analyser._enter_live_ts or 0.0))
except Exception:
secs = -1.0
reason = str(getattr(analyser, "_enter_live_reason", "") or "")
analyser._log_kv("性能", "进入直播间到识别福袋耗时", seconds=round(secs, 2), enter_reason=reason)
try:
analyser._enter_live_fudai_logged = True
except Exception:
pass
if copies:
analyser._log_kv("福袋", "识别福袋信息", copies=copies, participants=participants if participants else 0)
except Exception:
pass
# 倒计时:优先用完整OCR(更快,只需1次),失败再用分割OCR
time_text = ""
time_raw = analyser.operation.analyse_pic_word("福袋倒计时", 0)
time_text = normalize_countdown_text(time_raw)
if not time_text or not _parse_mm_ss(time_text):
try:
if countdown_box:
split_raw = _read_countdown_split(countdown_box)
split_text = normalize_countdown_text(split_raw)
if _parse_mm_ss(split_text):
time_text = split_text
except Exception:
pass
analyser._log_kv("福袋", "识别倒计时", countdown=time_text if time_text else "未识别到")
value_raw = analyser.operation.analyse_pic_word("福袋价值", 0)
prize_value_yuan = extract_prize_value_yuan(value_raw)
if prize_value_yuan:
if float(prize_value_yuan).is_integer():
show_value = int(prize_value_yuan)
else:
show_value = prize_value_yuan
analyser._log_kv("福袋", "识别奖品价值", value_yuan=show_value)
return fudai_content_text, time_text, participants, copies, prize_value_yuan
def check_contain(analyser, contains: str = "") -> bool:
"""检查福袋内容是否是想要,从配置文件 config 中读取。
返回值语义与原版保持一致:
- True : “建议丢弃”(内容不是想要的 / 未明确是否想要)
- False : “可以保留”(内容是想要的,或凌晨时段不做内容过滤)
"""
try:
hour = int(analyser.operation.get_current_hour())
except Exception:
hour = -1
# 凌晨:不对内容做要求(不过滤)
if 0 < hour < 7:
try:
analyser._log_kv("筛选", "凌晨不过滤福袋内容", hour=hour, contains=contains[:50] if contains else "")
except Exception:
pass
return False
try:
for contain in getattr(analyser, "contains_want", []) or []:
if contain and contain in (contains or ""):
try:
analyser._log_kv("筛选", "福袋内容是想要的", keyword=contain, contains=contains[:80] if contains else "")
except Exception:
pass
return False
for contain in getattr(analyser, "contains_not_want", []) or []:
if contain and contain in (contains or ""):
try:
analyser._log_kv("筛选", "福袋内容不是想要的", keyword=contain, contains=contains[:80] if contains else "")
except Exception:
pass
return True
try:
analyser._log_kv("筛选", "福袋内容未明确是否想要", contains=contains[:80] if contains else "")
except Exception:
pass
return True
except Exception:
# 容错:异常时不过滤
try:
analyser._log_kv("筛选", "check_contain 异常,默认保留", contains=contains[:80] if contains else "")
except Exception:
pass
return False
def attend_choujiang(analyser) -> bool:
"""点击参与抽奖。"""
analyser.last_attend_fail_reason = ""
click_times = 0
while click_times < 2:
analyser.cut_pic(306, 2030, 780, 2110, "", "参与按钮") # 参与福袋抽奖的文字
# 优化:尝试多种OCR模式提高精准度(优先保证精准)
attend_button_text = str(analyser.operation.analyse_pic_word("参与按钮", 0) or "")
if not attend_button_text or not any(
k in attend_button_text for k in ("参与成功", "还需看播", "评论", "参与抽奖", "开始观看", "参与")
):
attend_button_text = str(analyser.operation.analyse_pic_word("参与按钮", 1) or "")
analyser._log_kv("参与", "识别参与按钮", text=attend_button_text)
if "参与成功" in attend_button_text: # 如果识别到已经参与抽奖
analyser._log_kv("参与", "已参与,等待开奖", result="已参与")
analyser.click(500, 470) # 点击刚才打开福袋的旁边位置
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return True
elif "还需看播" in attend_button_text: # 如果识别到需要看播
analyser.last_attend_fail_reason = "watch_required"
analyser._log_kv("参与", "需要看播时长:默认不参与", result="看播门槛")
analyser.click(500, 470)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
elif "无法参与" in attend_button_text:
analyser._log_kv("参与", "无法参与(条件不满足)", result="无法参与")
analyser.click(500, 470)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
elif "时长不足" in attend_button_text:
analyser._log_kv("参与", "无法参与(看播时长不足)", result="时长不足")
analyser.click(500, 470)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
elif "评论" in attend_button_text:
analyser.click(500, 2060)
analyser._log_kv("参与", "点击参与抽奖", action="点击参与", mode="评论")
return True
elif "参与抽奖" in attend_button_text:
analyser.click(500, 2060)
analyser._log_kv("参与", "点击参与抽奖", action="点击参与", mode="普通")
return True
elif "加入粉丝团" in attend_button_text or "粉丝团" in attend_button_text:
# 需要粉丝团门槛:先点击尝试进入加入/点亮流程
analyser.last_attend_fail_reason = "fansclub_required"
analyser.click(500, 2060)
analyser._log_kv("参与", "粉丝团门槛:已点击加入按钮", result="需要粉丝团")
analyser.operation.delay(2)
if not analyser.auto_join_fansclub:
analyser._log_kv("参与", "未启用自动加粉丝团:等待手动确认", wait_seconds=15)
analyser.operation.delay(15)
# 点击粉丝团按钮后,可能进入“加入粉丝团”独立页面:需要在该页再次点击“加入粉丝团”才能成功。
def _recover_to_fudai_detail_after_fansclub(max_steps: int = 4) -> bool:
for i in range(int(max_steps or 4)):
try:
analyser._shot_and_diagnose("粉丝团", "粉丝团后定位福袋详情({}/{})".format(i + 1, max_steps), analyser.device_id)
except Exception:
pass
try:
st, seen = analyser._diagnose_stage_from_current_screen()
except Exception:
st, seen = "", ""
if st == "FUDai_DETAIL":
return True
if st == "FANSCLUB_JOIN":
# 在加入页点击底部“加入粉丝团”按钮(与详情页按钮同位置)
analyser._log_kv("参与", "粉丝团加入页:点击加入粉丝团", stage=st, seen=seen, attempt=i + 1)
try:
analyser.click(500, 2060)
except Exception:
pass
analyser.operation.delay(2)
# 尝试返回到详情
try:
analyser.operation.click_back(analyser.device_id)
analyser.operation.delay(1)
except Exception:
pass
continue
# 已经回到直播间/列表时:不要继续按返回键,避免退出直播
if st in ("IN_LIVE_ROOM", "FOLLOW_LIST", "LIVE_LIST"):
analyser._log_kv("参与", "已离开福袋详情:停止继续返回", stage=st, seen=seen)
return False
# 其它未知弹窗:尝试一次返回
analyser._log_kv("参与", "粉丝团流程:尝试返回", stage=st, seen=seen)
try:
analyser.operation.click_back(analyser.device_id)
except Exception:
pass
analyser.operation.delay(1)
return False
ok = _recover_to_fudai_detail_after_fansclub()
if ok:
analyser._log_kv("参与", "粉丝团流程结束:已回到福袋详情", result="回到详情")
return attend_choujiang(analyser)
analyser._log_kv("参与", "粉丝团流程失败:未回到福袋详情", result="粉丝团门槛仍未解决")
analyser.operation.delay(1.2)
try:
analyser.operation.get_screenshot(analyser.device_id)
except Exception:
pass
if analyser.check_in_fudai_detail():
analyser._log_kv("参与", "粉丝团流程失败:仍在详情页,执行关闭", stage="FUDai_DETAIL")
try:
analyser.click(500, 470)
except Exception:
pass
analyser.operation.delay(0.8)
else:
analyser._log_kv("参与", "粉丝团流程失败:已不在详情页,视为已退出", stage="UNKNOWN")
try:
analyser._log_kv("参与", "粉丝团门槛仍未解决", result="粉丝团门槛仍未解决")
except Exception:
pass
return False
else:
# 未识别到任何可用文案:再尝试一次
analyser._log_kv("参与", "未识别到可用参与按钮文案", text=attend_button_text or "")
click_times += 1
analyser.operation.delay(1)
continue
analyser._log_kv("参与", "参与抽奖多次点击失败", result="多次失败")
return False
def check_lucky_draw_result(analyser):
"""判定福袋抽奖的结果。"""
# 速度优化:只在需要时截图(如果有最近截图且缓存有效,可跳过)
cache_key = ("lucky_draw_result", 0)
now = time.time()
need_screenshot = True
# 检查缓存是否有效
if cache_key in analyser._ocr_cache:
cached_ts, cached_result = analyser._ocr_cache[cache_key]
if (now - cached_ts) < 0.5 and cached_result and ("没有抽中" in cached_result or "抽中福袋" in cached_result):
need_screenshot = False
if need_screenshot:
analyser._shot_and_diagnose("开奖结果", "识别是否中奖/未中奖", analyser.device_id)
# 优化:稍微扩大区域(655->650, 755->765),确保包含完整的开奖结果文字
analyser.cut_pic(350, 650, 750, 765, "", "抽奖结果") # 没有抽中福袋位置
# 诊断开始时间
t0 = time.time()
# 检查缓存
if cache_key in analyser._ocr_cache:
cached_ts, cached_result = analyser._ocr_cache[cache_key]
if (now - cached_ts) < analyser._ocr_cache_ttl:
lucky_draw_result = cached_result
else:
lucky_draw_result = str(analyser.operation.analyse_pic_word("抽奖结果", 0) or "")
if not lucky_draw_result or ("没有抽中" not in lucky_draw_result and "抽中福袋" not in lucky_draw_result):
lucky_draw_result = str(analyser.operation.analyse_pic_word("抽奖结果", 1) or "")
analyser._ocr_cache[cache_key] = (now, lucky_draw_result)
else:
lucky_draw_result = str(analyser.operation.analyse_pic_word("抽奖结果", 0) or "")
if not lucky_draw_result or ("没有抽中" not in lucky_draw_result and "抽中福袋" not in lucky_draw_result):
lucky_draw_result = str(analyser.operation.analyse_pic_word("抽奖结果", 1) or "")
analyser._ocr_cache[cache_key] = (now, lucky_draw_result)
dur = time.time() - t0
try:
brief = (lucky_draw_result or "").replace("\n", " ")[:140]
except Exception:
brief = ""
try:
analyser._log_kv(
"开奖诊断",
"lucky_draw_result",
brief=brief,
ocr_dur=round(dur, 2),
)
except Exception:
pass
if "没有抽中" in (lucky_draw_result or ""):
# 速度优化:使用缓存避免重复OCR
cache_key2 = ("no_award_confirm", 0)
if cache_key2 in analyser._ocr_cache:
cached_ts2, cached_result2 = analyser._ocr_cache[cache_key2]
if (now - cached_ts2) < analyser._ocr_cache_ttl:
no_award_confirm = cached_result2
else:
t1 = time.time()
analyser.cut_pic(340, 1195, 740, 1355, "", "无奖励确认")
no_award_confirm = analyser.operation.analyse_pic_word("无奖励确认")
analyser._ocr_cache[cache_key2] = (time.time(), no_award_confirm)
dur2 = time.time() - t1
else:
t1 = time.time()
analyser.cut_pic(340, 1200, 730, 1350, "", "无奖励确认")
no_award_confirm = analyser.operation.analyse_pic_word("无奖励确认")
analyser._ocr_cache[cache_key2] = (time.time(), no_award_confirm)
dur2 = time.time() - t1
try:
dur2
except NameError:
dur2 = 0.0
try:
brief2 = (no_award_confirm or "").replace("\n", " ")[:140]
except Exception:
brief2 = ""
try:
analyser._log_kv(
"开奖诊断",
"no_award_confirm",
brief=brief2,
ocr_dur=round(dur2, 2),
)
except Exception:
pass
if "我知道了" in (no_award_confirm or ""):
return 1
elif "领取并使用" in (no_award_confirm or ""):
return 2
return 3
elif "抽中福袋" in (lucky_draw_result or ""):
try:
analyser._log_kv("开奖诊断", "恭喜中奖了", result_text=brief)
except Exception:
pass
# 速度优化:优先检测第一个常见位置,命中即返回
y_value = [1438, 1490]
for y in y_value:
cache_key3 = (f"have_read_user_agreement_{y}", 0)
have_read_user_agreement = None
dur3 = 0.0
if cache_key3 in analyser._ocr_cache:
cached_ts3, cached_result3 = analyser._ocr_cache[cache_key3]
if (now - cached_ts3) < analyser._ocr_cache_ttl:
have_read_user_agreement = cached_result3
if not have_read_user_agreement:
t2 = time.time()
analyser.cut_pic(280, y - 35, 670, y + 35, "", f"已阅读用户协议_{y}")
have_read_user_agreement = analyser.operation.analyse_pic_word(f"已阅读用户协议_{y}")
analyser._ocr_cache[cache_key3] = (time.time(), have_read_user_agreement)
dur3 = time.time() - t2
try:
brief3 = (have_read_user_agreement or "").replace("\n", " ")[:140]
except Exception:
brief3 = ""
try:
analyser._log_kv(
"开奖诊断",
"have_read_user_agreement",
brief=brief3,
ocr_dur=round(dur3, 2),
y=y,
)
except Exception:
pass
if "已阅读" in (have_read_user_agreement or ""):
return y
return False
def check_have_reward_notice_confirm(analyser) -> bool:
"""判断是否有领奖的二次确认提醒。"""
analyser._shot_and_diagnose("领奖确认", "识别是否有我知道了", analyser.device_id)
analyser.cut_pic(370, 1350, 680, 1440, "", "奖励通知确认")
reward_notice_confirm = analyser.operation.analyse_pic_word("奖励通知确认", 1)
if "我知道了" in (reward_notice_confirm or ""):
try:
analyser._log_kv(
"开奖诊断",
"存在奖品领取提醒",
text=(reward_notice_confirm or "")[:80],
)
except Exception:
pass
return True
return False
def check_in_order_confirm_page(analyser) -> bool:
"""判断是否在中奖下单后的购买成功的页面。"""
analyser._shot_and_diagnose("下单结果", "识别是否购买成功页", analyser.device_id)
analyser.cut_pic(370, 120, 700, 220, "", "订单确认")
order_confirm = analyser.operation.analyse_pic_word("订单确认")
if "购买成功" in (order_confirm or ""):
try:
analyser._log_kv(
"开奖诊断",
"订单确认页面:购买成功",
text=(order_confirm or "")[:80],
)
except Exception:
pass
return True
return False
def get_reward(analyser, reward_y: int = 0) -> None:
"""中奖后领奖然后返回。"""
analyser.operation.save_reward_pic(analyser.device_id)
try:
analyser._log_kv("领奖", "保存中奖截图", step="save_reward_pic")
except Exception:
pass
analyser.click(243, reward_y) # 勾选协议
analyser.operation.delay(1)
analyser.click(540, reward_y - 140) # 点击领取
try:
analyser._log_kv("领奖", "勾选协议并点击领取", reward_y=reward_y)
except Exception:
pass
analyser.operation.delay(5)
analyser.click(886, 2170) # 点击下单
try:
analyser._log_kv("领奖", "点击下单")
except Exception:
pass
analyser.operation.delay(10)
# 循环检查下单结果,增加保护避免极端死循环
max_checks = 5
checks = 0
while checks < max_checks and check_in_order_confirm_page(analyser):
checks += 1
analyser.operation.click_back(analyser.device_id)
try:
analyser._log_kv("领奖", "点击回退按钮", check_round=checks)
except Exception:
pass
analyser.operation.delay(4)
try:
analyser._log_kv("领奖", "下完单返回到直播间", checks=checks)
except Exception:
pass
analyser.operation.delay(4)
if check_lucky_draw_result(analyser):
try:
analyser._log_kv("领奖", "领奖弹窗未关闭,准备关闭")
except Exception:
pass
analyser.click(540, reward_y + 180)
try:
analyser._log_kv(
"领奖",
"点击关闭领奖弹窗",
x=540,
y=(reward_y + 180) * analyser.resolution_ratio_y // 2400,
)
except Exception:
pass
analyser.operation.delay(2)
analyser.operation.save_reward_pic(analyser.device_id)
try:
analyser._log_kv("领奖", "保存关闭领奖弹窗后的截图")
except Exception:
pass
analyser.operation.click_back(analyser.device_id)
analyser.operation.delay(2)
if check_have_reward_notice_confirm(analyser):
try:
analyser._log_kv("领奖", "提醒领奖弹窗未关闭,点击我知道了")
except Exception:
pass
analyser.click(540, 1400)
analyser.operation.delay(2)
try:
analyser._log_kv("领奖", "关闭中奖提醒后等待", wait_seconds=30)
except Exception:
pass
analyser.operation.delay(30)
def should_skip_fudai_by_config(analyser: Any, detail: Tuple) -> Tuple[bool, str]:
"""根据 config.json 中的阈值与关键词,判断是否应丢弃当前福袋。
返回:
(skip, reason) 其中 reason 为简单文字原因,便于日志查看。
"""
if not isinstance(detail, tuple) or len(detail) < 6:
return False, "detail_tuple_invalid"
content = str(detail[0] or "")
time_text = str(detail[1] or "")
try:
participants = int(detail[2] or 0)
except Exception:
participants = 0
try:
copies = int(detail[3] or 0)
except Exception:
copies = 0
try:
prize_value_yuan = float(detail[4] or 0.0)
except Exception:
prize_value_yuan = 0.0
cd_start, cd_end = None, None
if isinstance(detail[5], (tuple, list)) and len(detail[5]) >= 2:
cd_start, cd_end = detail[5][0], detail[5][1]
now = time.time()
remain_seconds = None
try:
if isinstance(cd_end, (int, float)) and cd_end > now:
remain_seconds = float(cd_end - now)
except Exception:
remain_seconds = None
# 读取配置阈值
try:
min_value = float(getattr(analyser, "min_prize_value_yuan", 0.0) or 0.0)
except Exception:
min_value = 0.0
try:
min_participants = int(getattr(analyser, "min_participants", 0) or 0)
except Exception:
min_participants = 0
try:
max_participants = int(getattr(analyser, "max_participants", 0) or 0)
except Exception:
max_participants = 0
try:
skip_if_lt = int(getattr(analyser, "skip_if_countdown_lt_seconds", 0) or 0)
except Exception:
skip_if_lt = 0
# 1) 价值过滤
if min_value > 0 and prize_value_yuan > 0 and prize_value_yuan < min_value:
return True, f"value_lt_{min_value}"
# 2) 参与人数过滤
if min_participants > 0 and participants and participants < min_participants:
return True, f"participants_lt_{min_participants}"
if max_participants > 0 and participants and participants > max_participants:
return True, f"participants_gt_{max_participants}"
# 3) 倒计时过滤
if skip_if_lt > 0 and remain_seconds is not None and remain_seconds < skip_if_lt:
return True, f"countdown_lt_{skip_if_lt}"
# 4) 内容关键词过滤(复用 check_contain 逻辑)
if check_contain(analyser, content):
return True, "content_not_wanted"
try:
analyser._log_kv(
"筛选",
"should_skip_fudai_by_config 通过",
content=content[:60] if content else "",
participants=participants,
copies=copies,
value_yuan=prize_value_yuan,
remain_seconds=int(remain_seconds) if remain_seconds is not None else None,
)
except Exception:
pass
return False, "ok"
def basic_join_decision(analyser: Any, detail: Tuple) -> bool:
"""基础版参与决策:根据配置筛选福袋,满足条件则尝试参与抽奖。
返回:
True 表示已经参与或已处理完成;
False 表示未参与(例如被配置筛掉或参与失败)。
"""
skip, reason = should_skip_fudai_by_config(analyser, detail)
if skip:
try:
analyser._log_kv(
"参与",
"basic_join_decision:按配置丢弃福袋",
reason=reason,
)
except Exception:
pass
# 丢弃时,尽量关闭详情,避免停留在详情页
try:
analyser.click(500, 470)
except Exception:
try:
analyser.operation.click_back(analyser.device_id)
except Exception:
pass
try:
analyser._log_kv("参与", "basic_join_decision:已关闭详情", action="close_detail")
except Exception:
pass
# 如果是“内容不想要”的原因,且允许切换直播间,则立即切换到下一个直播间
try:
needswitch = bool(getattr(analyser, "needswitch", True))
except Exception:
needswitch = True
if reason == "content_not_wanted" and needswitch:
try:
analyser._log_kv("参与", "basic_join_decision:内容不想要且允许切房,立即切换直播间")
except Exception:
pass
try:
# 优先使用封装好的上滑切房方法
if hasattr(analyser, "swipe_up_to_next_live"):
analyser.swipe_up_to_next_live()
else:
# 兜底:直接调用底层 swipe(基于 1080x2400 基准坐标)
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
start_x = 540 * rx // 1080
start_y = 1700 * ry // 2400
end_x = 540 * rx // 1080
end_y = 700 * ry // 2400
analyser.operation.swipe(analyser.device_id, start_x, start_y, end_x, end_y, 300)
except Exception:
# 切房失败不影响主流程,直接返回
pass
return False
# 未被筛掉:尝试参与抽奖
try:
analyser._log_kv("参与", "basic_join_decision:尝试参与抽奖")
except Exception:
pass
return attend_choujiang(analyser)