Compare commits
4 Commits
12af466d94
...
e20d81144d
| Author | SHA1 | Date | |
|---|---|---|---|
| e20d81144d | |||
| b75c517488 | |||
| 3fcd72bfd5 | |||
| 648cdfc299 |
2
.idea/iOSAI.iml
generated
2
.idea/iOSAI.iml
generated
@@ -2,7 +2,7 @@
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.12" jdkType="Python SDK" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.12 (IOS-AI)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -3,5 +3,5 @@
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.12" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12" project-jdk-type="Python SDK" />
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (IOS-AI)" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
@@ -203,7 +203,7 @@ class AiUtils(object):
|
||||
print("1.找到了")
|
||||
return homeButton
|
||||
else:
|
||||
print("没找到")
|
||||
print("1.没找到")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(e)
|
||||
@@ -241,23 +241,34 @@ class AiUtils(object):
|
||||
# 获取关注按钮
|
||||
@classmethod
|
||||
def getFollowButton(cls, session: Client):
|
||||
followButton = session.xpath("//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[2]/Other[1]/Other[1]/Other[3]/Other[1]/Other[1]/Button[1]")
|
||||
# followButton = session.xpath("//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[2]/Other[1]/Other[1]/Other[3]/Other[1]/Other[1]/Button[1]")
|
||||
|
||||
followButton = session.xpath(
|
||||
'//XCUIElementTypeOther[@name="cta_social_interaction"]//XCUIElementTypeButton[@name="关注"]')
|
||||
|
||||
if followButton.exists:
|
||||
print("2.找到了")
|
||||
print("2.关注找到了")
|
||||
LogManager.info("2.关注找到了")
|
||||
return followButton
|
||||
else:
|
||||
print("没找到")
|
||||
print("2.关注没找到")
|
||||
print("2.关注没找到")
|
||||
return None
|
||||
|
||||
# 查找发消息按钮
|
||||
@classmethod
|
||||
def getSendMesageButton(cls, session: Client):
|
||||
msgButton = session.xpath("//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[2]/Other[1]/Other[1]/Other[3]/Other[1]/Other[1]")
|
||||
# msgButton = session.xpath("//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[2]/Other[1]/Other[1]/Other[3]/Other[1]/Other[1]")
|
||||
|
||||
msgButton = session.xpath(
|
||||
'//XCUIElementTypeOther[@name="cta_social_interaction"]//XCUIElementTypeButton[@name="发消息"]')
|
||||
if msgButton.exists:
|
||||
print("3.找到了")
|
||||
print("3.发消息按钮找到了")
|
||||
LogManager.info("3.发消息按钮找到了")
|
||||
return msgButton
|
||||
else:
|
||||
print("没找到")
|
||||
print("3.发消息按钮没找到")
|
||||
LogManager.info("3.发消息按钮没找到")
|
||||
return None
|
||||
|
||||
# 获取当前屏幕上的节点
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import re
|
||||
|
||||
import tidevice
|
||||
import wda
|
||||
from wda import Client
|
||||
from Utils.AiUtils import AiUtils
|
||||
from Utils.LogManager import LogManager
|
||||
@@ -139,3 +140,31 @@ class ControlUtils(object):
|
||||
else:
|
||||
print("没有找到主页的第一个视频")
|
||||
return False, num
|
||||
|
||||
@classmethod
|
||||
def clickFollow(cls, session, aid):
|
||||
# 1) 含“关注/已关注/Follow/Following”的首个 cell
|
||||
cell_xpath = (
|
||||
'(//XCUIElementTypeCollectionView[@name="TTKSearchCollectionComponent"]'
|
||||
'//XCUIElementTypeCell[.//XCUIElementTypeButton[@name="关注" or @name="Follow" or @name="已关注" or @name="Following"]])[1]'
|
||||
)
|
||||
cell = session.xpath(cell_xpath).get(timeout=5)
|
||||
|
||||
# 2) 先试“用户信息 Button”(label/name 里包含 aid)
|
||||
profile_btn_xpath = (
|
||||
f'{cell_xpath}//XCUIElementTypeButton[contains(@label, "{aid}") or contains(@name, "{aid}")]'
|
||||
)
|
||||
|
||||
try:
|
||||
profile_btn = session.xpath(profile_btn_xpath).get(timeout=3)
|
||||
profile_btn.click()
|
||||
except wda.WDAElementNotFoundError:
|
||||
# 3) 兜底:用“关注”按钮做锚点,向左偏移点击头像/用户名区域
|
||||
follow_btn_xpath = (
|
||||
f'{cell_xpath}//XCUIElementTypeButton[@name="关注" or @name="Follow" or @name="已关注" or @name="Following"]'
|
||||
)
|
||||
follow_btn = session.xpath(follow_btn_xpath).get(timeout=5)
|
||||
rect = follow_btn.bounds
|
||||
left_x = max(1, rect.x - 20)
|
||||
center_y = rect.y + rect.height // 2
|
||||
session.tap(left_x, center_y)
|
||||
|
||||
@@ -49,7 +49,7 @@ class ScriptManager():
|
||||
time.sleep(1)
|
||||
ControlUtils.openTikTok(session, udid)
|
||||
time.sleep(3)
|
||||
LogManager.info("养号重启tiktok")
|
||||
LogManager.info("养号重启tiktok", udid)
|
||||
AiUtils.makeUdidDir(udid)
|
||||
|
||||
# ========= 主循环 =========
|
||||
@@ -183,7 +183,6 @@ class ScriptManager():
|
||||
if live_button.exists:
|
||||
continue
|
||||
|
||||
|
||||
# 下滑一下
|
||||
client.swipe_up()
|
||||
|
||||
@@ -479,6 +478,7 @@ class ScriptManager():
|
||||
|
||||
# 关注打招呼以及回复主播消息
|
||||
def greetNewFollowers(self, udid, needReply, event):
|
||||
|
||||
client = wda.USBClient(udid)
|
||||
session = client.session()
|
||||
print(f"是否要自动回复消息:{needReply}")
|
||||
@@ -536,24 +536,8 @@ class ScriptManager():
|
||||
session.appium_settings({"snapshotMaxDepth": 23})
|
||||
|
||||
try:
|
||||
|
||||
# 查找“关注”按钮
|
||||
follow_btn = session.xpath(
|
||||
'//XCUIElementTypeButton[@name="关注" or @name="Follow" or @name="已关注" or @name="Following"]').get(
|
||||
timeout=5)
|
||||
|
||||
# 获取按钮位置和大小
|
||||
rect = follow_btn.bounds
|
||||
left_x = max(1, rect.x - 20) # 向左偏移 20px,确保不会点到屏幕外
|
||||
center_y = rect.y + rect.height // 2
|
||||
|
||||
# 打印调试信息
|
||||
print(f"关注按钮位置 x={rect.x}, y={rect.y}, width={rect.width}, height={rect.height}")
|
||||
print(f"即将点击的位置 x={left_x}, y={center_y}")
|
||||
|
||||
# 点击关注按钮左侧区域(通常是头像)
|
||||
session.tap(left_x, center_y)
|
||||
|
||||
# 点击关注按钮
|
||||
ControlUtils.clickFollow(session, aid)
|
||||
except wda.WDAElementNotFoundError:
|
||||
# 如果没有“关注”按钮,则不点击
|
||||
print("未找到‘关注’按钮,跳过点击。")
|
||||
@@ -596,6 +580,7 @@ class ScriptManager():
|
||||
time.sleep(2)
|
||||
# 查找add图标
|
||||
r = ControlUtils.clickLike(session, udid)
|
||||
|
||||
# 点赞成功。
|
||||
# if r == True:
|
||||
|
||||
@@ -628,8 +613,8 @@ class ScriptManager():
|
||||
followButton.click()
|
||||
else:
|
||||
LogManager.info("没找到关注按钮", udid)
|
||||
removeModelFromAnchorList(anchor)
|
||||
goBack(3)
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
continue
|
||||
|
||||
time.sleep(2)
|
||||
@@ -641,8 +626,8 @@ class ScriptManager():
|
||||
msgButton.click()
|
||||
else:
|
||||
print("没有识别出发消息按钮")
|
||||
removeModelFromAnchorList(anchor)
|
||||
goBack(3)
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
continue
|
||||
|
||||
time.sleep(3)
|
||||
@@ -666,6 +651,8 @@ class ScriptManager():
|
||||
|
||||
# 接着下一个主播
|
||||
# removeModelFromAnchorList(anchor)
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
|
||||
goBack(4)
|
||||
|
||||
else:
|
||||
@@ -697,6 +684,7 @@ class ScriptManager():
|
||||
time.sleep(3)
|
||||
|
||||
print("重新创建wda会话 防止wda会话失效")
|
||||
|
||||
client = wda.USBClient(udid)
|
||||
session = client.session()
|
||||
|
||||
@@ -738,9 +726,8 @@ class ScriptManager():
|
||||
|
||||
# 调整节点的深度为 7
|
||||
session.appium_settings({"snapshotMaxDepth": 7})
|
||||
el = session(xpath='//XCUIElementTypeButton[@name="a11y_vo_inbox"]')
|
||||
|
||||
print(111111111111111111111111)
|
||||
el = session(xpath='//XCUIElementTypeButton[@name="a11y_vo_inbox"]')
|
||||
|
||||
# 如果收件箱有消息 则进行点击
|
||||
if el.exists:
|
||||
@@ -751,7 +738,6 @@ class ScriptManager():
|
||||
session.appium_settings({"snapshotMaxDepth": 25})
|
||||
time.sleep(3)
|
||||
while True:
|
||||
|
||||
info_count = 0
|
||||
|
||||
# 创建新的会话
|
||||
@@ -776,7 +762,6 @@ class ScriptManager():
|
||||
LogManager.error(f"当前页面不再收件箱页面,重启", udid)
|
||||
raise Exception("当前页面不再收件箱页面,重启")
|
||||
|
||||
print(3333333333333333333333333333333)
|
||||
m = re.search(r'(\d+)', el.label) # 抓到的第一个数字串
|
||||
count = int(m.group(1)) if m else 0
|
||||
|
||||
@@ -818,7 +803,70 @@ class ScriptManager():
|
||||
']//XCUIElementTypeStaticText[@value and translate(@value,"0123456789","")=""]'
|
||||
)
|
||||
|
||||
print(44444444444444444444444444444444)
|
||||
try:
|
||||
# 如果 2 秒内找不到,会抛异常
|
||||
user_text = session.xpath(xp_badge_numeric).get(timeout=2.0)
|
||||
val = (user_text.info.get("value") or
|
||||
user_text.info.get("label") or
|
||||
user_text.info.get("name"))
|
||||
LogManager.info(f"用户未读数量:{val}", udid)
|
||||
except Exception:
|
||||
LogManager.warning("当前屏幕没有找到 用户 未读徽标数字", udid)
|
||||
print("当前屏幕没有找到 用户消息 未读徽标数字", udid)
|
||||
user_text = None
|
||||
info_count += 1
|
||||
|
||||
if user_text:
|
||||
user_text.tap()
|
||||
time.sleep(3)
|
||||
xml = session.source()
|
||||
msgs = AiUtils.extract_messages_from_xml(xml)
|
||||
# 检测出对方发的最后一条信息
|
||||
last_msg_text = next(item['text'] for item in reversed(msgs) if item['type'] == 'msg')
|
||||
# 向ai发送信息
|
||||
|
||||
# 获取主播的名称
|
||||
anchor_name = AiUtils.get_navbar_anchor_name(session)
|
||||
|
||||
# 找到输入框
|
||||
|
||||
sel = session.xpath("//TextView")
|
||||
|
||||
if anchor_name not in anchorWithSession:
|
||||
# 如果是第一次发消息(没有sessionId的情况)
|
||||
response = Requester.chatToAi({"msg": last_msg_text})
|
||||
aiResult = response['result']
|
||||
sessionId = response['session_id']
|
||||
|
||||
anchorWithSession[anchor_name] = sessionId
|
||||
# 找到输入框,输入ai返回出来的消息
|
||||
if sel.exists:
|
||||
sel.click() # 聚焦
|
||||
time.sleep(1)
|
||||
sel.clear_text()
|
||||
sel.set_text(aiResult + "\n")
|
||||
else:
|
||||
LogManager.error("找不到输入框,重启", udid)
|
||||
raise Exception("找不到输入框,重启")
|
||||
else:
|
||||
# 如果不是第一次发消息(证明存储的有sessionId)
|
||||
sessionId = anchorWithSession[anchor_name]
|
||||
response = Requester.chatToAi({"msg": last_msg_text, "sid": sessionId})
|
||||
aiResult = response['result']
|
||||
if sel.exists:
|
||||
sel.click() # 聚焦
|
||||
time.sleep(1)
|
||||
sel.clear_text()
|
||||
sel.set_text(aiResult + "\n")
|
||||
|
||||
LogManager.info(f"存储的sessionId:{anchorWithSession}", udid)
|
||||
time.sleep(1)
|
||||
# 返回
|
||||
ControlUtils.clickBack(session)
|
||||
|
||||
# 重新回到收件箱页面后,强制刷新节点
|
||||
session.appium_settings({"snapshotMaxDepth": 25})
|
||||
time.sleep(1)
|
||||
|
||||
try:
|
||||
# 如果 2 秒内找不到,会抛异常
|
||||
@@ -894,71 +942,6 @@ class ScriptManager():
|
||||
badge_text = None
|
||||
info_count += 1
|
||||
|
||||
try:
|
||||
# 如果 2 秒内找不到,会抛异常
|
||||
user_text = session.xpath(xp_badge_numeric).get(timeout=2.0)
|
||||
val = (user_text.info.get("value") or
|
||||
user_text.info.get("label") or
|
||||
user_text.info.get("name"))
|
||||
LogManager.info(f"用户未读数量:{val}", udid)
|
||||
except Exception:
|
||||
LogManager.warning("当前屏幕没有找到 用户 未读徽标数字", udid)
|
||||
print("当前屏幕没有找到 用户消息 未读徽标数字", udid)
|
||||
user_text = None
|
||||
info_count += 1
|
||||
|
||||
if user_text:
|
||||
user_text.tap()
|
||||
time.sleep(3)
|
||||
xml = session.source()
|
||||
msgs = AiUtils.extract_messages_from_xml(xml)
|
||||
# 检测出对方发的最后一条信息
|
||||
last_msg_text = next(item['text'] for item in reversed(msgs) if item['type'] == 'msg')
|
||||
# 向ai发送信息
|
||||
|
||||
# 获取主播的名称
|
||||
anchor_name = AiUtils.get_navbar_anchor_name(session)
|
||||
|
||||
# 找到输入框
|
||||
|
||||
sel = session.xpath("//TextView")
|
||||
|
||||
if anchor_name not in anchorWithSession:
|
||||
# 如果是第一次发消息(没有sessionId的情况)
|
||||
response = Requester.chatToAi({"msg": last_msg_text})
|
||||
aiResult = response['result']
|
||||
sessionId = response['session_id']
|
||||
|
||||
anchorWithSession[anchor_name] = sessionId
|
||||
# 找到输入框,输入ai返回出来的消息
|
||||
if sel.exists:
|
||||
sel.click() # 聚焦
|
||||
time.sleep(1)
|
||||
sel.clear_text()
|
||||
sel.set_text(aiResult + "\n")
|
||||
else:
|
||||
LogManager.error("找不到输入框,重启", udid)
|
||||
raise Exception("找不到输入框,重启")
|
||||
else:
|
||||
# 如果不是第一次发消息(证明存储的有sessionId)
|
||||
sessionId = anchorWithSession[anchor_name]
|
||||
response = Requester.chatToAi({"msg": last_msg_text, "sid": sessionId})
|
||||
aiResult = response['result']
|
||||
if sel.exists:
|
||||
sel.click() # 聚焦
|
||||
time.sleep(1)
|
||||
sel.clear_text()
|
||||
sel.set_text(aiResult + "\n")
|
||||
|
||||
LogManager.info(f"存储的sessionId:{anchorWithSession}", udid)
|
||||
time.sleep(1)
|
||||
# 返回
|
||||
ControlUtils.clickBack(session)
|
||||
|
||||
# 重新回到收件箱页面后,强制刷新节点
|
||||
session.appium_settings({"snapshotMaxDepth": 25})
|
||||
time.sleep(1)
|
||||
|
||||
# 双击收件箱 定位到消息的位置
|
||||
if info_count == 5:
|
||||
r = el.bounds # 可能是命名属性,也可能是 tuple
|
||||
@@ -966,9 +949,9 @@ class ScriptManager():
|
||||
cy = int((r.y + r.height / 2) if hasattr(r, "y") else (r[1] + r[3] / 2))
|
||||
session.double_tap(cx, cy) # 可能抛异常:方法不存在
|
||||
LogManager.info(f"双击收件箱 定位到信息", udid)
|
||||
|
||||
else:
|
||||
return
|
||||
else:
|
||||
LogManager.error(f"检测不到收件箱", udid)
|
||||
raise Exception("当前页面找不到收件箱,重启")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user