Files
iOSAI/script/ScriptManager.py

352 lines
13 KiB
Python
Raw Normal View History

2025-08-06 22:11:33 +08:00
import random
2025-08-08 22:08:10 +08:00
import threading
2025-08-06 22:11:33 +08:00
import time
2025-08-08 22:08:10 +08:00
from enum import Enum
2025-08-01 13:43:51 +08:00
import wda
import os
2025-08-05 15:41:20 +08:00
from Utils.AiUtils import AiUtils
2025-08-06 22:11:33 +08:00
from Utils.ControlUtils import ControlUtils
from Utils.LogManager import LogManager
2025-08-12 18:53:06 +08:00
from Entity.Variables import anchorList, removeModelFromAnchorList, prologueList
from Utils.Requester import Requester
2025-08-01 13:43:51 +08:00
# 脚本管理类
class ScriptManager():
2025-08-05 15:41:20 +08:00
# 单利对象
_instance = None # 类变量,用于存储单例实例
def __new__(cls):
# 如果实例不存在,则创建一个新实例
if cls._instance is None:
cls._instance = super(ScriptManager, cls).__new__(cls)
# 返回已存在的实例
return cls._instance
2025-08-01 13:43:51 +08:00
def __init__(self):
super().__init__()
2025-08-05 15:41:20 +08:00
self.initialized = True # 标记已初始化
2025-08-01 13:43:51 +08:00
# 养号
2025-08-06 22:11:33 +08:00
def growAccount(self, udid, event):
2025-08-01 13:43:51 +08:00
client = wda.USBClient(udid)
session = client.session()
2025-08-08 22:08:10 +08:00
session.appium_settings({"snapshotMaxDepth": 0})
# 先关闭Tik Tok
ControlUtils.closeTikTok(session, udid)
time.sleep(1)
2025-08-06 22:11:33 +08:00
2025-08-08 22:08:10 +08:00
# 重新打开Tik Tok
ControlUtils.openTikTok(session, udid)
2025-08-06 22:11:33 +08:00
time.sleep(3)
# 创建udid名称的目录
AiUtils.makeUdidDir(udid)
2025-08-06 22:11:33 +08:00
# 假设此时有个开关
while not event.is_set():
try:
img = client.screenshot()
filePath = f"resources/{udid}/bgv.png"
2025-08-06 22:11:33 +08:00
img.save(filePath)
LogManager.info("保存屏幕图像成功", udid)
print("保存了背景图")
time.sleep(1)
2025-08-06 22:11:33 +08:00
except Exception as e:
LogManager.error(e, udid)
2025-08-06 22:11:33 +08:00
print(e)
try:
# 判断视频类型
addX, addY = AiUtils.findImageInScreen("add", udid)
2025-08-11 22:06:48 +08:00
# 多次获取结果是否一致,如果有不一致的结果就切换视频
isSame = False
for i in range(2):
tx, ty = AiUtils.findImageInScreen("add", udid)
if addX == tx and addY == ty:
isSame = True
time.sleep(1)
else:
isSame = False
break
2025-08-06 22:11:33 +08:00
# 如果找到普通视频
if addX > 0 and isSame:
needLike = random.randint(0, 10)
# 查找首页按钮
homeButton = AiUtils.findHomeButton(udid)
if homeButton:
2025-08-07 21:37:46 +08:00
print("有首页按钮,查看视频")
videoTime = random.randint(5, 15)
time.sleep(videoTime)
2025-08-06 22:11:33 +08:00
# 百分之三的概率点赞
if needLike < 3:
print("点赞")
ControlUtils.clickLike(session, udid)
2025-08-06 22:11:33 +08:00
print("继续观看视频")
videoTime = random.randint(10, 30)
time.sleep(videoTime)
print("准备划到下一个视频")
client.swipe_up()
else:
2025-08-07 21:37:46 +08:00
print("找不到首页按钮。出错了")
else:
nextTime = random.randint(1, 5)
time.sleep(nextTime)
client.swipe_up()
except Exception as e:
print(f"发生异常:{e}")
client.swipe_up()
2025-08-06 22:11:33 +08:00
2025-08-08 22:08:10 +08:00
# 观看直播
2025-08-11 22:06:48 +08:00
def watchLiveForGrowth(self, udid, event):
client = wda.USBClient(udid)
session = client.session()
session.appium_settings({"snapshotMaxDepth": 15})
# 先关闭Tik Tok
ControlUtils.closeTikTok(session, udid)
time.sleep(1)
# 重新打开Tik Tok
ControlUtils.openTikTok(session, udid)
time.sleep(3)
# 进入直播
live_button = session(xpath='//XCUIElementTypeButton[@name="直播"]')
if live_button.exists:
live_button.click()
time.sleep(20)
size = session.window_size()
width, height = size.width, size.height
print(f"屏幕的宽高是:{width},{height}")
2025-08-08 22:08:10 +08:00
2025-08-11 22:06:48 +08:00
# 可选:重新拉起 session规避偶发 Stale 会话
session = client.session()
# session.appium_settings({"snapshotMaxDepth": 25})
while not event.is_set():
try:
time.sleep(3)
# 如果处于 PK分数条直接划走
if session(xpath='//XCUIElementTypeOther[@name="kGBLInteractionViewMatchScoreBar"]').exists:
print("✅ 当前是 PK跳过")
session.swipe_up()
continue
# 数直播显示区域窗口(主画面 + 连麦小窗)
count = AiUtils.count_add_by_xml(session)
print(f"检测到直播显示区域窗口数:{count}")
if count > 1:
print("❌ 多窗口(有人连麦/分屏),划走")
session.swipe_up()
continue
else:
print("✅ 单窗口(只有一个主播),(目前是20%概率)开始点赞")
# 点赞仍保留中途转PK的保护
if random.random() >= 0.89:
print("开始点赞")
for _ in range(random.randint(10, 30)):
if session(xpath='//XCUIElementTypeOther[@name="kGBLInteractionViewMatchScoreBar"]').exists:
print("❗ 中途开始 PK停止点赞并跳过")
session.swipe_up()
break
x = width // 3 + random.randint(-10, 10)
y = height // 3 + random.randint(10, 20)
print("双击坐标:", x, y)
session.double_tap(x, y)
print("--------------------------------------------")
time.sleep(random.randint(100, 300))
session.swipe_up()
except Exception as e:
print("循环异常,重试:", repr(e))
# 轻量恢复:重新获取 session避免因为快照或元素句柄失效卡死
try:
session = client.session()
except Exception:
time.sleep(2)
session = client.session()
# 关注打招呼以及回复主播消息
2025-08-08 22:08:10 +08:00
def greetNewFollowers(self, udid, needReply, event):
client = wda.USBClient(udid)
session = client.session()
# 先关闭Tik Tok
ControlUtils.closeTikTok(session, udid)
time.sleep(1)
# 重新打开Tik Tok
ControlUtils.openTikTok(session, udid)
time.sleep(3)
2025-08-12 18:53:06 +08:00
# 设置查找深度
session.appium_settings({"snapshotMaxDepth": 15})
2025-08-08 22:08:10 +08:00
# 点击搜索按钮
2025-08-11 22:06:48 +08:00
ControlUtils.clickSearch(session)
# 返回上一步
2025-08-12 22:03:08 +08:00
def goBack(count):
# 如需回复消息
if needReply:
count += 1
for i in range(count):
ControlUtils.clickBack(session)
time.sleep(2)
2025-08-11 22:06:48 +08:00
# 循环条件。1、 循环关闭 2、 数据处理完毕
while not event.is_set() or len(anchorList) > 0:
2025-08-12 22:03:08 +08:00
if needReply:
print("如果需要回复主播消息。走此逻辑")
if AiUtils.getUnReadMsgCount(session) > 0:
# 此处调用另外一个方法处理
# 设置查找深度
session.appium_settings({"snapshotMaxDepth": 15})
# 点击搜索按钮
ControlUtils.clickSearch(session)
# 查找输入框
input = session.xpath('//XCUIElementTypeSearchField')
# 如果找到了输入框,就点击并且输入内容
input.click()
# 稍作停顿
time.sleep(1)
2025-08-12 18:53:06 +08:00
2025-08-11 22:06:48 +08:00
# 获取一个主播
anchor = anchorList[0]
aid = anchor.anchorId
2025-08-12 18:53:06 +08:00
anchorCountry = anchor.country
2025-08-11 22:06:48 +08:00
2025-08-12 18:53:06 +08:00
input.clear_text()
2025-08-11 22:06:48 +08:00
time.sleep(2)
# 输入主播id
2025-08-08 22:08:10 +08:00
input.set_text(aid + "\n")
# 切换UI查找深度
session.appium_settings({"snapshotMaxDepth": 25})
2025-08-11 22:06:48 +08:00
# 定位 "关注" 按钮 通过关注按钮的位置点击主播首页
2025-08-12 22:03:08 +08:00
follow_button = session.xpath('//XCUIElementTypeOther[@name="zhang1231511"]//XCUIElementTypeButton[@enabled="true" and @visible="true"]')
time.sleep(2)
if follow_button.exists:
2025-08-11 22:06:48 +08:00
print("找到关注按钮!")
x = follow_button.bounds.x - 200
y = follow_button.bounds.y
client.click(x, y)
else:
2025-08-12 22:03:08 +08:00
goBack(1)
removeModelFromAnchorList(anchor)
2025-08-11 22:06:48 +08:00
print("未找到关注按钮")
2025-08-12 22:03:08 +08:00
continue
2025-08-08 22:08:10 +08:00
time.sleep(3)
2025-08-11 22:06:48 +08:00
# 找到并点击第一个视频
cellClickResult = ControlUtils.clickFirstVideoFromDetailPage(session)
2025-08-08 22:08:10 +08:00
# 观看主播视频
def viewAnchorVideo():
2025-08-11 22:06:48 +08:00
print("开始查看视频")
2025-08-12 18:53:06 +08:00
count = 2
2025-08-12 22:03:08 +08:00
while count != 0:
time.sleep(5)
2025-08-11 22:06:48 +08:00
print("条件满足,继续查看")
img = client.screenshot()
time.sleep(1)
filePath = f"resources/{udid}/bgv.png"
img.save(filePath)
LogManager.info("保存屏幕图像成功", udid)
print("保存了背景图")
2025-08-12 22:03:08 +08:00
time.sleep(2)
# 查找add图标
2025-08-11 22:06:48 +08:00
addX, addY = AiUtils.findImageInScreen("add", udid)
if addX != -1:
r = ControlUtils.clickLike(session, udid)
2025-08-12 22:03:08 +08:00
# 点赞成功。总数量减1否则就滑到下一个视频。原因太多。不好判断。不如直接下一个视频
if r == True:
2025-08-11 22:06:48 +08:00
count -= 1
else:
client.swipe_up()
else:
2025-08-12 22:03:08 +08:00
print("没找有效视频")
2025-08-11 22:06:48 +08:00
client.swipe_up()
2025-08-12 22:03:08 +08:00
break
2025-08-11 22:06:48 +08:00
# 假装看几秒视频
2025-08-12 22:03:08 +08:00
time.sleep(5)
2025-08-11 22:06:48 +08:00
client.swipe_up()
2025-08-12 22:03:08 +08:00
if count == 0:
ControlUtils.clickBack(session)
2025-08-11 22:06:48 +08:00
break
2025-08-12 22:03:08 +08:00
# 如果打开视频失败。说明该主播没有视频
if cellClickResult == True:
2025-08-11 22:06:48 +08:00
print("点击了视频")
2025-08-12 22:03:08 +08:00
session.appium_settings({"snapshotMaxDepth": 15})
time.sleep(2)
2025-08-11 22:06:48 +08:00
print("重新设置了匹配深度")
viewAnchorVideo()
2025-08-12 18:53:06 +08:00
time.sleep(2)
print("视频看完了")
session.appium_settings({"snapshotMaxDepth": 25})
2025-08-12 22:03:08 +08:00
# 点击关注按钮
followButton = AiUtils.getFollowButton(session)
if followButton.exists:
followButton.click()
else:
goBack(2)
return
2025-08-12 18:53:06 +08:00
time.sleep(2)
2025-08-12 22:03:08 +08:00
AiUtils.getSendMesageButton(session)
2025-08-12 18:53:06 +08:00
time.sleep(2)
2025-08-11 22:06:48 +08:00
2025-08-12 18:53:06 +08:00
# 查找聊天界面中的输入框节点
2025-08-11 22:06:48 +08:00
chatInput = session.xpath("//*[className='XCUIElementTypeTextView']")
if chatInput.exists:
print("找到了")
2025-08-12 18:53:06 +08:00
# 准备发送一条信息
chatInput.click()
time.sleep(2)
2025-08-12 22:03:08 +08:00
text = random.choice(prologueList)
msg = Requester.translation(text, anchorCountry)
chatInput.set_text(msg + "\n")
2025-08-12 18:53:06 +08:00
time.sleep(1)
2025-08-11 22:06:48 +08:00
else:
2025-08-12 22:03:08 +08:00
print("无法发送信息,换个主播")
# 接着下一个主播
goBack(3)
2025-08-11 22:06:48 +08:00
else:
2025-08-12 22:03:08 +08:00
print("没找到主播的第一个视频")
# 删除当前数据
removeModelFromAnchorList(anchor)
goBack(2)
2025-08-08 22:08:10 +08:00
2025-08-12 18:53:06 +08:00
def test(self, udid):
client = wda.USBClient(udid)
2025-08-12 22:03:08 +08:00
session = client.session()
session.appium_settings({"snapshotMaxDepth": 10})
2025-08-08 22:08:10 +08:00
2025-08-12 22:03:08 +08:00
print(client.source())
2025-08-08 22:08:10 +08:00
2025-08-12 22:03:08 +08:00
# manager = ScriptManager()
# manager.test("eca000fcb6f55d7ed9b4c524055214c26a7de7aa")
2025-08-06 22:11:33 +08:00
# manager.growAccount("eca000fcb6f55d7ed9b4c524055214c26a7de7aa")
2025-08-01 13:43:51 +08:00