'commit'
This commit is contained in:
@@ -24,84 +24,59 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
async def check_and_close_ad(d):
|
||||
"""
|
||||
检测并关闭广告弹窗(仅使用 VL 视觉模型)
|
||||
检测并关闭广告弹窗(循环检测直至无广告)
|
||||
"""
|
||||
logger.info("开始检测广告弹窗 (VL方案)...")
|
||||
|
||||
# [已移除] 策略:先向上滑动一点,触发悬浮广告(如兔子)收起/躲避
|
||||
# 用户反馈:一滚动,兔子就藏起来了,导致无法检测到广告,因此取消此逻辑。
|
||||
# try:
|
||||
# w, h = d.window_size()
|
||||
# logger.info(f"执行微小滑动 (Swipe Up),尝试触发悬浮广告收起...")
|
||||
# # 从 70% 处滑到 50% 处,模拟手指上滑,页面内容上移
|
||||
# d.swipe(w * 0.5, h * 0.7, w * 0.5, h * 0.5, duration=0.3)
|
||||
# # 等待滑动动画结束和广告收起动画
|
||||
# await asyncio.sleep(0.5)
|
||||
# except Exception as e:
|
||||
# logger.warning(f"滑动操作异常: {e}")
|
||||
|
||||
# 1. 拍摄截图
|
||||
t1 = time.time()
|
||||
image_uuid = str(uuid.uuid4())
|
||||
# 使用相对路径: 基于当前脚本目录下的 Images 文件夹
|
||||
save_dir = TEMP_IMAGE_DIR
|
||||
screenshot_path = take_screenshot(d, image_uuid, save_dir=save_dir)
|
||||
logger.info(f"Step [广告检测截图] 耗时: {time.time() - t1:.4f}s")
|
||||
|
||||
# 2. 视觉大模型检测方案 (VL Model)
|
||||
try:
|
||||
window_size = d.window_size()
|
||||
device_info = {
|
||||
"width": window_size[0],
|
||||
"height": window_size[1],
|
||||
"productName": d.info.get('productName', 'unknown')
|
||||
}
|
||||
max_loops = 3 # 最大检测 3 轮,防止死循环
|
||||
for loop_idx in range(max_loops):
|
||||
logger.info(f"开始第 {loop_idx + 1} 轮广告检测...")
|
||||
|
||||
# 使用最新的 detect_ad_popup 方法
|
||||
ad_result = await ReadImageKit.detect_ad_popup(screenshot_path, device_info=device_info)
|
||||
if ad_result:
|
||||
x, y = ad_result["x"], ad_result["y"]
|
||||
ad_type = ad_result.get("ad_type")
|
||||
# 1. 拍摄截图
|
||||
t1 = time.time()
|
||||
image_uuid = str(uuid.uuid4())
|
||||
save_dir = TEMP_IMAGE_DIR
|
||||
screenshot_path = take_screenshot(d, image_uuid, save_dir=save_dir)
|
||||
logger.info(f"Step [广告检测截图] 耗时: {time.time() - t1:.4f}s")
|
||||
|
||||
# 2. 视觉大模型检测方案 (VL Model)
|
||||
try:
|
||||
window_size = d.window_size()
|
||||
device_info = {
|
||||
"width": window_size[0],
|
||||
"height": window_size[1],
|
||||
"productName": d.info.get('productName', 'unknown')
|
||||
}
|
||||
|
||||
logger.info(f"通过视觉大模型检测到广告关闭按钮: ({x}, {y}) [Type: {ad_type}]")
|
||||
|
||||
# 针对“新电兔AI”兔子广告的强力关闭策略
|
||||
if ad_type == "rabbit":
|
||||
logger.info(">>> 检测到顽固的兔子广告,执行强力双击策略...")
|
||||
d.double_click(x, y, duration=0.05)
|
||||
# 使用最新的 detect_ad_popup 方法
|
||||
ad_result = await ReadImageKit.detect_ad_popup(screenshot_path, device_info=device_info)
|
||||
if ad_result:
|
||||
x, y = ad_result["x"], ad_result["y"]
|
||||
ad_type = ad_result.get("ad_type")
|
||||
|
||||
# 处理“误触背景卡片”的逻辑
|
||||
logger.info(">>> 等待 3s 以确认是否进入了二级页面...")
|
||||
await asyncio.sleep(3.0)
|
||||
logger.info(f"检测到广告关闭按钮: ({x}, {y}) [Type: {ad_type}]")
|
||||
|
||||
# 检查是否还在列表页
|
||||
is_list_page = d(text="全城").exists or d(text="综合排序").exists or d(text="搜索场站").exists
|
||||
if not is_list_page:
|
||||
logger.info(">>> 检测到当前不在列表页,尝试执行返回操作...")
|
||||
d.press("back")
|
||||
await asyncio.sleep(1.5)
|
||||
if ad_type == "rabbit":
|
||||
logger.info(">>> 检测到兔子广告。根据最新策略,我们不再尝试关闭它,直接继续。")
|
||||
if os.path.exists(screenshot_path): os.remove(screenshot_path)
|
||||
return True
|
||||
else:
|
||||
logger.info(">>> 当前仍在列表页,无需返回。")
|
||||
logger.info(f">>> 正在点击关闭非兔子广告 ({ad_type})...")
|
||||
d.click(x, y)
|
||||
await asyncio.sleep(2.0) # 等待关闭动画
|
||||
|
||||
if os.path.exists(screenshot_path): os.remove(screenshot_path)
|
||||
# 继续下一轮循环,检查是否还有其他广告
|
||||
continue
|
||||
else:
|
||||
d.click(x, y)
|
||||
|
||||
if os.path.exists(screenshot_path):
|
||||
os.remove(screenshot_path)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"视觉大模型广告检测异常: {e}")
|
||||
logger.info("本轮未检测到广告。")
|
||||
if os.path.exists(screenshot_path):
|
||||
os.remove(screenshot_path)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"视觉大模型广告检测异常: {e}")
|
||||
if os.path.exists(screenshot_path): os.remove(screenshot_path)
|
||||
break
|
||||
|
||||
logger.info(f"VL 检测完成,未发现已知广告。")
|
||||
|
||||
# 如果未检测到广告,保留截图以便调试(保存为 debug_ad_failure.jpg)
|
||||
debug_path = os.path.join(os.path.dirname(screenshot_path), "debug_ad_failure.jpg")
|
||||
if os.path.exists(debug_path):
|
||||
os.remove(debug_path)
|
||||
if os.path.exists(screenshot_path):
|
||||
os.rename(screenshot_path, debug_path)
|
||||
logger.info(f"未检测到广告,截图已保存至: {debug_path} (请检查该图片是否真的包含广告)")
|
||||
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user