15 Commits
Author SHA1 Message Date
CodexandClaude Sonnet 4.5 703da834fb fix(security): 修复 shell 注入风险和类型转换未捕获问题
- path_config.py: find_executable Unix 回退路径使用 shlex.quote 防注入
- underlying_operations.py: try_set_ime IME ID 使用 shlex.quote 防注入
- screenshot_direct.py: 环境变量 float/int 转换添加 try/except 容错
- device_select.py: 用户输入 int() 转换添加 try/except 容错
- read_once.py / image_acquirer.py / detail_verifier.py: PIL Image 添加 close()
- detector.py: 移除 return 后不可达死代码
- optimized_entry.py: 统一返回值类型 int,失败时返回 0
- read_once.py: 移除 try/except 中重复的相同代码

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-05-23 20:50:26 +08:00
CodexandClaude Opus 4.7 e46497ea80 refactor(underlying): 抽 cut_pic 图片裁剪到 image_ops (1574 → 1327)
underlying_operations.cut_pic L474-721 是 248 行图片裁剪管道:
- 默认/命名两个分支共享重试 3 次的截图加载验证
- 默认分支裁出 cut.png 或 ocr/<name>.png;命名分支裁出 pic/<target>/<name>.png
- 写入 _ocr_coords_cache 供 analyse_pic_word 调试图像引用
- SAVE_CUT_PIC_DEBUG_IMAGES=1 时保存 cut_pic 调试图

抽到 underlying_operations_pkg/image_ops.py:
- cut_pic(self, left_up, right_down, target, name) -> bool
- _ensure_parent_dir / _validate_coords / _save_cut_debug_images 内部 helper

主类方法替换为 4 行委托。

更新两个旧的源码断言测试以适配抽离:
- test_cut_pic_path:从 inspect.getsource(op.cut_pic) 改为读 image_ops.py 文件源码
- test_runtime_path_anchors:把 path_config.get_pic_dir() 检查范围从单一文件
  扩展到 underlying_operations.py + image_ops.py + ocr_pipeline.py 三处

1361 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 02:43:36 +08:00
CodexandClaude Opus 4.7 8457219241 refactor(underlying): 抽 analyse_pic_word OCR 管道到 ocr_pipeline (1939 → 1560)
underlying_operations.analyse_pic_word L723-1102 是 380 行 OCR 主管道:
路径解析 + 字节缓存 + PIL 加载 + change_color 预处理 + 调试图像保存
+ EasyOCR/PaddleOCR 调用 + 置信度提取 + 缓存写回 + 成功识别落盘。

抽到 underlying_operations_pkg/ocr_pipeline.py:
- analyse_pic_word(self, picname, change_color, pic_path, return_confidence)
- _save_debug_preprocessed: 预处理后的调试图保存(仅 SAVE_OCR_DEBUG_IMAGES=1)
- _save_debug_after_ocr: 含 OCR 文本预览 + 置信度的 after_ocr 调试图
- _save_success_pic: 成功识别落盘到 ocr/<category>/<picname>_success.png

主类方法替换为 4 行委托。原行为完全保留:缓存 / 二值化 / EasyOCR vs Paddle
predict/ocr 兜底 / [None] 短路 / 异常返回空 / picname 失败诊断日志。

新增 16 个单元测试覆盖:
- 早出 4 种(pic_path 空/不存在/带 confidence/OCR 未初始化)
- OCR 调用路径(EasyOCR / Paddle predict / Paddle ocr 兜底 / [None] 短路 / 异常)
- 缓存命中跳过 OCR / 缓存未命中写回
- change_color 0/1/2 三种模式
- return_confidence True/False 返回类型
- autouse fixture 清 OCR 模块级缓存防止测试污染

1361 passed (+16)。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 02:36:47 +08:00
CodexandClaude Opus 4.7 07eee6da52 fix(paths): 修同类型 5 处路径推导 bug + 加回归测试
cut_pic 路径 bug 之后再做地毯式排查,发现 5 处类似潜在 bug:
fallback 分支用 dirname(__file__) 推根级目录,path_config 不可用时
会指向错误位置(推到 douyin/core/pic 等子目录而非项目根 pic/)。

修复:
- underlying_operations.py: screenshot_calls.jsonl 路径 + OCR category 目录
  改用 path_config.get_logs_dir() / get_pic_dir()
- douyin_fudai.py: 2 处 actions.log 兜底路径
  优先 path_config.get_actions_log_path(),兜底加 ".." 回到项目根
- fudai_detail_room_title.py: 截图复用 fallback 路径加 ".." 回退
- fudai_detail_orchestrator_pkg/read_once.py: 同上 2 处 fallback

新增 11 个回归测试覆盖:
- 关键文件不再用单步 dirname/__file__/pic 写法
- path_config 仍把所有路径锚到 PROJECT_ROOT
- 兜底分支必须含 ".." 表示回退

注:douyin_fudai.py:299 的 reservations.json 因 dirname 恰好等于
PROJECT_ROOT/douyin/core,跟 path_config.get_reservations_path() 一致,
不是 bug,未改动。

1226 passed (+11 新增)。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 01:21:27 +08:00
CodexandClaude Opus 4.7 4ab4504425 fix(cut_pic): 修截图路径错位 — pic 目录从 douyin/core/pic 改回项目根 pic
cut_pic 历史 bug:用 os.path.dirname(__file__) + "/pic" 推路径,
误指向 douyin/core/pic/(不存在),实际截图保存在 <project_root>/pic/。
日志中频繁出现"截图文件不存在: .../douyin/core/pic/screenshot.png"。

改为:
- 默认分支用 path_config.get_pic_dir() / get_screenshot_path()
- target 分支用 os.path.join(path_config.get_pic_dir(), target)

补 5 个回归测试覆盖:
- 源码不再含旧的 dirname-based 路径
- path_config 路径解析到 <project_root>/pic
- 截图缺失时 cut_pic 返回 False(重试 3 次后兜底)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 00:47:02 +08:00
CodexandClaude Opus 4.7 fb82213d53 refactor(underlying): 抽 _get_screenshot_direct 到 underlying_operations_pkg/screenshot_direct (2416 → 1930)
497 行的 ADB 截图实现(含 exec-out 主路径 + 多重兜底 + watchdog)整段抽到
underlying_operations_pkg/screenshot_direct.capture_screenshot_direct,
原方法保留 9 行 thin shim:

    def _get_screenshot_direct(self, device_id, path="pic"):
        from douyin.core.underlying_operations_pkg.screenshot_direct import (
            capture_screenshot_direct,
        )
        return capture_screenshot_direct(self, device_id, path)

净减 486 行。函数体过深的两层嵌套截图逻辑现在独立成模块,方便单独测试与改进。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 22:55:34 +08:00
CodexandClaude Opus 4.7 4c1d95d4b5 feat(recovery): 桌面自救改用包名启动抖音,点击坐标降为兜底
当检测到不在抖音前台时,原先点击桌面图标 + 直播 + 进入直播间 3 步坐标的方式
依赖图标位置和分辨率,不同设备/桌面常常失败。改为优先调用 ADB 包名启动:

- underlying_operations 新增 launch_package(device_id, pkg):
  monkey -p <pkg> -c android.intent.category.LAUNCHER 1(主路径)
  失败回退到 am start -n <pkg>/<MainActivity>(多个常见 activity 兜底)
  默认按 com.ss.android.ugc.aweme / .lite / TikTok 顺序尝试

- 新增 is_douyin_foreground 便捷方法

- 改造 3 处自救点:
  fudai_modules/run_once_basic_helpers.py:handle_desktop_recovery
  douyin/other/douyin_yanghao.py(养号循环 + _yanghao_check_and_recover_from_desktop ×2)
  包名启动失败时仍走旧的点击逻辑作为兜底,向后兼容

新增 12 个单元测试覆盖 launch_package 主路径/兜底/失败和自救新行为。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 19:08:09 +08:00
CodexandClaude Opus 4.7 a337b4b93d underlying_operations 第三轮拆分(2434 → 2351,再瘦身 83 行)
新增 2 个子模块:

- device_select (新, 73)  select_device 设备选择交互(无 self.X)
- ocr_digits (新, 49)     analyse_pic_digits 数字 OCR 识别
                         (保留 analyser 参数以调 analyser.analyse_pic_word)

策略:
- select_device 的 self.X 全是 _adb_path / _adb_args,已经是 module-level,
  搬出后改用 from .adb_helpers import _adb_path, _adb_args 即可。
- analyse_pic_digits 用 self.analyse_pic_word(递归调用类内大方法)。
  搬到 module-level 后第一参数从 self 改名 analyser,类内 forwarding
  传 self 即可,外部 op.analyse_pic_digits(picname) 完全兼容。

回归:
- 4 个新/改文件 compileall 通过
- pytest 353 passed / 1 skipped
- underlying_operations.select_device / analyse_pic_digits 类方法和 module-level
  函数都可访问

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 20:12:40 +08:00
CodexandClaude Opus 4.7 57eacb3219 underlying_operations 第二轮拆分(2582 → 2434,再瘦身 148 行)
新增 3 个子模块 + 1 个函数搬到已有子模块:

- adb_helpers (38)         新增 _adb_args(构造 adb -s 命令行)
- reward_pic (新, 73)      save_reward_pic(中奖截图保存)
- swipe_sendevent (新, 47) _swipe_with_sendevent_improved(sendevent 滑动)
- clipboard (新, 75)       set_clipboard_text(设备剪贴板设置)

策略:4 个方法的 self.X 引用都是 self._adb_path / self._adb_args 这种
"无状态工具方法",可以替换为 module-level 函数调用。类内方法保留为
1 行 forwarding 调用 _impl_X。

外部调用全部不需修改:
- save_reward_pic 4 处外部 self.save_reward_pic 仍工作
- _adb_args / _swipe_with_sendevent_improved / set_clipboard_text 0 外部引用

回归:
- 5 个新/改文件 compileall 通过
- pytest 353 passed / 1 skipped
- 类的 _adb_args / save_reward_pic / set_clipboard_text /
  _swipe_with_sendevent_improved 4 个 forwarding 全部正确

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 20:07:23 +08:00
CodexandClaude Opus 4.7 254820e6dc 拆分 underlying_operations (3021 → 5 子模块 + 2582 行原文件)
把 douyin/core/underlying_operations.py 中 6 个零 self 引用的方法搬到
douyin/core/underlying_operations_pkg/ 子包,类内方法变成 1 行 forwarding。
原文件保留 underlying_operations 巨型类(>50 个实例方法用 self)。

新建子包:
- adb_helpers (34)   _adb_path / _pick_adbkeyboard_ime
- pic_path (65)      _find_pic_path 按名称查找图片路径
- ocr_content (228)  extract_ocr_content OCR 文本拼接 + 置信度
- countdown (179)    check_countdown 倒计时文本解析(含 OCR 字符纠错)
- bounds (17)        _parse_bounds 解析 UI dump 的 [x1,y1][x2,y2]

策略:6 个方法的 self 引用全为 0,纯算法逻辑直接搬到 module-level;
类内对应方法保留装饰器和签名,body 改为 1 行 forwarding 调用:
  @staticmethod
  def _adb_path() -> str:
      return _impl_adb_path()

外部调用全部不需修改:
- ``self._find_pic_path(...)``         实例方法 forwarding 仍工作
- ``cls._adb_path()``                  staticmethod forwarding 仍工作
- ``ReservationHandler._parse_bounds(...)`` 外部 5 处引用零修改

修复发现:
- countdown.py 原本 ``import datetime``(应为 ``from datetime import datetime,
  timedelta``),导致 datetime.now()/timedelta() 触发 NameError 后被外层
  except 吞掉返回 False。修正后 check_countdown('5:30') 正确返回 (330, <ts>)
- 子模块内部用 _uo_log,加本地实现避免对原文件的循环依赖

回归:
- 6 个新文件 + 改写后 underlying_operations.py compileall 通过
- pytest 353 passed / 1 skipped
- check_countdown / extract_ocr_content / _parse_bounds /
  _pick_adbkeyboard_ime / _find_pic_path / _adb_path 6 个 forwarding 全部正确

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 19:54:22 +08:00
CodexandClaude Opus 4.7 c5c8def884 underlying_operations: 52 处 print → _uo_log
- 新增模块级 _uo_log(msg, level):与项目其他 print→logger 改造一致的模式,
  优先走 logging(带 [OCR]/[截图]/[Underlying]/[警告]/[错误] 前缀),失败兜底 stdout
- 替换的内容包括:
  - OCR 引擎初始化 / 延迟初始化(PaddleOCR / EasyOCR 切换)
  - exec-out 截图 / 文件替换 / 回退方案的失败日志
  - 滑动操作失败 / ADB 输入注入异常
  - 中奖截图保存 / OCR 调试输出
  - paste_text_at / click 等底层操作的调试输出
- 保留为 stdout:
  - 设备选择交互(L220-247,含 input() 提示,需要用户在 stdout 看到)
  - _uo_log helper 自身的最终兜底(L73)

回归:146 .py 全量编译通过;underlying_operations 类与 _LOGGER 加载正确;
test_filter_logging / check_web_gui_config_sync 通过。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 12:54:27 +08:00
CodexandClaude Opus 4.7 4f147d214e P2 §5.3: 4 处单点 importlib.import_module → 静态 from import
把单次调用 douyin_screenshot_manager 的 importlib 改为 from-import:
  - douyin/core/douyin_flow_control.py
  - douyin/core/underlying_operations.py
  - douyin/fudai/core/fudai_debug_utils.py
  - douyin/stage/douyin_stage.py

douyin_screenshot_manager 只引用 douyin.core.path_config,与上述 4 个文件
无循环依赖,可以安全静态化。bin/run_cli.py 的 importlib 是有意为之
(绕过 package __init__ 遮蔽),保留。

收益:
  - 可读性 + IDE 跳转/类型提示
  - 每次调用省去 ~100µs 的 import_module 开销

回归:140 .py 编译通过;4 处 importlib 残留全清;
test_filter_logging / check_web_gui_config_sync 通过;
douyin_screenshot_manager 仍可正常加载。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 12:01:16 +08:00
Codex 0131be7869 切换 OCR 默认引擎到 PaddleOCR v4-mobile 并简化预处理
- 默认引擎改为 PaddleOCR PP-OCRv4_mobile(实测 369ms/张,比 EasyOCR 629ms 更快且精度大幅提升,coupon 等关键图错字一片 -> 全对)
- EasyOCR 仅作回退,移除"小图自动放大+锐化+对比度增强"预处理(实测对 Paddle 是负优化,会引入伪影/重复字)
- analyse_pic_word 加 LRU 内容哈希缓存(256 条),重复读图直接命中
- analyse_pic_digits 从 200 行重复实现精简成 30 行委托 + 数字过滤
- 统一两处 OCR 初始化为 _build_paddle / _build_easyocr 工厂,消除重复
- 设置 FLAGS_use_mkldnn=false 绕开 Win + Paddle 3.x 的 oneDNN+PIR 不兼容 bug(实测 FLAGS_enable_pir_in_executor=0 也修不掉,关 oneDNN 是当前唯一可行解)
- 决策依据/速度数据/已知问题归档到 docs/ocr_engine_decision.md
- 业务接口完全向后兼容
2026-05-21 10:50:30 +08:00
CodexandClaude Opus 4.5 2d6cbecf7e refactor: 抽取 underlying_operations 顶部辅助函数到 io_utils.py
将 4 个模块级辅助函数及相关常量从 underlying_operations.py 抽离到
新建的 douyin/core/io_utils.py:
- _verify_png_integrity
- _safe_replace_file
- _ensure_parent_dir
- _mirror_screenshot_to_external
- _EXTERNAL_DOUYIN_FUDAI_DIR / _EXTERNAL_AXF_SCREENSHOT

underlying_operations.py 仍重新导出这些符号,保持向后兼容,
所有外部 import 无需修改。

文件行数:3508 -> 3342(减 166 行)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-21 09:01:14 +08:00
CodexandClaude Opus 4.5 8fe7ff0585 refactor: 根目录核心模块迁移到 douyin/core/ 和 douyin/logging/
将 4 个根目录核心模块迁移到合理位置:
- path_config.py    -> douyin/core/path_config.py
- version.py        -> douyin/core/version.py
- Underlying_Operations.py -> douyin/core/underlying_operations.py
- unified_logger.py -> douyin/logging/unified_logger.py

配套更新所有 import:
- path_config: 41 处 import 改为 douyin.core.path_config
- version: 4 处改为 douyin.core.version
- unified_logger: 5 处改为 douyin.logging.unified_logger
- Underlying_Operations: 14 处改为 douyin.core.underlying_operations

修复 path_config.PROJECT_ROOT 计算(向上 2 级而非当前目录)。

验证:python -m compileall 全项目通过。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-21 00:23:14 +08:00