import pyautogui import time import subprocess import sys from pathlib import Path from typing import Optional, Tuple BASE_DIR = Path(__file__).parent.parent.parent sys.path.insert(0, str(BASE_DIR)) from wechat_auto.config import settings from wechat_auto.utils.logger import logger class DesktopAutomation: """桌面自动化操作 - 点击微信、进入小程序、打开一见星球""" def __init__(self): pyautogui.FAILSAFE = settings.failsafe pyautogui.PAUSE = 0.5 self.screenshot_dir = Path(settings.screenshot_dir) self.screenshot_dir.mkdir(parents=True, exist_ok=True) # 微信窗口已知位置(从xwininfo获取) self.wechat_window = { 'x': 877, 'y': 207, 'width': 980, 'height': 710 } def click_at(self, x: int, y: int): """在指定位置点击""" pyautogui.click(x, y) logger.info(f"点击坐标:({x}, {y})") def screenshot(self, name: str): """截图保存""" filepath = self.screenshot_dir / f"{name}_{time.strftime('%Y%m%d_%H%M%S')}.png" try: subprocess.run(['scrot', str(filepath)], capture_output=True, timeout=5) except FileNotFoundError: try: subprocess.run(['gnome-screenshot', '-f', str(filepath)], capture_output=True, timeout=5) except: pass logger.info(f"截图:{filepath}") def get_screen_size(self) -> Tuple[int, int]: """获取屏幕尺寸""" return pyautogui.size() def open_wechat_and_miniprogram(self) -> bool: """ 打开微信并进入一见星球小程序 流程: 1. 点击桌面微信图标 2. 等待微信窗口 3. 点击左侧小程序图标 4. 点击一见星球 """ screen_width, screen_height = self.get_screen_size() logger.info(f"屏幕尺寸:{screen_width}x{screen_height}") self.screenshot("step0_start") # 步骤1:点击桌面微信图标 logger.info("步骤1:点击桌面微信图标") self.click_at(int(screen_width * 0.15), int(screen_height * 0.15)) time.sleep(4) self.screenshot("step1_click_wechat") # 步骤2:点击左侧小程序图标 # 微信窗口内相对位置 wx = self.wechat_window['x'] wy = self.wechat_window['y'] ww = self.wechat_window['width'] wh = self.wechat_window['height'] logger.info("步骤2:点击左侧小程序图标") # 小程序图标在左侧边栏,约为窗口宽度的4%,高度的22% mini_x = wx + int(ww * 0.04) mini_y = wy + int(wh * 0.22) self.click_at(mini_x, mini_y) time.sleep(2) self.screenshot("step2_miniprogram_panel") # 步骤3:点击一见星球小程序 # 一见星球在主面板中,约为窗口宽度的35%,高度的25% logger.info("步骤3:点击一见星球小程序") planet_x = wx + int(ww * 0.35) planet_y = wy + int(wh * 0.25) self.click_at(planet_x, planet_y) time.sleep(3) self.screenshot("step3_yijian_planet") logger.info("✅ 已成功打开一见星球小程序!") return True if __name__ == "__main__": automation = DesktopAutomation() print("=" * 50) print("开始执行:打开微信 -> 进入小程序 -> 一见星球") print("=" * 50) result = automation.open_wechat_and_miniprogram() if result: print("\n✅ 成功完成!") else: print("\n❌ 执行失败,请检查日志")