From 096b10f5f09f3acf222e970e8b00fda1cfc8e9c4 Mon Sep 17 00:00:00 2001
From: zhangkai <2403741920@qq.com>
Date: Mon, 15 Sep 2025 22:40:45 +0800
Subject: [PATCH] =?UTF-8?q?20250904-=E5=88=9D=E6=AD=A5=E5=8A=9F=E8=83=BD?=
=?UTF-8?q?=E5=B7=B2=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 1 -
.idea/workspace.xml | 88 ++++++++++++++++++++---------------------
Module/DeviceInfo.py | 38 ++++++++++++++++++
Utils/AiUtils.py | 1 +
Utils/LogManager.py | 2 +-
script/ScriptManager.py | 61 ++++++++++------------------
6 files changed, 103 insertions(+), 88 deletions(-)
diff --git a/.gitignore b/.gitignore
index 30c0a87..90e4b38 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,7 +20,6 @@ var/
.installed.cfg
*.egg
out/
-log/
# PyInstaller
# Usually these files are written by a python script from a template
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 6f0ee87..77c3d8f 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -8,6 +8,9 @@
+
+
+
@@ -38,6 +41,9 @@
+
+
+
{
"customColor": "",
"associatedIndex": 5
@@ -50,52 +56,37 @@
- {
+ "keyToString": {
+ "ASKED_ADD_EXTERNAL_FILES": "true",
+ "ASKED_MARK_IGNORED_FILES_AS_EXCLUDED": "true",
+ "Python.12.executor": "Run",
+ "Python.123.executor": "Run",
+ "Python.Main.executor": "Run",
+ "Python.tidevice_entry.executor": "Run",
+ "RunOnceActivity.ShowReadmeOnStart": "true",
+ "RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
+ "RunOnceActivity.git.unshallow": "true",
+ "SHARE_PROJECT_CONFIGURATION_FILES": "true",
+ "git-widget-placeholder": "main",
+ "javascript.nodejs.core.library.configured.version": "20.17.0",
+ "javascript.nodejs.core.library.typings.version": "20.17.58",
+ "last_opened_file_path": "F:/company code/AI item/20250820/iOSAI",
+ "node.js.detected.package.eslint": "true",
+ "node.js.detected.package.tslint": "true",
+ "node.js.selected.package.eslint": "(autodetect)",
+ "node.js.selected.package.tslint": "(autodetect)",
+ "nodejs_package_manager_path": "npm",
+ "settings.editor.selected.configurable": "preferences.editor.code.editing",
+ "vue.rearranger.settings.migration": "true"
}
-}]]>
+}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -168,15 +159,16 @@
-
+
-
+
+
@@ -241,6 +233,7 @@
+
@@ -310,15 +303,18 @@
-
+
-
-
-
-
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Module/DeviceInfo.py b/Module/DeviceInfo.py
index d828f2e..810e6bd 100644
--- a/Module/DeviceInfo.py
+++ b/Module/DeviceInfo.py
@@ -34,6 +34,9 @@ class Deviceinfo(object):
self._port_pool: List[int] = [] # 端口回收池
self._port_in_use: set[int] = set() # 正在使用的端口
+ # 🔥1. 启动 WDA 健康检查线程
+ # threading.Thread(target=self._wda_health_checker, daemon=True).start()
+
# region iproxy 初始化
try:
self.iproxy_path = self._iproxy_path()
@@ -124,6 +127,41 @@ class Deviceinfo(object):
time.sleep(1)
+ # 🔥2. WDA 健康检查
+ def _wda_health_checker(self):
+ while True:
+ time.sleep(1)
+ print(len(self.deviceModelList))
+ with self._lock:
+ online = [m for m in self.deviceModelList if m.ready] # ← 只检查就绪的
+ print(len(online))
+ for model in online:
+ udid = model.deviceId
+ if not self._wda_ok(udid):
+ LogManager.warning(f"WDA 异常,重启通道:{udid}", udid)
+ with self._lock:
+ self._remove_model(udid)
+ self.connectDevice(udid)
+
+ # 🔥3. 真正做 health-check 的地方
+ def _wda_ok(self, udid: str) -> bool:
+ """返回 True 表示 WDA 活着,False 表示已死"""
+ try:
+ # 用 2 秒超时快速探测
+ c = wda.USBClient(udid, 8100)
+ # 下面这句就是“xctest launched but check failed” 的触发点
+ # 如果 status 里返回了 WebDriverAgent 运行信息就认为 OK
+ st = c.status()
+ if st.get("state") != "success":
+ return False
+ # 你也可以再苛刻一点,多做一次 /wda/healthcheck
+ # c.http.get("/wda/healthcheck")
+ return True
+ except Exception as e:
+ # 任何异常(连接拒绝、超时、json 解析失败)都认为已死
+ LogManager.error(f"WDA health-check 异常:{e}", udid)
+ return False
+
# region ===================== 增删改查唯一入口(线程安全) =====================
def _has_model(self, udid: str) -> bool:
return udid in self._model_index
diff --git a/Utils/AiUtils.py b/Utils/AiUtils.py
index 89052b8..69c5a47 100644
--- a/Utils/AiUtils.py
+++ b/Utils/AiUtils.py
@@ -247,6 +247,7 @@ 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]")
+ session.appium_settings({"snapshotMaxDepth": 20})
followButton = session.xpath('//XCUIElementTypeButton[@name="关注" or @label="关注"]')
diff --git a/Utils/LogManager.py b/Utils/LogManager.py
index 67e8a24..a39050c 100644
--- a/Utils/LogManager.py
+++ b/Utils/LogManager.py
@@ -240,7 +240,7 @@ def _force_utf8_everywhere():
except Exception:
pass
-# _force_utf8_everywhere()
+_force_utf8_everywhere()
# ========= 全局:强制 UTF-8 + 关闭缓冲(运行期立刻生效) =========
diff --git a/script/ScriptManager.py b/script/ScriptManager.py
index 30cfb0d..9ba11e3 100644
--- a/script/ScriptManager.py
+++ b/script/ScriptManager.py
@@ -301,12 +301,12 @@ class ScriptManager():
LogManager.method_info(f"是否要自动回复消息:{needReply}", "关注打招呼", udid)
# 先关闭Tik Tok
- # ControlUtils.closeTikTok(session, udid)
- # time.sleep(random.uniform(1.5, 2.5))
- #
- # # 重新打开Tik Tok
- # ControlUtils.openTikTok(session, udid)
- # time.sleep(random.uniform(2.5, 3.5))
+ ControlUtils.closeTikTok(session, udid)
+ time.sleep(1)
+
+ # 重新打开Tik Tok
+ ControlUtils.openTikTok(session, udid)
+ time.sleep(3)
# 设置查找深度
session.appium_settings({"snapshotMaxDepth": 15})
@@ -319,7 +319,7 @@ class ScriptManager():
for i in range(count):
session.appium_settings({"snapshotMaxDepth": 15})
ControlUtils.clickBack(session)
- time.sleep(random.uniform(1.5, 2.5))
+ time.sleep(2)
LogManager.method_info(f"循环条件1:{not event.is_set()}", "关注打招呼", udid)
LogManager.method_info(f"循环条件2:{len(anchorList) > 0}", "关注打招呼", udid)
@@ -332,7 +332,7 @@ class ScriptManager():
anchor = AiUtils.pop_aclist_first()
if not anchor:
LogManager.method_info(f"数据库中的数据不足", "关注打招呼", udid)
- time.sleep(random.uniform(25, 35))
+ time.sleep(30)
continue
aid = anchor["anchorId"]
@@ -341,8 +341,6 @@ class ScriptManager():
# 点击搜索按钮
ControlUtils.clickSearch(session)
-
-
# 强制刷新session
session.appium_settings({"snapshotMaxDepth": 15})
@@ -358,7 +356,7 @@ class ScriptManager():
print(f"找不到输入框")
input.clear_text()
- time.sleep(random.uniform(1, 2))
+ time.sleep(1)
# 输入主播id
input.set_text(f"{aid or '暂无数据'}\n")
@@ -378,11 +376,11 @@ class ScriptManager():
session.appium_settings({"snapshotMaxDepth": 15})
continue
- time.sleep(random.uniform(1.5, 2.5))
+ time.sleep(2)
# 找到并点击第一个视频
cellClickResult, workCount = ControlUtils.clickFirstVideoFromDetailPage(session)
- time.sleep(random.uniform(1.5, 2.5))
+ time.sleep(2)
# 观看主播视频
def viewAnchorVideo(workCount):
@@ -395,11 +393,11 @@ class ScriptManager():
count = workCount
while count != 0:
- time.sleep(random.uniform(3, 6))
+ time.sleep(5)
img = client.screenshot()
- time.sleep(random.uniform(0.5, 2))
- # filePath = f"resources/{udid}/bgv.png"
+ time.sleep(1)
+ # filePath = f"resources/{udid}/bgv.png"
base_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) # 当前脚本目录的上一级
filePath = os.path.join(base_dir, "resources", udid, "bgv.png")
@@ -410,7 +408,7 @@ class ScriptManager():
img.save(filePath)
LogManager.method_info("保存屏幕图像成功", "关注打招呼", udid)
- time.sleep(random.uniform(1.5, 2.5))
+ time.sleep(2)
# 查找add图标
r = ControlUtils.clickLike(session, udid)
@@ -467,8 +465,8 @@ class ScriptManager():
LogManager.method_info(f"传递的打招呼的数据:{ev.prologueList}", "关注打招呼", udid)
# 准备打招呼的文案
- text = random.choice(ev.prologueList)
- # text = 'hello'
+ # text = random.choice(ev.prologueList)
+ text = 'hello'
LogManager.method_info(f"取出打招呼的数据,{text}, 判断是否需要翻译", "关注打招呼", udid)
@@ -487,13 +485,13 @@ class ScriptManager():
# 准备发送一条信息
chatInput.click()
- time.sleep(random.uniform(1.5, 2.5))
+ time.sleep(2)
# 发送消息
chatInput.set_text(f"{msg or '暂无数据'}\n")
# input.set_text(f"{aid or '暂无数据'}\n")
- time.sleep(random.uniform(1, 2))
+ time.sleep(1)
else:
print("无法发送信息")
@@ -503,28 +501,11 @@ class ScriptManager():
# removeModelFromAnchorList(anchor)
goBack(1)
- session.appium_settings({"snapshotMaxDepth": 25})
# 点击关注按钮
followButton = AiUtils.getFollowButton(session)
- if followButton:
-
-
+ if followButton is not None:
LogManager.method_info("找到关注按钮了", "关注打招呼", udid)
- 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)
- session.tap(cx - 95 + random.randint(-5, 5), cy)
- time.sleep(random.uniform(1.5, 2.5))
- # session.tap(cx - 50, cy - 50)
-
- LogManager.method_info(f"关注按钮落点=({cx},{cy})", "关注打招呼", udid)
- session.tap(cx, cy) # facebook-wda 需要列表传参
- time.sleep(random.uniform(1.5, 2.5))
-
+ followButton.click()
else:
LogManager.method_info("没找到关注按钮", "关注打招呼", udid)
time.sleep(1)