from __future__ import annotations import os import random import time def yanghao_idle(analyser, total_seconds: int) -> None: """养号等待。 - 若启用 `yanghao_enable_actions`:停留期间可按概率点爱心/收藏,停留结束后自动滑动。 - 否则:保持原有“分段等待 + 可选轻微滑动 + 定时休息”。 """ if total_seconds <= 0: return if getattr(analyser, "yanghao_enable_actions", False): # 循环:持续检测图标;都未检测到则立刻下滑继续;检测到则停留 10-15 秒并按概率执行动作,然后再下滑。 need_settle_after_swipe = False def _step_shot(label: str) -> None: # 环境变量优先:YANGHAO_STEP_SCREENSHOT=1 try: env_on = bool(str(os.environ.get("YANGHAO_STEP_SCREENSHOT", "")).strip()) except Exception: env_on = False if env_on or bool(getattr(analyser, "yanghao_step_screenshot", False)): analyser._save_step_screenshot(label) def _do_swipe_next() -> None: nonlocal need_settle_after_swipe _step_shot("before_swipe") dist = int(getattr(analyser, "yanghao_after_dwell_swipe_distance_px", 900) or 900) if dist < 200: dist = 200 if dist > 1400: dist = 1400 x = int(1080 * 0.7) y_mid = int(2400 * 0.55) if str(getattr(analyser, "yanghao_after_dwell_swipe_direction", "up") or "up").lower() == "up": # up:上滑切换到下一个内容(手指从下往上) y1 = y_mid + int(dist / 2) y2 = y_mid - int(dist / 2) else: # down:下滑回到上一个内容(手指从上往下) y1 = y_mid - int(dist / 2) y2 = y_mid + int(dist / 2) analyser.swipe(x, y1, x, y2, 180) print("[养号] 动作: 下滑/切换 direction={} dist={}px".format(getattr(analyser, "yanghao_after_dwell_swipe_direction", "up"), dist)) _step_shot("after_swipe") # 标记:下滑后下一轮截图/检测前需要随机等待,避免动画未结束影响识别 need_settle_after_swipe = True def _scaled_click(base_x: int, base_y: int) -> None: sx, sy = analyser._scale_from_base(int(base_x), int(base_y)) analyser.operation.click(analyser.device_id, sx, sy) def _human_delay(min_s: float = 0.4, max_s: float = 1.2) -> None: try: mn = float(min_s) mx = float(max_s) if mx < mn: mx = mn analyser.operation.delay(random.uniform(mn, mx)) except Exception: analyser.operation.delay(1) def _detect_icon(action: str, base_x: int, base_y: int, template_path: str, roi_label: str) -> tuple[bool, float]: nonlocal need_settle_after_swipe if need_settle_after_swipe: # 下滑后:先随机等一下,再进行截图与模板匹配 _human_delay(1.0, 2.2) need_settle_after_swipe = False _step_shot(f"before_detect_{roi_label}") if int(base_x or 0) <= 0 or int(base_y or 0) <= 0: return False, 0.0 # ROI 中心点允许独立偏移(仅影响截图检测,不影响点击坐标) off_x = 0 off_y = 0 if action == "爱心": off_x = int(getattr(analyser, "yanghao_like_roi_offset_x", 0) or 0) off_y = int(getattr(analyser, "yanghao_like_roi_offset_y", 0) or 0) elif action == "收藏": off_x = int(getattr(analyser, "yanghao_fav_roi_offset_x", 0) or 0) off_y = int(getattr(analyser, "yanghao_fav_roi_offset_y", 0) or 0) sx, sy = analyser._scale_from_base(int(base_x), int(base_y)) if off_x or off_y: try: sx = int(sx) + int(off_x * int(getattr(analyser, "resolution_ratio_x", 1080)) / 1080) sy = int(sy) + int(off_y * int(getattr(analyser, "resolution_ratio_y", 2400)) / 2400) except Exception: pass roi_path = analyser._capture_roi_screenshot(roi_label, sx, sy, int(getattr(analyser, "yanghao_icon_roi_size", 180) or 180)) if not template_path or not os.path.exists(template_path): print(f"[养号] 检测: {action} -> 失败(缺少模板文件:{template_path})") return False, 0.0 detected, score = analyser._match_icon_template(roi_path, template_path) print(f"[养号] 检测: {action} -> {'检测到' if detected else '未检测到'}(score={score:.3f})") return detected, score use_icon = ( int(getattr(analyser, "yanghao_like_icon_x", 0) or 0) > 0 and int(getattr(analyser, "yanghao_like_icon_y", 0) or 0) > 0 and int(getattr(analyser, "yanghao_fav_icon_x", 0) or 0) > 0 and int(getattr(analyser, "yanghao_fav_icon_y", 0) or 0) > 0 ) start_ts = time.time() last_desktop_check_ts = 0.0 while True: if time.time() - start_ts >= total_seconds: return # 定期检测是否在桌面(每5秒检测一次,避免过于频繁) now_ts = time.time() check_interval = 5.0 if now_ts - last_desktop_check_ts >= check_interval: last_desktop_check_ts = now_ts try: if yanghao_check_and_recover_from_desktop(analyser): print("[养号] 检测到在桌面,已尝试恢复") analyser.operation.delay(1) except Exception: pass if not use_icon: # 未配置图标检测就退化为“原地等待” analyser.operation.delay(int(max(1, total_seconds))) return like_ok, _like_score = _detect_icon( "爱心", int(getattr(analyser, "yanghao_like_icon_x", 0) or 0), int(getattr(analyser, "yanghao_like_icon_y", 0) or 0), getattr(analyser, "yanghao_like_template_path", "") or "", "action_like_roi", ) fav_ok, _fav_score = _detect_icon( "收藏", int(getattr(analyser, "yanghao_fav_icon_x", 0) or 0), int(getattr(analyser, "yanghao_fav_icon_y", 0) or 0), getattr(analyser, "yanghao_fav_template_path", "") or "", "action_fav_roi", ) if (not like_ok) and (not fav_ok): # 两个都没检测到:立刻下滑继续检测 _human_delay(0.3, 0.8) _do_swipe_next() # 下滑后的延迟在下一轮截图/检测前统一处理 continue # 检测到其中任意一个:在这里停留 10-15 秒,并按概率执行动作 dwell = int(random.randint(10, 15)) print(f"[养号] 检测到图标,停留 {dwell}s 后再切换") # 仅当“同时检测到爱心+收藏”时,按概率触发评论动作 if ( like_ok and fav_ok and float(getattr(analyser, "yanghao_comment_prob", 0) or 0) > 0 and random.random() < float(getattr(analyser, "yanghao_comment_prob", 0) or 0) ): comment_texts = getattr(analyser, "yanghao_comment_texts", None) if comment_texts: msg = random.choice(comment_texts) print("[养号] 动作: 评论(prob={}) -> {}".format(getattr(analyser, "yanghao_comment_prob", 0), msg)) try: _step_shot("comment_before_open") _scaled_click(int(getattr(analyser, "yanghao_comment_btn_x", 0) or 0), int(getattr(analyser, "yanghao_comment_btn_y", 0) or 0)) _human_delay(0.8, 1.6) _step_shot("comment_after_open") _scaled_click(int(getattr(analyser, "yanghao_comment_input_x", 0) or 0), int(getattr(analyser, "yanghao_comment_input_y", 0) or 0)) _human_delay(0.6, 1.2) _step_shot("comment_after_focus") # 输入随机文本(优先 ADBKeyboard;否则走剪贴板/兜底) ix, iy = analyser._scale_from_base( int(getattr(analyser, "yanghao_comment_input_x", 0) or 0), int(getattr(analyser, "yanghao_comment_input_y", 0) or 0), ) prev_ime = "" try: ok_ime, prev_ime, _adb_ime = analyser.operation.ensure_adbkeyboard_ime(analyser.device_id) except Exception: ok_ime = False ok_input = False if ok_ime: try: ok_input = analyser.operation.input_text_adbkeyboard(analyser.device_id, msg, restore_prev=False) except Exception: ok_input = False if not ok_input: ok_input = bool(analyser.operation.paste_text_at(analyser.device_id, ix, iy, msg)) print(f"[养号] 评论输入{'成功' if ok_input else '失败'}") _step_shot("comment_after_input") _human_delay(0.6, 1.4) # 发送:改为键盘确认(优先 ADBKeyBoard 的 SEND editor action;兜底 Enter) try: analyser.operation.confirm_from_keyboard(analyser.device_id) except Exception: pass _human_delay(0.8, 1.6) _step_shot("comment_after_send") _scaled_click(int(getattr(analyser, "yanghao_comment_close_x", 0) or 0), int(getattr(analyser, "yanghao_comment_close_y", 0) or 0)) _human_delay(0.6, 1.2) _step_shot("comment_after_close") # 恢复原输入法(如果之前切换过) if prev_ime: try: analyser.operation.restore_ime(analyser.device_id, prev_ime) except Exception: pass except Exception as e: print(f"[养号] 动作: 评论失败:{e}") else: print("[养号] 动作: 评论(prob={}) -> 跳过(未配置文案列表)".format(getattr(analyser, "yanghao_comment_prob", 0))) if like_ok and float(getattr(analyser, "yanghao_like_prob", 0) or 0) > 0 and random.random() < float(getattr(analyser, "yanghao_like_prob", 0) or 0): _human_delay(0.3, 0.9) _step_shot("before_like_click") _scaled_click(int(getattr(analyser, "yanghao_like_icon_x", 0) or 0), int(getattr(analyser, "yanghao_like_icon_y", 0) or 0)) _human_delay(0.2, 0.6) _step_shot("after_like_click") print("[养号] 动作: 点爱心(prob={})".format(getattr(analyser, "yanghao_like_prob", 0))) else: print("[养号] 动作: 跳过点爱心(prob={})".format(getattr(analyser, "yanghao_like_prob", 0))) if fav_ok and float(getattr(analyser, "yanghao_fav_prob", 0) or 0) > 0 and random.random() < float(getattr(analyser, "yanghao_fav_prob", 0) or 0): _human_delay(0.3, 0.9) _step_shot("before_fav_click") _scaled_click(int(getattr(analyser, "yanghao_fav_icon_x", 0) or 0), int(getattr(analyser, "yanghao_fav_icon_y", 0) or 0)) _human_delay(0.2, 0.6) _step_shot("after_fav_click") print("[养号] 动作: 点收藏(prob={})".format(getattr(analyser, "yanghao_fav_prob", 0))) else: print("[养号] 动作: 跳过点收藏(prob={})".format(getattr(analyser, "yanghao_fav_prob", 0))) # 停留 analyser.operation.delay(dwell) # 接着下滑 _human_delay(0.4, 1.0) _do_swipe_next() # 下滑后的延迟在下一轮截图/检测前统一处理 # 理论上不会走到这里 return # 不启用动作时,采用原有“分段等待 + 桌面检测”逻辑 start_ts = time.time() last_desktop_check_ts = 0.0 while True: now = time.time() elapsed = now - start_ts if elapsed >= total_seconds: return # 定期检测是否在桌面(每5秒检测一次,避免过于频繁) check_interval = 5.0 if now - last_desktop_check_ts >= check_interval: last_desktop_check_ts = now try: if yanghao_check_and_recover_from_desktop(analyser): print("[养号] 检测到在桌面,已尝试恢复") analyser.operation.delay(1) except Exception: pass # 分段等待(避免一次 sleep 太久,看起来更自然) remaining = int(total_seconds - (time.time() - start_ts)) if remaining <= 0: return chunk = 10 if remaining > 10 else remaining analyser.operation.delay(chunk) def yanghao_try_tap_text_or_fallback( analyser, text: str, fx: int, fy: int, contains: bool = True, wait_after: float = 1.2, use_fallback: bool = True, tap_timeout: float = 1.5, ) -> bool: ok = False # 测试模式下尽量快(避免用户以为卡死) if str(os.environ.get("FAST_UI", "")).strip() in ("1", "true", "True"): wait_after = 0.2 try: ok = analyser.operation.tap_by_text(analyser.device_id, text, contains=contains, timeout_seconds=tap_timeout) except Exception: ok = False if ok: print("[养号] 点击 '{}':UI 命中".format(text)) else: if use_fallback: print("[养号] 点击 '{}':UI 未命中,坐标兜底 ({},{})".format(text, fx, fy)) analyser.click(int(fx), int(fy)) ok = True else: print("[养号] 点击 '{}':UI 未命中,跳过兜底".format(text)) if wait_after and wait_after > 0: analyser.operation.delay(wait_after) return ok def yanghao_try_multi_texts( analyser, texts: list[str], contains: bool = True, wait_after: float = 1.2, tap_timeout: float = 2.5, ) -> bool: """依次尝试多组文案(无坐标兜底)。""" for idx, t in enumerate(texts, 1): ok = yanghao_try_tap_text_or_fallback( analyser, t, 0, 0, contains=contains, wait_after=wait_after if idx == len(texts) else 0.0, use_fallback=False, tap_timeout=tap_timeout, ) if ok: return True return False def yanghao_tap_close_icon(analyser) -> None: """仅使用系统返回键退出当前页(不再尝试坐标点击)。""" print("[养号] 发送系统返回键 1/1") analyser.operation.click_back(analyser.device_id) analyser.operation.delay(0.8) def yanghao_check_and_recover_from_desktop(analyser) -> bool: """检测是否在桌面,如果是则尝试恢复回到抖音。 返回 True 表示检测到桌面并已尝试恢复,False 表示不在桌面或无需处理。 """ try: top_pkg = "" try: top_pkg = str(analyser.operation.get_top_package(analyser.device_id) or "") except Exception: top_pkg = "" # 如果包名存在且不是抖音相关包,说明可能在桌面或其他应用 if top_pkg and ("aweme" not in top_pkg and "douyin" not in top_pkg and "com.ss.android.ugc" not in top_pkg): print(f"[养号] 检测到不在抖音前台(前台包: {top_pkg}),尝试恢复") try: # 点击桌面图标的预设坐标 try: analyser.click(int(getattr(analyser, "desktop_icon_x", 0) or 0), int(getattr(analyser, "desktop_icon_y", 0) or 0)) except Exception: # 退化为默认硬编码位置 analyser.click(153, 260) analyser.operation.delay(2) # 点击直播与进入直播间的备用坐标 try: lx = int(getattr(analyser, "yanghao_fallback_live_x", 0) or 0) ly = int(getattr(analyser, "yanghao_fallback_live_y", 0) or 0) print(f"[养号] 点击直播按钮 -> ({lx},{ly})") analyser.click(lx, ly) analyser.operation.delay(1.5) except Exception: pass try: ex = int(getattr(analyser, "yanghao_fallback_enter_live_x", 0) or 0) ey = int(getattr(analyser, "yanghao_fallback_enter_live_y", 0) or 0) print(f"[养号] 点击进入直播间 -> ({ex},{ey}) 连点两次") analyser.click(ex, ey) analyser.operation.delay(0.6) analyser.click(ex, ey) analyser.operation.delay(2.0) except Exception: pass return True except Exception as e: print(f"[养号] 恢复失败: {e}") # 仍然返回 True 表示检测到了问题并尝试过恢复 return True except Exception: pass return False def yanghao_recommend_flow(analyser, total_seconds: int) -> None: """按你的需求执行:X 退出直播 -> 点推荐 -> 养号 -> 点直播 -> 点“点击进入直播间”。 如果识别不到文字,会使用坐标兜底;兜底仍失败时就退化成原地养号(不阻塞主流程)。 """ try: print("[养号] 推荐页流程:退出直播→推荐→养号→直播→进入直播间") capture_steps = str(os.environ.get("YANGHAO_STEP_SCREENSHOT", "")).strip() in ("1", "true", "True") if capture_steps: analyser._save_step_screenshot("start") print("[养号] Step1/5:系统返回键退出当前页") yanghao_tap_close_icon(analyser) print("[养号] step1: 已发送系统返回键(1次)") if capture_steps: analyser._save_step_screenshot("step1_close") print("[养号] Step2/5:再次发送返回键") analyser.operation.click_back(analyser.device_id) analyser.operation.delay(1.2) if capture_steps: analyser._save_step_screenshot("step2_back") print("[养号] Step3/5:在当前页停留/浏览 {} 秒".format(int(total_seconds))) # 养号前检测是否在桌面 if yanghao_check_and_recover_from_desktop(analyser): print("[养号] 检测到在桌面,已尝试恢复,继续养号流程") analyser.operation.delay(1) yanghao_idle(analyser, total_seconds) print("[养号] step3: 停留/浏览完成 {} 秒".format(total_seconds)) if capture_steps: analyser._save_step_screenshot("step3_browse_done") print("[养号] Step4/5:回到『直播』") fx = int(getattr(analyser, "yanghao_fallback_live_x", 0) or 0) fy = int(getattr(analyser, "yanghao_fallback_live_y", 0) or 0) print("[养号] step4: 坐标点击直播 -> ({},{})".format(fx, fy)) analyser.click(fx, fy) analyser.operation.delay(1.5) if capture_steps: analyser._save_step_screenshot("step4_live") print("[养号] Step5/5:点击『点击进入直播间』") ex = int(getattr(analyser, "yanghao_fallback_enter_live_x", 0) or 0) ey = int(getattr(analyser, "yanghao_fallback_enter_live_y", 0) or 0) print("[养号] step5: 坐标点击进入直播间 -> ({},{}) 连点两次".format(ex, ey)) analyser.click(ex, ey) analyser.operation.delay(0.6) analyser.click(ex, ey) analyser.operation.delay(2.0) if capture_steps: analyser._save_step_screenshot("step5_enter") print("[养号] 推荐页流程完成") except Exception as e: print("[养号] 推荐页流程失败,退化为原地养号:{}".format(e)) yanghao_idle(analyser, total_seconds) def yanghao_wait(analyser, total_seconds: int) -> None: if total_seconds <= 0: return if not bool(getattr(analyser, "yanghao_enabled", True)): # 用户要求:关闭养号后不要再等待太久,而是继续执行主流程 print(f"[养号] yanghao_enabled=0:跳过推荐页养号(不再等待 {int(total_seconds)}s,快速继续)") try: analyser.operation.delay(1) except Exception: pass return # 只保留推荐页养号;增加“间隔(冷却)”控制,避免触发过于频繁 try: now = time.time() interval = int(getattr(analyser, "yanghao_recommend_interval_seconds", 0) or 0) last_ts = float(getattr(analyser, "_yanghao_last_recommend_ts", 0.0) or 0.0) if interval > 0 and last_ts > 0: dt = now - last_ts if dt < interval: remain = int(interval - dt) print(f"[养号] 推荐页养号冷却中,剩余 {remain}s,跳过本次(改为等待 {int(total_seconds)}s)") analyser.operation.delay(int(max(1, total_seconds))) return analyser._yanghao_last_recommend_ts = time.time() except Exception: pass yanghao_recommend_flow(analyser, total_seconds) def yanghao_get_recommend_seconds(analyser) -> int: s = 60 try: s = int(getattr(analyser, "yanghao_recommend_duration_seconds", 60) or 60) except Exception: s = 60 if s < 1: s = 1 if s > 3600: s = 3600 return s def maybe_reset_after_swipes(analyser, needswitch: bool, swipe_times: int) -> int: """达到切换阈值后,在当前直播间页面回退到第一个直播间。""" if not needswitch: return swipe_times if not bool(getattr(analyser, "reset_after_swipes_enabled", False)): return swipe_times threshold = max(1, int(getattr(analyser, "reset_after_swipes", 0) or 0)) if swipe_times < threshold: return swipe_times print("已切换直播间{}次,达到阈值{}:下滑回退到第一个直播间".format(swipe_times, threshold)) # 回退次数按当前已切换次数来(更符合“回到本轮第一个直播间”的语义) back_times = int(swipe_times) if back_times <= 0: return 0 # 下滑回退:与上划切换相反方向 for i in range(back_times): analyser.swipe(760, 800, 760, 1600, 200) print("回退直播间 {}/{}".format(i + 1, back_times)) analyser.operation.delay(2) # 回到第一个后,计数归零 analyser.operation.delay(3) return 0