拆分 douyin_yanghao (922 → 4 子模块 + 788 行原文件保留 yanghao_idle)
把 douyin/other/douyin_yanghao.py 中 6 个独立小函数拆出到
douyin/other/douyin_yanghao_pkg/ 子包,巨型函数 yanghao_idle (756 行)
留在原文件,从子包 import 拿 _yanghao_log/_scaled_click/_do_comment 用。
新建子包:
- logger (40) _yanghao_log + module logger
- click_helpers (75) _scaled_click / _do_comment
- entrypoints (82) yanghao_recommend_flow / yanghao_wait /
yanghao_get_recommend_seconds
(yanghao_recommend_flow 通过 lazy import 调用 yanghao_idle)
外部 import 路径不变:
``from douyin.other.douyin_yanghao import yanghao_recommend_flow / yanghao_wait /
yanghao_get_recommend_seconds`` 等仍可用(原文件 from yanghao_pkg import * re-export)。
- _scaled_click 4 处外部引用 / _yanghao_log 1 处 / 其他 0 处
回归:
- 4 个新文件 + 改写后 douyin_yanghao.py compileall 通过
- pytest 353 passed / 1 skipped
- _yanghao_log() / yanghao_get_recommend_seconds() 调用结果正常
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
badf64998d
commit
246c80498e
5 files changed
+242
-147
No files matched your search
+13
-147
@@ -11,100 +11,25 @@ cv2: Any = _cv2
|
||||
import numpy as np
|
||||
|
||||
|
||||
# 从子包 re-export(保持兼容;yanghao_idle 仍在本文件,需要这些 helper)
|
||||
from douyin.other.douyin_yanghao_pkg import (
|
||||
_LOGGER,
|
||||
_yanghao_log,
|
||||
_scaled_click,
|
||||
_do_comment,
|
||||
yanghao_recommend_flow,
|
||||
yanghao_wait,
|
||||
yanghao_get_recommend_seconds,
|
||||
)
|
||||
|
||||
|
||||
|
||||
# 模块级日志:优先走 analyser._log_kv,回退到 logging(避免裸 print 散落到 stdout)
|
||||
import logging as _logging
|
||||
|
||||
_LOGGER = _logging.getLogger("douyin.other.douyin_yanghao")
|
||||
|
||||
|
||||
def _yanghao_log(analyser, msg: str, level: str = "INFO") -> None:
|
||||
"""统一养号日志出口。
|
||||
|
||||
- 当 analyser 提供 `_log_kv` 时,优先用它(与项目统一日志体系一致)。
|
||||
- 否则回退到模块 logger;最后兜底到 stdout 以避免静默丢日志。
|
||||
"""
|
||||
try:
|
||||
log_kv = getattr(analyser, "_log_kv", None)
|
||||
if callable(log_kv):
|
||||
log_kv("养号", str(msg), level=str(level or "INFO"))
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
if str(level).upper() == "ERROR":
|
||||
_LOGGER.error(str(msg))
|
||||
elif str(level).upper() == "WARNING":
|
||||
_LOGGER.warning(str(msg))
|
||||
else:
|
||||
_LOGGER.info(str(msg))
|
||||
except Exception:
|
||||
try:
|
||||
_yanghao_log(analyser, str(msg))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
def _scaled_click(analyser, base_x: int, base_y: int) -> None:
|
||||
"""模块级点击封装:根据基准坐标缩放并点击(兼容原嵌套实现)。"""
|
||||
sx, sy = analyser._scale_from_base(int(base_x), int(base_y))
|
||||
# 使用底层 click 时不打印底层日志,以避免养号动作产生多余的聚焦/日志噪音
|
||||
try:
|
||||
analyser.operation.click(analyser.device_id, sx, sy, log=False)
|
||||
except TypeError:
|
||||
# 向后兼容:如果底层 click 还没有 log 参数,回退到旧调用
|
||||
analyser.operation.click(analyser.device_id, sx, sy)
|
||||
|
||||
|
||||
def _do_comment(analyser) -> None:
|
||||
"""模块级评论操作:点击评论按钮 -> 输入框 -> 输入文本 -> 发送 -> 关闭"""
|
||||
try:
|
||||
# 步骤1: 点击评论按钮
|
||||
btn_x = int(getattr(analyser, "yanghao_comment_btn_x", 540) or 540)
|
||||
btn_y = int(getattr(analyser, "yanghao_comment_btn_y", 2000) or 2000)
|
||||
_yanghao_log(analyser, f"[养号][评论] Step1: 点击评论按钮 coord=({btn_x},{btn_y})")
|
||||
_scaled_click(analyser, btn_x, btn_y)
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
# 步骤2: 点击输入框
|
||||
input_x = int(getattr(analyser, "yanghao_comment_input_x", 144) or 144)
|
||||
input_y = int(getattr(analyser, "yanghao_comment_input_y", 2179) or 2179)
|
||||
_yanghao_log(analyser, f"[养号][评论] Step2: 点击输入框 coord=({input_x},{input_y})")
|
||||
_scaled_click(analyser, input_x, input_y)
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
# 步骤3: 输入评论文本
|
||||
comment_texts = getattr(analyser, "yanghao_comment_texts", []) or []
|
||||
if not comment_texts:
|
||||
comment_texts = ["加油", "不错", "很好看"]
|
||||
selected_text = random.choice(comment_texts)
|
||||
_yanghao_log(analyser, f"[养号][评论] Step3: 选择文本 -> {selected_text}")
|
||||
|
||||
ix, iy = analyser._scale_from_base(input_x, input_y)
|
||||
ok_input = bool(
|
||||
analyser.operation.paste_text_at(
|
||||
analyser.device_id, ix, iy, selected_text, ensure_focus=False
|
||||
)
|
||||
)
|
||||
_yanghao_log(analyser, f"[养号][评论] paste_text_at -> ok={ok_input} scaled_coord=({ix},{iy})")
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
# 步骤4: 发送评论
|
||||
ok_send = bool(analyser.operation.confirm_from_keyboard(analyser.device_id))
|
||||
_yanghao_log(analyser, f"[养号][评论] Step4: 发送评论 -> ok={ok_send}")
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
# 步骤5: 关闭评论窗口
|
||||
close_x = int(getattr(analyser, "yanghao_comment_close_x", 900) or 900)
|
||||
close_y = int(getattr(analyser, "yanghao_comment_close_y", 300) or 300)
|
||||
_yanghao_log(analyser, f"[养号][评论] Step5: 关闭评论窗口 coord=({close_x},{close_y})")
|
||||
_scaled_click(analyser, close_x, close_y)
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
except Exception as e:
|
||||
_yanghao_log(analyser, f"[养号] 评论操作失败: {e}")
|
||||
|
||||
|
||||
def yanghao_idle(analyser, total_seconds: int) -> None:
|
||||
"""养号等待。
|
||||
|
||||
@@ -861,62 +786,3 @@ def yanghao_idle(analyser, total_seconds: int) -> None:
|
||||
return True, 1.0
|
||||
|
||||
|
||||
def yanghao_recommend_flow(analyser, total_seconds: int) -> None:
|
||||
"""执行推荐页养号流程:退出直播间 -> 进入推荐页 -> 养号。
|
||||
|
||||
每一步操作间隔1秒,确保界面稳定。
|
||||
"""
|
||||
try:
|
||||
# 输出设备分辨率与调试行(兼容原始日志格式)
|
||||
try:
|
||||
resx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||||
resy = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
||||
except Exception:
|
||||
resx, resy = 1080, 2400
|
||||
_yanghao_log(analyser, f"设备分辨率为:{resx}*{resy}")
|
||||
_yanghao_log(analyser, f"[YANGHAO-ONLY] device_id={analyser.device_id} res={resx}x{resy}")
|
||||
_yanghao_log(analyser, "[养号] 开始推荐页养号流程")
|
||||
|
||||
# 步骤1: 发送系统返回键退出当前直播间
|
||||
_yanghao_log(analyser, "[养号] 步骤1: 发送系统返回键退出直播间")
|
||||
analyser.operation.keyevent(analyser.device_id, 4) # KEYCODE_BACK = 4
|
||||
analyser.operation.delay(3.0)
|
||||
|
||||
# 步骤2: 再次发送返回键确保退出
|
||||
_yanghao_log(analyser, "[养号] 步骤2: 再次发送返回键")
|
||||
analyser.operation.keyevent(analyser.device_id, 4) # KEYCODE_BACK = 4
|
||||
analyser.operation.delay(3.0)
|
||||
|
||||
# 步骤3: 在推荐页执行养号
|
||||
_yanghao_log(analyser, f"[养号] 步骤3: 在推荐页养号 {total_seconds} 秒")
|
||||
analyser.operation.delay(3.0)
|
||||
yanghao_idle(analyser, total_seconds)
|
||||
|
||||
_yanghao_log(analyser, "[养号] 推荐页养号流程完成")
|
||||
|
||||
except Exception as e:
|
||||
_yanghao_log(analyser, f"[养号] 推荐页养号流程失败: {e}")
|
||||
# 失败时退化为普通养号
|
||||
yanghao_idle(analyser, total_seconds)
|
||||
|
||||
|
||||
def yanghao_wait(analyser, total_seconds: int) -> None:
|
||||
"""养号等待函数(兼容接口)。"""
|
||||
if total_seconds <= 0:
|
||||
return
|
||||
|
||||
# 检查是否启用养号
|
||||
if not bool(getattr(analyser, "yanghao_enabled", True)):
|
||||
_yanghao_log(analyser, "[养号] yanghao_enabled=0,跳过推荐页养号(快速继续)")
|
||||
return
|
||||
|
||||
# 执行推荐页养号流程
|
||||
yanghao_recommend_flow(analyser, total_seconds)
|
||||
|
||||
|
||||
def yanghao_get_recommend_seconds(analyser) -> int:
|
||||
"""返回推荐页养号的默认时长(兼容接口)。"""
|
||||
try:
|
||||
return int(getattr(analyser, "yanghao_recommend_duration_seconds", 60) or 60)
|
||||
except Exception:
|
||||
return 60
|
||||
@@ -0,0 +1,32 @@
|
||||
"""douyin_yanghao_pkg - 推荐页养号子包。
|
||||
|
||||
原 douyin/other/douyin_yanghao.py 拆出 6 个独立函数:
|
||||
|
||||
- logger _yanghao_log
|
||||
- click_helpers _scaled_click / _do_comment
|
||||
- entrypoints yanghao_recommend_flow / yanghao_wait / yanghao_get_recommend_seconds
|
||||
|
||||
巨型函数 yanghao_idle (756 行) 留在原文件,从子包 import _yanghao_log /
|
||||
_scaled_click / _do_comment 即可正常工作。
|
||||
|
||||
外部 import 路径不变:
|
||||
``from douyin.other.douyin_yanghao import yanghao_recommend_flow`` 仍可用。
|
||||
"""
|
||||
|
||||
from .logger import _LOGGER, _yanghao_log
|
||||
from .click_helpers import _do_comment, _scaled_click
|
||||
from .entrypoints import (
|
||||
yanghao_get_recommend_seconds,
|
||||
yanghao_recommend_flow,
|
||||
yanghao_wait,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"_LOGGER",
|
||||
"_yanghao_log",
|
||||
"_scaled_click",
|
||||
"_do_comment",
|
||||
"yanghao_recommend_flow",
|
||||
"yanghao_wait",
|
||||
"yanghao_get_recommend_seconds",
|
||||
]
|
||||
@@ -0,0 +1,75 @@
|
||||
"""douyin_yanghao_pkg.click_helpers - 点击 / 评论操作辅助。
|
||||
|
||||
- _scaled_click: 模块级点击封装,自动按分辨率缩放坐标。
|
||||
- _do_comment: 模块级评论操作(点击评论按钮 → 输入框 → 输入 → 发送 → 关闭)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
from typing import Any
|
||||
|
||||
from .logger import _yanghao_log
|
||||
|
||||
|
||||
def _scaled_click(analyser, base_x: int, base_y: int) -> None:
|
||||
"""模块级点击封装:根据基准坐标缩放并点击(兼容原嵌套实现)。"""
|
||||
sx, sy = analyser._scale_from_base(int(base_x), int(base_y))
|
||||
# 使用底层 click 时不打印底层日志,以避免养号动作产生多余的聚焦/日志噪音
|
||||
try:
|
||||
analyser.operation.click(analyser.device_id, sx, sy, log=False)
|
||||
except TypeError:
|
||||
# 向后兼容:如果底层 click 还没有 log 参数,回退到旧调用
|
||||
analyser.operation.click(analyser.device_id, sx, sy)
|
||||
|
||||
|
||||
|
||||
|
||||
def _do_comment(analyser) -> None:
|
||||
"""模块级评论操作:点击评论按钮 -> 输入框 -> 输入文本 -> 发送 -> 关闭"""
|
||||
try:
|
||||
# 步骤1: 点击评论按钮
|
||||
btn_x = int(getattr(analyser, "yanghao_comment_btn_x", 540) or 540)
|
||||
btn_y = int(getattr(analyser, "yanghao_comment_btn_y", 2000) or 2000)
|
||||
_yanghao_log(analyser, f"[养号][评论] Step1: 点击评论按钮 coord=({btn_x},{btn_y})")
|
||||
_scaled_click(analyser, btn_x, btn_y)
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
# 步骤2: 点击输入框
|
||||
input_x = int(getattr(analyser, "yanghao_comment_input_x", 144) or 144)
|
||||
input_y = int(getattr(analyser, "yanghao_comment_input_y", 2179) or 2179)
|
||||
_yanghao_log(analyser, f"[养号][评论] Step2: 点击输入框 coord=({input_x},{input_y})")
|
||||
_scaled_click(analyser, input_x, input_y)
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
# 步骤3: 输入评论文本
|
||||
comment_texts = getattr(analyser, "yanghao_comment_texts", []) or []
|
||||
if not comment_texts:
|
||||
comment_texts = ["加油", "不错", "很好看"]
|
||||
selected_text = random.choice(comment_texts)
|
||||
_yanghao_log(analyser, f"[养号][评论] Step3: 选择文本 -> {selected_text}")
|
||||
|
||||
ix, iy = analyser._scale_from_base(input_x, input_y)
|
||||
ok_input = bool(
|
||||
analyser.operation.paste_text_at(
|
||||
analyser.device_id, ix, iy, selected_text, ensure_focus=False
|
||||
)
|
||||
)
|
||||
_yanghao_log(analyser, f"[养号][评论] paste_text_at -> ok={ok_input} scaled_coord=({ix},{iy})")
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
# 步骤4: 发送评论
|
||||
ok_send = bool(analyser.operation.confirm_from_keyboard(analyser.device_id))
|
||||
_yanghao_log(analyser, f"[养号][评论] Step4: 发送评论 -> ok={ok_send}")
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
# 步骤5: 关闭评论窗口
|
||||
close_x = int(getattr(analyser, "yanghao_comment_close_x", 900) or 900)
|
||||
close_y = int(getattr(analyser, "yanghao_comment_close_y", 300) or 300)
|
||||
_yanghao_log(analyser, f"[养号][评论] Step5: 关闭评论窗口 coord=({close_x},{close_y})")
|
||||
_scaled_click(analyser, close_x, close_y)
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
except Exception as e:
|
||||
_yanghao_log(analyser, f"[养号] 评论操作失败: {e}")
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
"""douyin_yanghao_pkg.entrypoints - 养号入口函数。
|
||||
|
||||
- yanghao_recommend_flow: 推荐页养号流程(退出直播间 → 进推荐页 → 养号)
|
||||
- yanghao_wait: 养号等待函数(兼容接口)
|
||||
- yanghao_get_recommend_seconds: 推荐页养号默认时长读取
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from .logger import _yanghao_log
|
||||
|
||||
|
||||
def _yanghao_idle_from_main(analyser, total_seconds: int) -> None:
|
||||
"""懒加载 yanghao_idle(避免循环依赖:yanghao_idle 仍在原文件)。"""
|
||||
from douyin.other.douyin_yanghao import yanghao_idle as _impl
|
||||
_impl(analyser, total_seconds)
|
||||
|
||||
|
||||
def yanghao_recommend_flow(analyser, total_seconds: int) -> None:
|
||||
"""执行推荐页养号流程:退出直播间 -> 进入推荐页 -> 养号。
|
||||
|
||||
每一步操作间隔1秒,确保界面稳定。
|
||||
"""
|
||||
try:
|
||||
# 输出设备分辨率与调试行(兼容原始日志格式)
|
||||
try:
|
||||
resx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||||
resy = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
||||
except Exception:
|
||||
resx, resy = 1080, 2400
|
||||
_yanghao_log(analyser, f"设备分辨率为:{resx}*{resy}")
|
||||
_yanghao_log(analyser, f"[YANGHAO-ONLY] device_id={analyser.device_id} res={resx}x{resy}")
|
||||
_yanghao_log(analyser, "[养号] 开始推荐页养号流程")
|
||||
|
||||
# 步骤1: 发送系统返回键退出当前直播间
|
||||
_yanghao_log(analyser, "[养号] 步骤1: 发送系统返回键退出直播间")
|
||||
analyser.operation.keyevent(analyser.device_id, 4) # KEYCODE_BACK = 4
|
||||
analyser.operation.delay(3.0)
|
||||
|
||||
# 步骤2: 再次发送返回键确保退出
|
||||
_yanghao_log(analyser, "[养号] 步骤2: 再次发送返回键")
|
||||
analyser.operation.keyevent(analyser.device_id, 4) # KEYCODE_BACK = 4
|
||||
analyser.operation.delay(3.0)
|
||||
|
||||
# 步骤3: 在推荐页执行养号
|
||||
_yanghao_log(analyser, f"[养号] 步骤3: 在推荐页养号 {total_seconds} 秒")
|
||||
analyser.operation.delay(3.0)
|
||||
_yanghao_idle_from_main(analyser, total_seconds)
|
||||
|
||||
_yanghao_log(analyser, "[养号] 推荐页养号流程完成")
|
||||
|
||||
except Exception as e:
|
||||
_yanghao_log(analyser, f"[养号] 推荐页养号流程失败: {e}")
|
||||
# 失败时退化为普通养号
|
||||
_yanghao_idle_from_main(analyser, total_seconds)
|
||||
|
||||
|
||||
|
||||
|
||||
def yanghao_wait(analyser, total_seconds: int) -> None:
|
||||
"""养号等待函数(兼容接口)。"""
|
||||
if total_seconds <= 0:
|
||||
return
|
||||
|
||||
# 检查是否启用养号
|
||||
if not bool(getattr(analyser, "yanghao_enabled", True)):
|
||||
_yanghao_log(analyser, "[养号] yanghao_enabled=0,跳过推荐页养号(快速继续)")
|
||||
return
|
||||
|
||||
# 执行推荐页养号流程
|
||||
yanghao_recommend_flow(analyser, total_seconds)
|
||||
|
||||
|
||||
|
||||
|
||||
def yanghao_get_recommend_seconds(analyser) -> int:
|
||||
"""返回推荐页养号的默认时长(兼容接口)。"""
|
||||
try:
|
||||
return int(getattr(analyser, "yanghao_recommend_duration_seconds", 60) or 60)
|
||||
except Exception:
|
||||
return 60
|
||||
@@ -0,0 +1,40 @@
|
||||
"""douyin_yanghao_pkg.logger - 模块级日志输出。
|
||||
|
||||
_yanghao_log: 优先走 analyser._log_kv(项目统一日志体系),
|
||||
失败兜底到 logging logger,再失败兜底 stdout。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging as _logging
|
||||
|
||||
_LOGGER = _logging.getLogger("douyin.other.douyin_yanghao")
|
||||
|
||||
|
||||
def _yanghao_log(analyser, msg: str, level: str = "INFO") -> None:
|
||||
"""统一养号日志出口。
|
||||
|
||||
- 当 analyser 提供 `_log_kv` 时,优先用它(与项目统一日志体系一致)。
|
||||
- 否则回退到模块 logger;最后兜底到 stdout 以避免静默丢日志。
|
||||
"""
|
||||
try:
|
||||
log_kv = getattr(analyser, "_log_kv", None)
|
||||
if callable(log_kv):
|
||||
log_kv("养号", str(msg), level=str(level or "INFO"))
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
if str(level).upper() == "ERROR":
|
||||
_LOGGER.error(str(msg))
|
||||
elif str(level).upper() == "WARNING":
|
||||
_LOGGER.warning(str(msg))
|
||||
else:
|
||||
_LOGGER.info(str(msg))
|
||||
except Exception:
|
||||
try:
|
||||
_yanghao_log(analyser, str(msg))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user