Files
iOSAI/script/ScriptManager.py

186 lines
5.6 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-08 22:08:10 +08:00
from Entity.Variables import anchorList, removeModelFromAnchorList
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)
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
# 观看直播
def viewLive(self):
pass
# 关注打招呼
def greetNewFollowers(self, udid, needReply, 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)
# 点击搜索按钮
searchButton = ControlUtils.clickSearch(session)
if searchButton is not None:
searchButton.click()
else:
print("没找到搜索按钮")
return
# 搜索框
input = session.xpath('//XCUIElementTypeSearchField')
# 获取一个主播
anchor = anchorList[0]
aid = anchor.anchorId
# 如果找到了输入框,就点击并且输入内容
if input.exists:
input.click()
time.sleep(1)
input.set_text(aid + "\n")
# 切换UI查找深度
session.appium_settings({"snapshotMaxDepth": 25})
# 点击进入主播首页
session.xpath(
'(//XCUIElementTypeCollectionView[@name="TTKSearchCollectionComponent"]'
'//XCUIElementTypeCell'
'//XCUIElementTypeButton[following-sibling::XCUIElementTypeButton[@name="关注" or @label="关注"]])[1]'
).get(timeout=5).tap()
time.sleep(3)
# 向上划一点
r = client.swipe_up()
print(r)
# 分析页面节点
# print(session.source())
# 观看主播视频
def viewAnchorVideo():
pass
viewAnchorVideo()
else:
pass
# 功能待完善
# while not event.is_set() and len(anchorList) > 0:
# anchor = anchorList[0]
# aid = anchor.anchorId
#
# ControlUtils.clickSearch(session, udid)
#
# # 删除数据
# removeModelFromAnchorList(anchor)
# print(len(anchorList))
2025-08-06 22:11:33 +08:00
# manager = ScriptManager()
# manager.growAccount("eca000fcb6f55d7ed9b4c524055214c26a7de7aa")
2025-08-01 13:43:51 +08:00