修改全部停止,增加关注打招呼特殊翻译功能

This commit is contained in:
2025-11-04 14:07:46 +08:00
parent 1baf9c7fc5
commit f49755cb30
4 changed files with 91 additions and 63 deletions

View File

@@ -347,6 +347,8 @@ def passAnchorData():
# 获取打招呼数据 # 获取打招呼数据
ev.prologueList = data.get("prologueList", []) ev.prologueList = data.get("prologueList", [])
needTranslate = data.get("needTranslate", False)
# 添加主播数据 # 添加主播数据
addModelToAnchorList(acList) addModelToAnchorList(acList)
# 启动线程,执行脚本 # 启动线程,执行脚本
@@ -355,7 +357,7 @@ def passAnchorData():
event = threading.Event() event = threading.Event()
# 启动脚本 # 启动脚本
thread = threading.Thread(target=manager.safe_greetNewFollowers, thread = threading.Thread(target=manager.safe_greetNewFollowers,
args=(udid, needReply, isComment, event,)) args=(udid, needReply, isComment, needTranslate, event,))
# 添加到线程管理 # 添加到线程管理
ThreadManager.add(udid, thread, event) ThreadManager.add(udid, thread, event)
return ResultData(data="").toJson() return ResultData(data="").toJson()
@@ -379,6 +381,9 @@ def followAndGreetUnion():
# 是否需要回复 # 是否需要回复
needReply = data.get("needReply", True) needReply = data.get("needReply", True)
needTranslate = data.get("needTranslate", False)
# 获取打招呼数据 # 获取打招呼数据
@@ -392,7 +397,7 @@ def followAndGreetUnion():
event = threading.Event() event = threading.Event()
# 启动脚本 # 启动脚本
thread = threading.Thread(target=manager.safe_followAndGreetUnion, thread = threading.Thread(target=manager.safe_followAndGreetUnion,
args=(udid, needReply, event)) args=(udid, needReply, needTranslate, event))
# 添加到线程管理 # 添加到线程管理
ThreadManager.add(udid, thread, event) ThreadManager.add(udid, thread, event)
return ResultData(data="").toJson() return ResultData(data="").toJson()

View File

@@ -254,21 +254,53 @@ class ThreadManager:
} }
return o return o
# @classmethod
# def batch_stop(cls, ids: List[str]) -> Tuple[int, str]:
# failed = []
# with ThreadPoolExecutor(max_workers=4) as executor:
# futures = {executor.submit(cls.stop, udid): udid for udid in ids}
# for future in as_completed(futures):
# udid = futures[future]
# try:
# code, msg = future.result()
# except Exception as e:
# LogManager.method_error(f"[{udid}] stop 调用异常: {e}", "task")
# failed.append(udid)
# continue
# if code != 200:
# failed.append(udid)
# if failed:
# return 207, f"部分任务停止失败: {failed}"
# return 200, "全部停止请求已提交"
@classmethod @classmethod
def batch_stop(cls, ids: List[str]) -> Tuple[int, str]: def batch_stop(cls, ids: List[str]) -> Tuple[int, str]:
failed = [] failed = []
results = []
with ThreadPoolExecutor(max_workers=4) as executor: with ThreadPoolExecutor(max_workers=4) as executor:
futures = {executor.submit(cls.stop, udid): udid for udid in ids} futures = {executor.submit(cls.stop, udid): udid for udid in ids}
for future in as_completed(futures): for future in as_completed(futures):
udid = futures[future] udid = futures[future]
try: try:
code, msg = future.result() code, msg = future.result()
results.append((udid, code, msg))
except Exception as e: except Exception as e:
LogManager.method_error(f"[{udid}] stop 调用异常: {e}", "task") LogManager.method_error(f"[{udid}] stop 调用异常: {e}", "task")
failed.append(udid) failed.append(udid)
continue continue
if code != 200: if code != 200:
failed.append(udid) failed.append(udid)
# 等待所有线程完全停止
for udid, code, msg in results:
if code == 200:
obj = cls._tasks.get(udid)
if obj:
thread = obj["thread"]
while thread.is_alive():
time.sleep(0.1)
if failed: if failed:
return 207, f"部分任务停止失败: {failed}" return 207, f"部分任务停止失败: {failed}"
return 200, "全部停止请求已提交" return 200, "全部任务已成功停止"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 MiB

View File

@@ -20,6 +20,7 @@ from Utils.Requester import Requester
import Entity.Variables as ev import Entity.Variables as ev
from Utils.TencentOCRUtils import TencentOCR from Utils.TencentOCRUtils import TencentOCR
# 脚本管理类 # 脚本管理类
class ScriptManager(): class ScriptManager():
# 单利对象 # 单利对象
@@ -106,10 +107,15 @@ class ScriptManager():
tap_x = int(recomend_cx) if recomend_cx else 100 tap_x = int(recomend_cx) if recomend_cx else 100
tap_y = int(recomend_cy) if recomend_cy else 100 tap_y = int(recomend_cy) if recomend_cy else 100
if click_count: if click_count:
print("点击一次") print("点击一次")
LogManager.method_info("点击一次", "养号", udid)
session.tap(tap_x, tap_y) session.tap(tap_x, tap_y)
else: else:
print("点击两次") print("点击两次")
LogManager.method_info("点击两次", "养号", udid)
session.tap(tap_x, tap_y) session.tap(tap_x, tap_y)
time.sleep(1) time.sleep(1)
session.tap(tap_x, tap_y) session.tap(tap_x, tap_y)
@@ -117,6 +123,7 @@ class ScriptManager():
# 养号 # 养号
def growAccount(self, udid, isComment, event, is_monitoring=False): def growAccount(self, udid, isComment, event, is_monitoring=False):
LogManager.method_info(f"调用刷视频", "养号", udid) LogManager.method_info(f"调用刷视频", "养号", udid)
# ========= 初始化 ========= # ========= 初始化 =========
client = wda.USBClient(udid, ev.wdaFunctionPort) client = wda.USBClient(udid, ev.wdaFunctionPort)
session = client.session() session = client.session()
@@ -125,6 +132,7 @@ class ScriptManager():
while not event.is_set(): while not event.is_set():
try: try:
if not is_monitoring: if not is_monitoring:
LogManager.method_info(f"开始养号重启tiktok", "养号", udid)
ControlUtils.closeTikTok(session, udid) ControlUtils.closeTikTok(session, udid)
event.wait(timeout=1) event.wait(timeout=1)
ControlUtils.openTikTok(session, udid) ControlUtils.openTikTok(session, udid)
@@ -248,12 +256,12 @@ class ScriptManager():
continue continue
except Exception as e: except Exception as e:
print("刷视频脚本有错误:错误内容:",e) print("刷视频脚本有错误:错误内容:", e)
LogManager.method_error(f"刷视频过程出现错误,重试", "养号", udid) LogManager.method_error(f"刷视频过程出现错误,重试", "养号", udid)
raise e # 抛出给上层,触发重生机制 raise e # 抛出给上层,触发重生机制
except Exception as e: except Exception as e:
print("刷视频遇到错误了。错误内容:",e) print("刷视频遇到错误了。错误内容:", e)
LogManager.method_error(f"[{udid}] 养号出现异常,将重启流程: {e}", "养号", udid) LogManager.method_error(f"[{udid}] 养号出现异常,将重启流程: {e}", "养号", udid)
event.wait(timeout=3) event.wait(timeout=3)
@@ -400,13 +408,11 @@ class ScriptManager():
关注打招呼以及回复主播消息 关注打招呼以及回复主播消息
""" """
def safe_greetNewFollowers(self, udid, needReply, isComment, event): def safe_greetNewFollowers(self, udid, needReply, isComment, needTranslate, event):
retries = 0 retries = 0
while not event.is_set(): while not event.is_set():
try: try:
self.greetNewFollowers(udid, needReply, isComment, needTranslate, event)
self.greetNewFollowers(udid, needReply, isComment, event)
except Exception as e: except Exception as e:
retries += 1 retries += 1
LogManager.method_error(f"greetNewFollowers 出现异常: {e},准备第 {retries} 次重试", "关注打招呼", udid) LogManager.method_error(f"greetNewFollowers 出现异常: {e},准备第 {retries} 次重试", "关注打招呼", udid)
@@ -419,7 +425,7 @@ class ScriptManager():
LogManager.method_error("greetNewFollowers 重试次数耗尽,任务终止", "关注打招呼", udid) LogManager.method_error("greetNewFollowers 重试次数耗尽,任务终止", "关注打招呼", udid)
# 关注打招呼 # 关注打招呼
def greetNewFollowers(self, udid, needReply, isComment, event): def greetNewFollowers(self, udid, needReply, isComment, needTranslate, event):
client = wda.USBClient(udid, ev.wdaFunctionPort) client = wda.USBClient(udid, ev.wdaFunctionPort)
session = client.session() session = client.session()
@@ -449,7 +455,7 @@ class ScriptManager():
LogManager.method_info(f"返回上一步", "关注打招呼", udid) LogManager.method_info(f"返回上一步", "关注打招呼", udid)
session.appium_settings({"snapshotMaxDepth": 15}) session.appium_settings({"snapshotMaxDepth": 15})
source = session.source() source = session.source()
LogManager.method_info(f"返回按钮的节点:{source}","返回节点", udid) LogManager.method_info(f"返回按钮的节点:{source}", "返回节点", udid)
ControlUtils.clickBack(session) ControlUtils.clickBack(session)
event.wait(timeout=2) event.wait(timeout=2)
@@ -651,15 +657,12 @@ class ScriptManager():
# LogManager.method_info(f"传递的打招呼的数据:{ev.prologueList}", "关注打招呼", udid) # LogManager.method_info(f"传递的打招呼的数据:{ev.prologueList}", "关注打招呼", udid)
# 取出国家进行对应国家语言代码 # 取出国家进行对应国家语言代码
anchorCountry_code = CountryLanguageMapper.get_language_code(anchorCountry) anchorCountry_code = CountryLanguageMapper.get_language_code(anchorCountry)
LogManager.method_info(f"获取的语言代码是:{ev.prologueList}", "关注打招呼", udid)
LogManager.method_info(f"存储的打招呼语句是:{ev.prologueList}", "关注打招呼", udid)
LogManager.method_info(f"获取的语言代码是:{ev.prologueList}","关注打招呼",udid)
LogManager.method_info(f"存储的打招呼语句是:{ev.prologueList}","关注打招呼",udid)
# 判断对应的语言代码是否在传入的字典中 # 判断对应的语言代码是否在传入的字典中
if anchorCountry_code in ev.prologueList: if anchorCountry_code in ev.prologueList:
@@ -668,33 +671,29 @@ class ScriptManager():
# 进行原本的进行传入 # 进行原本的进行传入
privateMessageList = ev.prologueList[anchorCountry_code] privateMessageList = ev.prologueList[anchorCountry_code]
text = random.choice(privateMessageList)
msg = text
needTranslate = False
else: else:
LogManager.method_info(f"不在存储的字典中 打招呼语句是:{ev.prologueList}", "关注打招呼", udid) LogManager.method_info(f"不在存储的字典中 打招呼语句是:{ev.prologueList}", "关注打招呼", udid)
# 需要翻译 # 需要翻译
privateMessageList = ev.prologueList['yolo'] privateMessageList = ev.prologueList['yolo']
needTranslate = True # 使用yolo必须翻译 # 准备打招呼的文案
text = random.choice(privateMessageList)
LogManager.method_info(f"取出打招呼的数据,{text}", "关注打招呼",
udid)
# 准备打招呼的文案 if needTranslate:
text = random.choice(privateMessageList) LogManager.method_info(f"需要翻译:{text},参数为:国家为{anchorCountry}, 即将进行翻译",
# text = "hello" "关注打招呼", udid)
msg = Requester.translation(text, anchorCountry)
LogManager.method_info(f"取出打招呼的数据,{text}", "关注打招呼", LogManager.method_info(f"翻译成功:{msg}, ", "关注打招呼", udid)
udid) else:
msg = text
if needTranslate: LogManager.method_info(f"即将发送的私信内容:{msg}", "关注打招呼", udid)
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") chatInput = session.xpath("//TextView")
@@ -751,7 +750,6 @@ class ScriptManager():
print("即将要回复消息") print("即将要回复消息")
LogManager.method_info("即将要回复消息", "关注打招呼", udid) LogManager.method_info("即将要回复消息", "关注打招呼", udid)
LogManager.method_info(f"是否需要进行监控消息:{needReply}", "监控消息") LogManager.method_info(f"是否需要进行监控消息:{needReply}", "监控消息")
if needReply: if needReply:
@@ -779,12 +777,12 @@ class ScriptManager():
print("greetNewFollowers方法执行完毕") print("greetNewFollowers方法执行完毕")
def safe_followAndGreetUnion(self, udid, needReply, event): def safe_followAndGreetUnion(self, udid, needReply, needTranslate, event):
retries = 0 retries = 0
while not event.is_set(): while not event.is_set():
try: try:
self.followAndGreetUnion(udid, needReply, event) self.followAndGreetUnion(udid, needReply, needTranslate, event)
except Exception as e: except Exception as e:
retries += 1 retries += 1
@@ -797,7 +795,7 @@ class ScriptManager():
LogManager.method_error("greetNewFollowers 重试次数耗尽,任务终止", "关注打招呼", udid) LogManager.method_error("greetNewFollowers 重试次数耗尽,任务终止", "关注打招呼", udid)
# 关注打招呼以及回复主播消息(联盟号) # 关注打招呼以及回复主播消息(联盟号)
def followAndGreetUnion(self, udid, needReply, event): def followAndGreetUnion(self, udid, needReply, needTranslate, event):
client = wda.USBClient(udid, ev.wdaFunctionPort) client = wda.USBClient(udid, ev.wdaFunctionPort)
session = client.session() session = client.session()
@@ -888,7 +886,6 @@ class ScriptManager():
print(f"找不到输入框") print(f"找不到输入框")
raise Exception("找不到输入框") raise Exception("找不到输入框")
input = session.xpath('//XCUIElementTypeSearchField') input = session.xpath('//XCUIElementTypeSearchField')
if input.exists: if input.exists:
input.clear_text() input.clear_text()
@@ -954,30 +951,25 @@ class ScriptManager():
if anchorCountry_code in ev.prologueList: if anchorCountry_code in ev.prologueList:
# 进行原本的进行传入 # 进行原本的进行传入
privateMessageList = ev.prologueList[anchorCountry_code] privateMessageList = ev.prologueList[anchorCountry_code]
needTranslate = False text = random.choice(privateMessageList)
else:
# 需要翻译
privateMessageList = ev.prologueList['yolo']
needTranslate = True # 使用yolo必须翻译
# 准备打招呼的文案
text = random.choice(privateMessageList)
# text = "hello"
# sweetm_0
if needTranslate:
# 翻译成主播国家的语言
LogManager.method_info(f"需要翻译:{text},参数为:国家为{anchorCountry}, 即将进行翻译",
"关注打招呼(联盟号)", udid)
msg = Requester.translation(text, anchorCountry)
LogManager.method_info(f"翻译成功:{msg}, ", "关注打招呼(联盟号)", udid)
else:
msg = text msg = text
LogManager.method_info(f"即将发送的私信内容:{msg}", "关注打招呼(联盟号)", udid) else:
# 从yolo中拿取
privateMessageList = ev.prologueList['yolo']
text = random.choice(privateMessageList)
if needTranslate:
# 翻译成主播国家的语言
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") chatInput = session.xpath("//TextView")
@@ -1522,7 +1514,6 @@ class ScriptManager():
client = wda.USBClient(udid, ev.wdaFunctionPort) client = wda.USBClient(udid, ev.wdaFunctionPort)
session = client.session() session = client.session()
count = 0 count = 0
while count <= 5: while count <= 5:
try: try: