合并代码
This commit is contained in:
@@ -621,6 +621,265 @@ class ScriptManager():
|
||||
|
||||
print("greetNewFollowers方法执行完毕")
|
||||
|
||||
def safe_followAndGreetUnion(self, udid, needReply, event):
|
||||
|
||||
retries = 0
|
||||
while not event.is_set():
|
||||
try:
|
||||
self.followAndGreetUnion(udid, needReply, event)
|
||||
|
||||
except Exception as e:
|
||||
retries += 1
|
||||
LogManager.method_error(f"greetNewFollowers 出现异常: {e},准备第 {retries} 次重试", "关注打招呼", udid)
|
||||
event.wait(timeout=3)
|
||||
|
||||
if event.is_set():
|
||||
LogManager.method_info("外层 while 检测到停止,即将 break", "关注打招呼", udid)
|
||||
break
|
||||
LogManager.method_error("greetNewFollowers 重试次数耗尽,任务终止", "关注打招呼", udid)
|
||||
|
||||
# 关注打招呼以及回复主播消息(联盟号)
|
||||
def followAndGreetUnion(self, udid, needReply, event):
|
||||
|
||||
client = wda.USBClient(udid)
|
||||
session = client.session()
|
||||
|
||||
print(f"是否要自动回复消息:{needReply}")
|
||||
|
||||
LogManager.method_info(f"是否要自动回复消息:{needReply}", "关注打招呼(联盟号)", udid)
|
||||
|
||||
# 先关闭Tik Tok
|
||||
ControlUtils.closeTikTok(session, udid)
|
||||
event.wait(timeout=1)
|
||||
|
||||
# 重新打开Tik Tok
|
||||
ControlUtils.openTikTok(session, udid)
|
||||
event.wait(timeout=3)
|
||||
LogManager.method_info(f"重启tiktok", "关注打招呼(联盟号)", udid)
|
||||
# 设置查找深度
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
|
||||
# 创建udid名称的目录
|
||||
AiUtils.makeUdidDir(udid)
|
||||
|
||||
# 返回上一步
|
||||
def goBack(count):
|
||||
for i in range(count):
|
||||
LogManager.method_info(f"返回上一步", "关注打招呼(联盟号)", udid)
|
||||
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
ControlUtils.clickBack(session)
|
||||
event.wait(timeout=2)
|
||||
|
||||
LogManager.method_info(f"循环条件1:{not event.is_set()}", "关注打招呼(联盟号)", udid)
|
||||
LogManager.method_info(f"循环条件2:{len(anchorList) > 0}", "关注打招呼(联盟号)", udid)
|
||||
LogManager.method_info(f"循环条件3:{not event.is_set() and len(anchorList) > 0}", "关注打招呼(联盟号)", udid)
|
||||
|
||||
# 循环条件。1、 循环关闭 2、 数据处理完毕
|
||||
while not event.is_set():
|
||||
|
||||
LogManager.method_info("=== 外层 while 新一轮 ===", "关注打招呼(联盟号)", udid)
|
||||
if event.is_set():
|
||||
break
|
||||
|
||||
# 获取一个主播,
|
||||
LogManager.method_info(f"开始获取数据", "关注打招呼(联盟号)", udid)
|
||||
# 获取一个主播,
|
||||
result = AiUtils.peek_aclist_first()
|
||||
LogManager.method_info(f"数据是:{result}", "关注打招呼(联盟号)", udid)
|
||||
|
||||
state = result.get("state", 0)
|
||||
if not state:
|
||||
LogManager.method_info(f"当前主播的状态是:{state} 不通行,取出数据移到列表尾部 继续下一个",
|
||||
"关注打招呼(联盟号)",
|
||||
udid)
|
||||
AiUtils.pop_aclist_first(mode="move")
|
||||
continue
|
||||
|
||||
# 并删除
|
||||
anchor = AiUtils.pop_aclist_first()
|
||||
LogManager.method_info(f"当前主播的状态是:{state} 通行,取出数据删除", "关注打招呼(联盟号)", udid)
|
||||
|
||||
if not anchor:
|
||||
LogManager.method_info(f"数据库中的数据不足", "关注打招呼(联盟号)", udid)
|
||||
if not self.interruptible_sleep(event, 30):
|
||||
continue
|
||||
|
||||
aid = anchor.get("anchorId", "")
|
||||
anchorCountry = anchor.get("country", "")
|
||||
|
||||
LogManager.method_info(f"主播的数据,用户名:{aid},国家:{anchorCountry}", "关注打招呼(联盟号)", udid)
|
||||
|
||||
# 点击搜索按钮
|
||||
ControlUtils.clickSearch(session)
|
||||
|
||||
LogManager.method_info(f"点击搜索按钮", "关注打招呼(联盟号)", udid)
|
||||
|
||||
# 强制刷新session
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
|
||||
# 查找输入框
|
||||
input = session.xpath('//XCUIElementTypeSearchField')
|
||||
|
||||
# 如果找到了输入框,就点击并且输入内容
|
||||
if input.exists:
|
||||
input.click()
|
||||
# 稍作停顿
|
||||
event.wait(timeout=0.5)
|
||||
else:
|
||||
print(f"找不到输入框")
|
||||
|
||||
input = session.xpath('//XCUIElementTypeSearchField')
|
||||
if input.exists:
|
||||
input.clear_text()
|
||||
event.wait(timeout=1)
|
||||
# 输入主播id
|
||||
input.set_text(f"{aid or '暂无数据'}\n")
|
||||
|
||||
# 定位 "关注" 按钮 通过关注按钮的位置点击主播首页
|
||||
|
||||
session.appium_settings({"snapshotMaxDepth": 25})
|
||||
|
||||
try:
|
||||
# 点击进入首页
|
||||
ControlUtils.clickFollow(session, aid)
|
||||
LogManager.method_info("点击进入主播首页", "关注打招呼(联盟号)", udid)
|
||||
except wda.WDAElementNotFoundError:
|
||||
LogManager.method_info("未找到进入主播首页的按钮,使用第二个方案。", "关注打招呼(联盟号)", udid)
|
||||
enter_room = ControlUtils.userClickProfile(session, aid)
|
||||
if not enter_room:
|
||||
goBack(2)
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
continue
|
||||
|
||||
event.wait(timeout=2)
|
||||
|
||||
session.appium_settings({"snapshotMaxDepth": 25})
|
||||
event.wait(timeout=0.5)
|
||||
# 向上滑动
|
||||
session.swipe_down()
|
||||
|
||||
event.wait(timeout=2)
|
||||
msgButton = AiUtils.getSendMesageButton(session)
|
||||
event.wait(timeout=2)
|
||||
|
||||
if msgButton.exists:
|
||||
# 进入聊天页面
|
||||
msgButton.click()
|
||||
LogManager.method_info("找到发消息按钮了", "关注打招呼(联盟号)", udid)
|
||||
print("找到发消息按钮了")
|
||||
else:
|
||||
LogManager.method_info("没有识别出发消息按钮", "关注打招呼(联盟号)", udid)
|
||||
print("没有识别出发消息按钮")
|
||||
goBack(3)
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
continue
|
||||
|
||||
event.wait(timeout=3)
|
||||
# 查找聊天界面中的输入框节点
|
||||
chatInput = session.xpath("//TextView")
|
||||
if chatInput.exists:
|
||||
|
||||
print("找到输入框了, 准备发送一条打招呼消息")
|
||||
LogManager.method_info("找到输入框了, 准备发送一条打招呼消息", "关注打招呼(联盟号)", udid)
|
||||
|
||||
print("打招呼的数据", ev.prologueList)
|
||||
LogManager.method_info(f"传递的打招呼的数据:{ev.prologueList}", "关注打招呼(联盟号)", udid)
|
||||
|
||||
# 准备打招呼的文案
|
||||
text = random.choice(ev.prologueList)
|
||||
# text = 'hello'
|
||||
|
||||
LogManager.method_info(f"取出打招呼的数据,{text}, 判断是否需要翻译", "关注打招呼(联盟号)", udid)
|
||||
|
||||
isContainChniese = AiUtils.contains_chinese(text)
|
||||
|
||||
if isContainChniese:
|
||||
# 翻译成主播国家的语言
|
||||
LogManager.method_info(f"需要翻译:{text},参数为:国家为{anchorCountry}, 即将进行翻译",
|
||||
"关注打招呼(联盟号)", udid)
|
||||
|
||||
msg = Requester.translation(text, anchorCountry)
|
||||
|
||||
LogManager.method_info(f"翻译成功:{msg}, ", "关注打招呼(联盟号)", udid)
|
||||
|
||||
else:
|
||||
msg = text
|
||||
LogManager.method_info(f"即将发送的私信内容:{msg}", "关注打招呼(联盟号)", udid)
|
||||
|
||||
# 准备发送一条信息
|
||||
chatInput = session.xpath("//TextView")
|
||||
if chatInput.exists:
|
||||
chatInput.click()
|
||||
chatInput.set_text(f"{msg or '暂无数据'}\n")
|
||||
event.wait(timeout=2)
|
||||
# 发送消息
|
||||
# input.set_text(f"{aid or '暂无数据'}\n")
|
||||
event.wait(timeout=1)
|
||||
else:
|
||||
print("无法发送信息")
|
||||
LogManager.method_info(f"给主播{aid} 发送消息失败", "关注打招呼(联盟号)", udid)
|
||||
|
||||
# 接着下一个主播
|
||||
goBack(1)
|
||||
|
||||
# 点击关注按钮
|
||||
# followButton = AiUtils.getFollowButton(session).get(timeout=5)
|
||||
# if followButton is not None:
|
||||
# # LogManager.method_info("找到关注按钮了", "关注打招呼", udid)
|
||||
# # followButton.click()
|
||||
# x, y, w, h = followButton.bounds
|
||||
# cx = int(x + w / 2)
|
||||
# cy = int(y + h / 2)
|
||||
# # 随机偏移 ±5 px(可自己改范围)
|
||||
# cx += random.randint(-5, 5)
|
||||
# cy += random.randint(-5, 5)
|
||||
#
|
||||
# ControlUtils.tap_mini_cluster(cx, cy, session)
|
||||
#
|
||||
# else:
|
||||
# LogManager.method_info("没找到关注按钮", "关注打招呼", udid)
|
||||
# time.sleep(1)
|
||||
# goBack(4)
|
||||
# session.appium_settings({"snapshotMaxDepth": 15})
|
||||
# continue
|
||||
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
goBack(3)
|
||||
|
||||
# 设置查找深度
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
event.wait(timeout=2)
|
||||
print("即将要回复消息")
|
||||
LogManager.method_info("即将要回复消息", "关注打招呼(联盟号)", udid)
|
||||
|
||||
if needReply:
|
||||
print("如果需要回复主播消息。走此逻辑")
|
||||
|
||||
print("----------------------------------------------------------")
|
||||
print("监控回复消息")
|
||||
# 执行回复消息逻辑
|
||||
self.monitorMessages(session, udid, event)
|
||||
|
||||
homeButton = AiUtils.findHomeButton(udid)
|
||||
if homeButton.exists:
|
||||
homeButton.click()
|
||||
else:
|
||||
ControlUtils.closeTikTok(session, udid)
|
||||
event.wait(timeout=2)
|
||||
ControlUtils.openTikTok(session, udid)
|
||||
event.wait(timeout=3)
|
||||
|
||||
print("重新创建wda会话 防止wda会话失效")
|
||||
client = wda.USBClient(udid)
|
||||
session = client.session()
|
||||
# 执行完成之后。继续点击搜索
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
else:
|
||||
session.appium_settings({"snapshotMaxDepth": 15})
|
||||
|
||||
print("greetNewFollowers方法执行完毕")
|
||||
|
||||
# 检测消息
|
||||
def replyMessages(self, udid, event):
|
||||
client = wda.USBClient(udid)
|
||||
|
||||
Reference in New Issue
Block a user