增加切换账号功能
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import atexit
|
||||
import random
|
||||
import re
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from enum import Enum
|
||||
@@ -11,6 +13,7 @@ from Utils.IOSAIStorage import IOSAIStorage
|
||||
from Utils.JsonUtils import JsonUtils
|
||||
from Utils.LogManager import LogManager
|
||||
from Entity.Variables import anchorList, removeModelFromAnchorList, anchorWithSession
|
||||
# from Utils.OCRUtils import OCRUtils
|
||||
from Utils.Requester import Requester
|
||||
import Entity.Variables as ev
|
||||
|
||||
@@ -42,8 +45,11 @@ class ScriptManager():
|
||||
|
||||
# 关闭并重新打开 TikTok
|
||||
ControlUtils.closeTikTok(session, udid)
|
||||
|
||||
event.wait(timeout=1)
|
||||
|
||||
ControlUtils.openTikTok(session, udid)
|
||||
|
||||
event.wait(timeout=3)
|
||||
LogManager.method_info("养号重启tiktok", "养号", udid)
|
||||
AiUtils.makeUdidDir(udid)
|
||||
@@ -124,7 +130,7 @@ class ScriptManager():
|
||||
ControlUtils.clickLike(session, udid)
|
||||
|
||||
LogManager.method_info("继续观看视频", "养号", udid)
|
||||
videoTime = random.randint(10, 30)
|
||||
videoTime = random.randint(25, 40)
|
||||
for _ in range(videoTime):
|
||||
if event.is_set():
|
||||
break
|
||||
@@ -137,9 +143,9 @@ class ScriptManager():
|
||||
else:
|
||||
nextTime = random.randint(1, 5)
|
||||
for _ in range(nextTime):
|
||||
if event.is_set():
|
||||
break
|
||||
event.wait(timeout=1)
|
||||
if event.is_set():
|
||||
break
|
||||
event.wait(timeout=1)
|
||||
client.swipe_up()
|
||||
|
||||
except Exception as e:
|
||||
@@ -172,7 +178,9 @@ class ScriptManager():
|
||||
|
||||
# 2) 进入直播 (使用英文)
|
||||
live_button = session(
|
||||
xpath='//XCUIElementTypeButton[@name="直播"] | //XCUIElementTypeOther[@name="直播"]')
|
||||
xpath='//XCUIElementTypeButton[@name="LIVE" or @label="LIVE" or @name="直播" or @label="直播"] '
|
||||
'| //XCUIElementTypeOther[@name="LIVE" or @label="LIVE" or @name="直播" or @label="直播"]'
|
||||
)
|
||||
|
||||
if live_button.exists:
|
||||
live_button.click()
|
||||
@@ -186,7 +194,12 @@ class ScriptManager():
|
||||
break
|
||||
event.wait(timeout=1)
|
||||
|
||||
live_button = session(xpath='//XCUIElementTypeButton[@name="直播"]')
|
||||
# live_button = session(xpath='//XCUIElementTypeButton[@name="直播"]')
|
||||
live_button = session(
|
||||
xpath='//XCUIElementTypeButton[@name="LIVE" or @label="LIVE" or @name="直播" or @label="直播"] '
|
||||
'| //XCUIElementTypeOther[@name="LIVE" or @label="LIVE" or @name="直播" or @label="直播"]'
|
||||
)
|
||||
|
||||
if live_button.exists:
|
||||
continue
|
||||
|
||||
@@ -880,14 +893,26 @@ class ScriptManager():
|
||||
|
||||
print("greetNewFollowers方法执行完毕")
|
||||
|
||||
|
||||
|
||||
# 检测消息
|
||||
def replyMessages(self, udid, event):
|
||||
client = wda.USBClient(udid)
|
||||
session = client.session()
|
||||
|
||||
try:
|
||||
client = wda.USBClient(udid)
|
||||
session = client.session()
|
||||
except Exception as e:
|
||||
LogManager.method_error(f"创建wda会话异常: {e}", "检测消息", udid)
|
||||
return
|
||||
|
||||
LogManager.method_info("开始重启tiktok", "监控消息")
|
||||
ControlUtils.closeTikTok(session, udid)
|
||||
event.wait(timeout=2)
|
||||
# time.sleep(1)
|
||||
ControlUtils.openTikTok(session, udid)
|
||||
event.wait(timeout=3)
|
||||
# time.sleep(1)
|
||||
LogManager.method_info("重启tiktok成功", "监控消息")
|
||||
|
||||
while not event.is_set():
|
||||
try:
|
||||
@@ -895,16 +920,344 @@ class ScriptManager():
|
||||
self.monitorMessages(session, udid, event)
|
||||
except Exception as e:
|
||||
LogManager.method_error(f"监控消息 出现异常: {e},重新启动监控直播", "检测消息", udid)
|
||||
|
||||
LogManager.method_info(f"出现异常时,稍等再重启 TikTok 并重试 异常是: {e}", "监控消息", udid)
|
||||
|
||||
LogManager.method_info(f"出现异常,重新创建wda", "监控消息", udid)
|
||||
client = wda.USBClient(udid)
|
||||
session = client.session()
|
||||
|
||||
LogManager.method_info(f"重启 TikTok", "监控消息", udid)
|
||||
# 出现异常时,稍等再重启 TikTok 并重试
|
||||
ControlUtils.closeTikTok(session, udid)
|
||||
event.wait(timeout=2)
|
||||
# time.sleep(1)
|
||||
ControlUtils.openTikTok(session, udid)
|
||||
event.wait(timeout=3)
|
||||
# time.sleep(1)
|
||||
LogManager.method_info("TikTok 重启成功", "监控消息", udid)
|
||||
continue # 重新进入 while 循环,调用 monitorMessages
|
||||
|
||||
# 检查未读消息并回复
|
||||
# 此方法暂时只取最后一天进行发送给ai
|
||||
|
||||
# def monitorMessages(self, session, udid, event):
|
||||
#
|
||||
# LogManager.method_info("脚本开始执行中", "监控消息")
|
||||
#
|
||||
# # 调整节点的深度为 7
|
||||
# session.appium_settings({"snapshotMaxDepth": 7})
|
||||
#
|
||||
# el = session.xpath(
|
||||
# '//XCUIElementTypeButton[@name="a11y_vo_inbox"]'
|
||||
# ' | '
|
||||
# '//XCUIElementTypeButton[contains(@name,"收件箱")]'
|
||||
# ' | '
|
||||
# '//XCUIElementTypeButton[.//XCUIElementTypeStaticText[@value="收件箱"]]'
|
||||
# )
|
||||
#
|
||||
# # 如果收件箱有消息 则进行点击
|
||||
# if el.exists:
|
||||
#
|
||||
# try:
|
||||
# m = re.search(r'(\d+)', el.label) # 抓到的第一个数字串
|
||||
# except Exception as e:
|
||||
# LogManager.method_error(f"解析收件箱数量异常: {e}", "检测消息", udid)
|
||||
#
|
||||
# count = int(m.group(1)) if m else 0
|
||||
# if count:
|
||||
# el.click()
|
||||
# session.appium_settings({"snapshotMaxDepth": 25})
|
||||
# event.wait(timeout=3)
|
||||
# while True:
|
||||
# info_count = 0
|
||||
#
|
||||
# # 创建新的会话
|
||||
# el = session.xpath(
|
||||
# '//XCUIElementTypeButton[@name="a11y_vo_inbox"]'
|
||||
# ' | '
|
||||
# '//XCUIElementTypeButton[contains(@name,"收件箱")]'
|
||||
# ' | '
|
||||
# '//XCUIElementTypeButton[.//XCUIElementTypeStaticText[@value="收件箱"]]'
|
||||
# )
|
||||
#
|
||||
# print("el", el)
|
||||
# if not el.exists:
|
||||
# LogManager.method_error(f"检测不到收件箱", "检测消息", udid)
|
||||
# raise Exception("当前页面找不到收件箱,重启")
|
||||
# # break
|
||||
#
|
||||
# # 支持中文“收件箱”和英文“Inbox”
|
||||
# xpath_query = (
|
||||
# "//XCUIElementTypeStaticText"
|
||||
# "[@value='收件箱' or @label='收件箱' or @name='收件箱'"
|
||||
# " or @value='Inbox' or @label='Inbox' or @name='Inbox']"
|
||||
# )
|
||||
#
|
||||
# # 查找所有收件箱节点
|
||||
# inbox_nodes = session.xpath(xpath_query).find_elements()
|
||||
#
|
||||
# if len(inbox_nodes) < 2:
|
||||
# LogManager.method_error(f"当前页面不再收件箱页面,重启", "检测消息", udid)
|
||||
# raise Exception("当前页面不再收件箱页面,重启")
|
||||
#
|
||||
# m = re.search(r'(\d+)', el.label) # 抓到的第一个数字串
|
||||
# count = int(m.group(1)) if m else 0
|
||||
#
|
||||
# if not count:
|
||||
# LogManager.method_info(f"当前收件箱的总数量{count}", "检测消息", udid)
|
||||
# break
|
||||
#
|
||||
# # 新粉丝
|
||||
# xp_new_fan_badge = (
|
||||
# "//XCUIElementTypeCell[.//XCUIElementTypeLink[@name='新粉丝']]"
|
||||
# "//XCUIElementTypeStaticText[string-length(@value)>0 and translate(@value,'0123456789','')='']"
|
||||
# )
|
||||
#
|
||||
# # 活动
|
||||
# xp_activity_badge = (
|
||||
# "//XCUIElementTypeCell[.//XCUIElementTypeLink[@name='活动']]"
|
||||
# "//XCUIElementTypeStaticText[string-length(@value)>0 and translate(@value,'0123456789','')='']"
|
||||
# )
|
||||
#
|
||||
# # 系统通知
|
||||
# xp_system_badge = (
|
||||
# "//XCUIElementTypeCell[.//XCUIElementTypeLink[@name='系统通知']]"
|
||||
# "//XCUIElementTypeStaticText[string-length(@value)>0 and translate(@value,'0123456789','')='']"
|
||||
# )
|
||||
#
|
||||
# # 消息请求
|
||||
# xp_request_badge = (
|
||||
# "//XCUIElementTypeCell"
|
||||
# "[.//*[self::XCUIElementTypeLink or self::XCUIElementTypeStaticText]"
|
||||
# " [@name='消息请求' or @label='消息请求' or @value='消息请求']]"
|
||||
# "//XCUIElementTypeStaticText[string-length(@value)>0 and translate(@value,'0123456789','')='']"
|
||||
# )
|
||||
#
|
||||
# # 用户消息
|
||||
# xp_badge_numeric = (
|
||||
# "("
|
||||
# # 你的两类未读容器组件 + 数字徽标(value 纯数字)
|
||||
# "//XCUIElementTypeOther["
|
||||
# " @name='AWEIMChatListCellUnreadCountViewComponent'"
|
||||
# " or @name='TikTokIMImpl.InboxCellUnreadCountViewBuilder'"
|
||||
# "]//XCUIElementTypeStaticText[@value and translate(@value,'0123456789','')='']"
|
||||
# ")/ancestor::XCUIElementTypeCell[1]"
|
||||
# " | "
|
||||
# # 兜底:任何在 CollectionView 下、value 纯数字的徽标 → 找其最近的 Cell
|
||||
# "//XCUIElementTypeCollectionView//XCUIElementTypeStaticText"
|
||||
# "[@value and translate(@value,'0123456789','')='']"
|
||||
# "/ancestor::XCUIElementTypeCell[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.method_info(f"用户未读数量:{val}", "检测消息", udid)
|
||||
# except Exception:
|
||||
# LogManager.method_warning("当前屏幕没有找到 用户 未读徽标数字", "检测消息", udid)
|
||||
# print("当前屏幕没有找到 用户消息 未读徽标数字", udid)
|
||||
# user_text = None
|
||||
# info_count += 1
|
||||
#
|
||||
# if user_text:
|
||||
#
|
||||
# user_text.tap()
|
||||
# event.wait(timeout=3)
|
||||
#
|
||||
# xml = session.source()
|
||||
# msgs = AiUtils.extract_messages_from_xml(xml)
|
||||
#
|
||||
# text_list = ['What do you think of my live stream?',
|
||||
# 'What do you think makes my streams special?',
|
||||
# 'Do you think I’m one of the most engaging streamers you’ve seen?']
|
||||
#
|
||||
# # 检测出对方发的最后一条信息
|
||||
#
|
||||
#
|
||||
# last_msg = next((item['text'] for item in reversed(msgs) if item['type'] == 'msg'),
|
||||
# random.choice(text_list))
|
||||
#
|
||||
# LogManager.method_info(f"检测到对方最后发送的消息:{last_msg}", "检测消息", udid)
|
||||
#
|
||||
# isLanguage = AiUtils.is_language(last_msg)
|
||||
# if isLanguage:
|
||||
# # LogManager.method_info(f":{last_msg}", "检测消息", udid)
|
||||
#
|
||||
# last_msg_text = last_msg
|
||||
# else:
|
||||
# LogManager.method_info(f"对方发送的消息不是语言,随机挑选作为最后一条进行回复:{last_msg}",
|
||||
# "检测消息", udid)
|
||||
# last_msg_text = random.choice(text_list)
|
||||
#
|
||||
# if AiUtils.contains_chinese(last_msg_text):
|
||||
# LogManager.method_info(f"需要翻译:{last_msg_text}, 即将进行翻译", "检测消息", udid)
|
||||
# last_msg_text = Requester.translation(last_msg_text)
|
||||
# LogManager.method_info(f"翻译成功:{last_msg_text}, ", "检测消息", udid)
|
||||
#
|
||||
# # 向ai发送信息
|
||||
# # 获取主播的名称
|
||||
# anchor_name = AiUtils.get_navbar_anchor_name(session)
|
||||
#
|
||||
# LogManager.method_info(f"获取主播的名称:{anchor_name}", "检测消息", udid)
|
||||
# LogManager.method_info(f"获取主播最后发送的消息 进行翻译:{last_msg}", "检测消息", udid)
|
||||
# last_msg = Requester.translationToChinese(last_msg)
|
||||
# LogManager.method_info(f"翻译后的内容:{last_msg}", "检测消息", udid)
|
||||
#
|
||||
# # 找到输入框
|
||||
# last_data = [{
|
||||
# "sender": anchor_name,
|
||||
# "device": udid,
|
||||
# "text": last_msg,
|
||||
# "status": 0
|
||||
# }]
|
||||
# print(last_data)
|
||||
#
|
||||
# LogManager.method_info(f"主播最后发送的数据,传递给前端进行记录:{last_data}", "检测消息", udid)
|
||||
# JsonUtils.append_json_items(last_data, "log/last_message.json")
|
||||
#
|
||||
# # 从C盘中读取数据
|
||||
# anchorWithSession = IOSAIStorage.load()
|
||||
#
|
||||
# sel = session.xpath("//TextView")
|
||||
# if anchor_name not in anchorWithSession:
|
||||
# # 如果是第一次发消息(没有sessionId的情况)
|
||||
# LogManager.method_info(f"第一次发消息:{anchor_name},没有记忆 开始请求ai", "检测消息", udid)
|
||||
# LogManager.method_info(f"向ai发送的参数", "检测消息", udid)
|
||||
# aiResult, sessionId = Requester.chatToAi({"query": last_msg_text, "user": "1"})
|
||||
# IOSAIStorage.save({anchor_name: sessionId})
|
||||
#
|
||||
# # 找到输入框,输入ai返回出来的消息
|
||||
#
|
||||
# if sel.exists:
|
||||
# sel.click() # 聚焦
|
||||
# event.wait(timeout=1)
|
||||
# sel.clear_text()
|
||||
# sel.set_text(f"{aiResult or '暂无数据'}\n")
|
||||
# else:
|
||||
# LogManager.method_error("找不到输入框,重启", "检测消息", udid)
|
||||
# raise Exception("找不到输入框,重启")
|
||||
# else:
|
||||
# LogManager.method_info(f"不是一次发消息:{anchor_name},有记忆", "检测消息", udid)
|
||||
# # 如果不是第一次发消息(证明存储的有sessionId)
|
||||
# sessionId = anchorWithSession[anchor_name]
|
||||
#
|
||||
# # TODO: user后续添加,暂时写死
|
||||
#
|
||||
# aiResult, sessionId = Requester.chatToAi(
|
||||
# {"query": last_msg_text, "conversation_id": sessionId, "user": "1"})
|
||||
# # aiResult = response['result']
|
||||
# if sel.exists:
|
||||
# sel.click() # 聚焦
|
||||
# event.wait(timeout=1)
|
||||
# sel.clear_text()
|
||||
# sel.set_text(f"{aiResult or '暂无数据'}\n")
|
||||
#
|
||||
# LogManager.method_info(f"存储的sessionId:{anchorWithSession}", "检测消息", udid)
|
||||
# event.wait(timeout=1)
|
||||
# # 返回
|
||||
# ControlUtils.clickBack(session)
|
||||
#
|
||||
# # 重新回到收件箱页面后,强制刷新节点
|
||||
# session.appium_settings({"snapshotMaxDepth": 25})
|
||||
# event.wait(timeout=1)
|
||||
# try:
|
||||
# # 如果 2 秒内找不到,会抛异常
|
||||
# badge_text = session.xpath(xp_new_fan_badge).get(timeout=2.0)
|
||||
# val = (badge_text.info.get("value") or
|
||||
# badge_text.info.get("label") or
|
||||
# badge_text.info.get("name"))
|
||||
#
|
||||
# LogManager.method_info(f"新粉丝未读数量:{val}", "检测消息", udid)
|
||||
# if badge_text:
|
||||
# badge_text.tap()
|
||||
# event.wait(timeout=1)
|
||||
# ControlUtils.clickBack(session)
|
||||
# event.wait(timeout=1)
|
||||
# except Exception:
|
||||
# LogManager.method_warning("当前屏幕没有找到 新粉丝 未读徽标数字", "检测消息", udid)
|
||||
# print("当前屏幕没有找到 新粉丝 未读徽标数字", udid)
|
||||
# badge_text = None
|
||||
# info_count += 1
|
||||
#
|
||||
# try:
|
||||
#
|
||||
# # 如果 2 秒内找不到,会抛异常
|
||||
# badge_text = session.xpath(xp_activity_badge).get(timeout=2.0)
|
||||
# val = (badge_text.info.get("value") or
|
||||
# badge_text.info.get("label") or
|
||||
# badge_text.info.get("name"))
|
||||
# LogManager.method_info(f"活动未读数量:{val}", "检测消息", udid)
|
||||
# if badge_text:
|
||||
# badge_text.tap()
|
||||
# event.wait(timeout=1)
|
||||
# ControlUtils.clickBack(session)
|
||||
# event.wait(timeout=1)
|
||||
# except Exception:
|
||||
# LogManager.method_warning("当前屏幕没有找到 活动 未读徽标数字", "检测消息", udid)
|
||||
# print("当前屏幕没有找到 活动 未读徽标数字", udid)
|
||||
# badge_text = None
|
||||
# info_count += 1
|
||||
#
|
||||
# try:
|
||||
# # 如果 2 秒内找不到,会抛异常
|
||||
# badge_text = session.xpath(xp_system_badge).get(timeout=2.0)
|
||||
# val = (badge_text.info.get("value") or
|
||||
# badge_text.info.get("label") or
|
||||
# badge_text.info.get("name"))
|
||||
# LogManager.method_info(f"系统通知未读数量:{val}", "检测消息", udid)
|
||||
# if badge_text:
|
||||
# badge_text.tap()
|
||||
# event.wait(timeout=1)
|
||||
# ControlUtils.clickBack(session)
|
||||
# event.wait(timeout=1)
|
||||
# except Exception:
|
||||
# LogManager.method_warning("当前屏幕没有找到 系统通知 未读徽标数字", "检测消息", udid)
|
||||
# print("当前屏幕没有找到 系统通知 未读徽标数字", udid)
|
||||
# badge_text = None
|
||||
# info_count += 1
|
||||
#
|
||||
# try:
|
||||
# # 如果 2 秒内找不到,会抛异常
|
||||
# badge_text = session.xpath(xp_request_badge).get(timeout=2.0)
|
||||
# val = (badge_text.info.get("value") or
|
||||
# badge_text.info.get("label") or
|
||||
# badge_text.info.get("name"))
|
||||
# LogManager.method_info(f"消息请求未读数量:{val}", "检测消息", udid)
|
||||
# if badge_text:
|
||||
# badge_text.tap()
|
||||
# event.wait(timeout=1)
|
||||
# ControlUtils.clickBack(session)
|
||||
# event.wait(timeout=1)
|
||||
# except Exception:
|
||||
# LogManager.method_warning("当前屏幕没有找到 消息请求 未读徽标数字", "检测消息", udid)
|
||||
# print("当前屏幕没有找到 消息请求 未读徽标数字", udid)
|
||||
# badge_text = None
|
||||
# info_count += 1
|
||||
#
|
||||
# # 双击收件箱 定位到消息的位置
|
||||
# if info_count == 5:
|
||||
# r = el.bounds # 可能是命名属性,也可能是 tuple
|
||||
# cx = int((r.x + r.width / 2) if hasattr(r, "x") else (r[0] + r[2] / 2))
|
||||
# cy = int((r.y + r.height / 2) if hasattr(r, "y") else (r[1] + r[3] / 2))
|
||||
# session.double_tap(cx, cy) # 可能抛异常:方法不存在
|
||||
# LogManager.method_info(f"双击收件箱 定位到信息", "检测消息", udid)
|
||||
#
|
||||
# else:
|
||||
# return
|
||||
# else:
|
||||
# LogManager.method_error(f"检测不到收件箱", "检测消息", udid)
|
||||
# raise Exception("当前页面找不到收件箱,重启")
|
||||
|
||||
# 检测消息进行优化,检测直到我发送的内容,把这些发送给ai
|
||||
|
||||
def monitorMessages(self, session, udid, event):
|
||||
|
||||
LogManager.method_info("脚本开始执行中", "监控消息")
|
||||
|
||||
# 调整节点的深度为 7
|
||||
session.appium_settings({"snapshotMaxDepth": 7})
|
||||
|
||||
@@ -918,7 +1271,12 @@ class ScriptManager():
|
||||
|
||||
# 如果收件箱有消息 则进行点击
|
||||
if el.exists:
|
||||
m = re.search(r'(\d+)', el.label) # 抓到的第一个数字串
|
||||
|
||||
try:
|
||||
m = re.search(r'(\d+)', el.label) # 抓到的第一个数字串
|
||||
except Exception as e:
|
||||
LogManager.method_error(f"解析收件箱数量异常: {e}", "检测消息", udid)
|
||||
|
||||
count = int(m.group(1)) if m else 0
|
||||
if count:
|
||||
el.click()
|
||||
@@ -1019,6 +1377,7 @@ class ScriptManager():
|
||||
info_count += 1
|
||||
|
||||
if user_text:
|
||||
|
||||
user_text.tap()
|
||||
event.wait(timeout=3)
|
||||
|
||||
@@ -1028,46 +1387,46 @@ class ScriptManager():
|
||||
text_list = ['What do you think of my live stream?',
|
||||
'What do you think makes my streams special?',
|
||||
'Do you think I’m one of the most engaging streamers you’ve seen?']
|
||||
# 检测出对方发的最后一条信息
|
||||
|
||||
# 检测出对方发的最后一条信息,
|
||||
|
||||
# 获取了最后一条消息
|
||||
last_msg = next((item['text'] for item in reversed(msgs) if item['type'] == 'msg'),
|
||||
random.choice(text_list))
|
||||
|
||||
LogManager.method_info(f"检测到对方最后发送的消息:{last_msg}", "检测消息", udid)
|
||||
|
||||
isLanguage = AiUtils.is_language(last_msg)
|
||||
if isLanguage:
|
||||
# LogManager.method_info(f":{last_msg}", "检测消息", udid)
|
||||
# 如果最后一条消息不是文字,随机取出一条当做最后一条消息,最后一条消息是last_msg_text
|
||||
|
||||
isLanguage = AiUtils.is_language(last_msg)
|
||||
|
||||
if isLanguage:
|
||||
last_msg_text = last_msg
|
||||
else:
|
||||
LogManager.method_info(f"对方发送的消息不是语言,随机挑选作为最后一条进行回复:{last_msg}",
|
||||
"检测消息", udid)
|
||||
last_msg_text = random.choice(text_list)
|
||||
|
||||
if AiUtils.contains_chinese(last_msg_text):
|
||||
LogManager.method_info(f"需要翻译:{last_msg_text}, 即将进行翻译", "检测消息", udid)
|
||||
last_msg_text = Requester.translation(last_msg_text)
|
||||
LogManager.method_info(f"翻译成功:{last_msg_text}, ", "检测消息", udid)
|
||||
|
||||
# 向ai发送信息
|
||||
# 获取主播的名称
|
||||
anchor_name = AiUtils.get_navbar_anchor_name(session)
|
||||
|
||||
LogManager.method_info(f"获取主播的名称:{anchor_name}", "检测消息", udid)
|
||||
LogManager.method_info(f"获取主播最后发送的消息 进行翻译:{last_msg}", "检测消息", udid)
|
||||
last_msg = Requester.translation(last_msg, "中国")
|
||||
LogManager.method_info(f"翻译后的内容:{last_msg}", "检测消息", udid)
|
||||
chinese_last_msg_text = Requester.translationToChinese(last_msg_text)
|
||||
LogManager.method_info(f"翻译中文后的内容,交给前端进行展示:{chinese_last_msg_text}", "检测消息",
|
||||
udid)
|
||||
|
||||
# 找到输入框
|
||||
last_data = [{
|
||||
"sender": anchor_name,
|
||||
"device": udid,
|
||||
"text": last_msg,
|
||||
"text": chinese_last_msg_text,
|
||||
"status": 0
|
||||
}]
|
||||
print(last_data)
|
||||
|
||||
LogManager.method_info(f"主播最后发送的数据,传递给前端进行记录:{last_data}", "检测消息", udid)
|
||||
LogManager.method_info(f"主播最后发送的数据,传递给前端进行记录:{chinese_last_msg_text}",
|
||||
"检测消息", udid)
|
||||
JsonUtils.append_json_items(last_data, "log/last_message.json")
|
||||
|
||||
# 从C盘中读取数据
|
||||
@@ -1075,11 +1434,12 @@ class ScriptManager():
|
||||
|
||||
sel = session.xpath("//TextView")
|
||||
if anchor_name not in anchorWithSession:
|
||||
|
||||
# 如果是第一次发消息(没有sessionId的情况)
|
||||
LogManager.method_info(f"第一次发消息:{anchor_name},没有记忆 开始请求ai", "检测消息", udid)
|
||||
LogManager.method_info(f"向ai发送的参数", "检测消息", udid)
|
||||
LogManager.method_info(f"向ai发送的参数: 文本为:{last_msg_text}", "检测消息", udid)
|
||||
aiResult, sessionId = Requester.chatToAi({"query": last_msg_text, "user": "1"})
|
||||
IOSAIStorage.save({anchor_name: sessionId})
|
||||
IOSAIStorage.save({anchor_name: sessionId}, mode="merge")
|
||||
|
||||
# 找到输入框,输入ai返回出来的消息
|
||||
|
||||
@@ -1098,6 +1458,8 @@ class ScriptManager():
|
||||
|
||||
# TODO: user后续添加,暂时写死
|
||||
|
||||
LogManager.method_info(f"向ai发送的参数: 文本为:{last_msg_text}", "检测消息", udid)
|
||||
|
||||
aiResult, sessionId = Requester.chatToAi(
|
||||
{"query": last_msg_text, "conversation_id": sessionId, "user": "1"})
|
||||
# aiResult = response['result']
|
||||
@@ -1115,6 +1477,7 @@ class ScriptManager():
|
||||
# 重新回到收件箱页面后,强制刷新节点
|
||||
session.appium_settings({"snapshotMaxDepth": 25})
|
||||
event.wait(timeout=1)
|
||||
|
||||
try:
|
||||
# 如果 2 秒内找不到,会抛异常
|
||||
badge_text = session.xpath(xp_new_fan_badge).get(timeout=2.0)
|
||||
@@ -1203,7 +1566,6 @@ class ScriptManager():
|
||||
LogManager.method_error(f"检测不到收件箱", "检测消息", udid)
|
||||
raise Exception("当前页面找不到收件箱,重启")
|
||||
|
||||
|
||||
# 放在 ScriptManager 类外面或 utils 里
|
||||
def interruptible_sleep(self, event: threading.Event, seconds: float, slice_: float = 1.0):
|
||||
"""把一次长 sleep 拆成 1 秒一片,随时响应 event"""
|
||||
@@ -1214,3 +1576,82 @@ class ScriptManager():
|
||||
left -= timeout
|
||||
return not event.is_set() # 返回 True 表示正常睡完,False 被中断
|
||||
|
||||
# 切换账号
|
||||
def changeAccount(self, udid, event):
|
||||
|
||||
LogManager.method_info("开始进行切换账号", "切换账号", udid)
|
||||
|
||||
client = wda.USBClient(udid)
|
||||
session = client.session()
|
||||
|
||||
# 重启进行切换账号
|
||||
ControlUtils.closeTikTok(session, udid)
|
||||
# event.wait(timeout=2)
|
||||
time.sleep(1)
|
||||
ControlUtils.openTikTok(session, udid)
|
||||
|
||||
# 使用pop取出列表中的第一个元素,
|
||||
account_ids = IOSAIStorage.load(f"{udid}/accountId.json")
|
||||
account_id = account_ids.pop(0)
|
||||
# 再放到末尾
|
||||
account_ids.append(account_id)
|
||||
# 重新进行保存覆盖
|
||||
IOSAIStorage.save(account_ids, f"{udid}/accountId.json")
|
||||
|
||||
LogManager.method_info("重启进行切换账号", "切换账号", udid)
|
||||
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
|
||||
user_home = session.xpath('//XCUIElementTypeButton[@name="a11y_vo_profile" or @label="主页"]')
|
||||
LogManager.method_info("检测主页按钮", "切换账号", udid)
|
||||
if user_home.exists:
|
||||
LogManager.method_info("检测主页按钮成功,点击主页", "切换账号", udid)
|
||||
user_home.click()
|
||||
|
||||
session.appium_settings({"snapshotMaxDepth": 25})
|
||||
|
||||
LogManager.method_info("检测切换账号按钮", "切换账号", udid)
|
||||
check_account_btn = session.xpath('//XCUIElementTypeButton[@name="切换账号" or @label="切换账号"]')
|
||||
if check_account_btn.exists:
|
||||
LogManager.method_info("检测切换账号按钮成功,点击切换账号", "切换账号", udid)
|
||||
check_account_btn.click()
|
||||
|
||||
# 使用截图进行保存,保存进行图像的识别
|
||||
|
||||
try:
|
||||
img = client.screenshot()
|
||||
base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||||
resource_dir = os.path.join(base_dir, "resources", udid)
|
||||
os.makedirs(resource_dir, exist_ok=True)
|
||||
filePath = os.path.join(resource_dir, "home.png")
|
||||
img.save(filePath)
|
||||
LogManager.method_info(f"保存屏幕图像成功 -> {filePath}", "养号", udid)
|
||||
print("保存了背景图:", filePath)
|
||||
event.wait(timeout=1)
|
||||
except Exception as e:
|
||||
LogManager.method_info(f"截图或保存失败,失败原因:{e}", "养号", udid)
|
||||
raise Exception("截图或保存失败,重启养号功能")
|
||||
|
||||
# 使用EasyOcr进行识别,
|
||||
|
||||
# 取出图片路径
|
||||
image_path = AiUtils.imagePathWithName(udid, "home") # 替换为你的图像路径
|
||||
|
||||
# ocr = OCRUtils(langs=['ch_sim', 'en'], force_gpu=None)
|
||||
# print(f"image_path:{image_path}")
|
||||
#
|
||||
# # 进行识别,切换
|
||||
# hit = ocr.find_best(image_path, account_id, match_mode='fuzzy', weight_sim=0.75)
|
||||
#
|
||||
# if hit:
|
||||
# print(f"[BEST] {hit.text} | sim={hit.sim:.2f} conf={hit.conf:.2f} score={hit.score:.2f}")
|
||||
# print(f"bbox={hit.bbox}, center={hit.center}")
|
||||
# # 做一次缩放映射
|
||||
# mapped_hit = hit.mapped(1 / 6, 1 / 6)
|
||||
# print(f"center(/6)={mapped_hit.center}")
|
||||
#
|
||||
# # 传给 session.tap
|
||||
# session.tap(mapped_hit.center[0], mapped_hit.center[1])
|
||||
#
|
||||
# else:
|
||||
# print("未找到目标名称")
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user