123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- import hug
- from bottle import run
- import pyautogui
- import random
- import time
- import os
- import win32gui
- import win32con
- import autoit
- import datetime
- # 配置参数
- CONFIG = {
- "exe_path": r"C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE",
- "no_auth_button": {"x": 1150, "y": 481},
- "source_button": {"x": 495, "y": 37},
- "refresh_icon_button": {"x": 583, "y": 112},
- "refresh_all_button": {"x": 616, "y": 157},
- "random_click_range": {"min_x": 450, "max_x": 650, "min_y": 450, "max_y": 650},
- "get_data_timeout_time": 20,
- "screen_resolution": {"width": 1920, "height": 1080}, # 默认分辨率
- }
- hug.API(__name__).http.output_format = hug.output_format.json
- def scale_coordinates(x, y):
- """根据屏幕分辨率动态缩放坐标"""
- screen_width, screen_height = pyautogui.size()
- scale_x = screen_width / CONFIG["screen_resolution"]["width"]
- scale_y = screen_height / CONFIG["screen_resolution"]["height"]
- return int(x * scale_x), int(y * scale_y)
- def random_click(min_x, max_x, min_y, max_y, duration=0.5):
- """随机点击指定范围内的坐标"""
- point_x = random.randint(min_x, max_x)
- point_y = random.randint(min_y, max_y)
- pyautogui.moveTo(point_x, point_y, duration)
- pyautogui.click()
- print(f"随机点击:X={point_x}, Y={point_y}")
- return point_x, point_y
- def open_excel_file(file_path):
- """使用 Excel 打开文件,并动态等待窗口激活"""
- try:
- file_name_ext = os.path.basename(file_path)
- print(f"尝试打开文件:{file_name_ext}")
- # 启动 Excel 并打开文件
- autoit.run(f'{CONFIG["exe_path"]} "{file_path}"')
- time.sleep(15)
- # 动态等待窗口激活,最多等待 20 秒
- # if not autoit.win_wait_active(file_name_ext, timeout=20):
- # print(f"窗口激活超时:{file_name_ext}")
- # return False
- # 最大化窗口
- hwnd = win32gui.GetForegroundWindow()
- win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
- time.sleep(1) # 短暂等待窗口最大化完成
- print(f"成功打开并最大化窗口:{file_name_ext}")
- except Exception as e:
- print(f"打开文件失败:{e}")
- return False
- return True
- @hug.get('/radish_research/server')
- def radish_research_server():
- return 1
- @hug.get('/radish_research/refresh')
- def radish_research_refresh(FilePath):
- pyautogui.FAILSAFE = False
- FilePath = FilePath.replace('"', '')
- # 检查文件路径
- if not os.path.exists(FilePath) or ".xlsx" not in FilePath \
- or "~$" in FilePath or "template" in FilePath:
- print(f"文件无效:{FilePath}")
- return False
- # 按钮坐标
- no_auth_button = CONFIG["no_auth_button"]["x"], CONFIG["no_auth_button"]["y"]
- source_button = CONFIG["source_button"]["x"], CONFIG["source_button"]["y"]
- refresh_icon_button = CONFIG["refresh_icon_button"]["x"], CONFIG["refresh_icon_button"]["y"]
- refresh_all_button = CONFIG["refresh_all_button"]["x"], CONFIG["refresh_all_button"]["y"]
- # 打开桌面并点击空白区域
- pyautogui.hotkey('win', 'd')
- move_x, move_y = scale_coordinates(1100, 600)
- pyautogui.moveTo(move_x, move_y, 0.5)
- pyautogui.click(move_x, move_y)
- try:
- # 打开文件
- if not open_excel_file(FilePath):
- return False
- # 随机点击单元格区域
- for _ in range(random.randint(2, 3)):
- random_click(
- CONFIG["random_click_range"]["min_x"],
- CONFIG["random_click_range"]["max_x"],
- CONFIG["random_click_range"]["min_y"],
- CONFIG["random_click_range"]["max_y"]
- )
- time.sleep(1)
- # 处理无权限弹框
- print("开始点击无权限按钮")
- pyautogui.moveTo(*no_auth_button, 0.5)
- pyautogui.click()
- time.sleep(2)
- print("结束点击无权限按钮")
- # 点击顶部萝卜投研按钮
- print("开始点击数据按钮")
- pyautogui.moveTo(*source_button, 0.5)
- pyautogui.click()
- time.sleep(2)
- print("结束点击数据按钮")
- # 点击刷新小箭头
- print("开始点击刷新小箭头")
- pyautogui.moveTo(*refresh_icon_button, 1)
- pyautogui.click()
- time.sleep(1)
- print("结束点击刷新小箭头")
- # 点击刷新所有页
- print("开始点击刷新所有页")
- pyautogui.moveTo(*refresh_all_button, 1)
- pyautogui.click()
- time.sleep(1)
- print("结束点击刷新所有页")
- # 随机点击
- random_click(
- CONFIG["random_click_range"]["min_x"],
- CONFIG["random_click_range"]["max_x"],
- CONFIG["random_click_range"]["min_y"],
- CONFIG["random_click_range"]["max_y"]
- )
- # 等待数据更新
- time.sleep(CONFIG["get_data_timeout_time"])
- # 保存并关闭文件
- pyautogui.hotkey('ctrl', 's')
- time.sleep(1)
- pyautogui.hotkey('ctrl', 'w')
- return True
- except Exception as e:
- print(f"发生异常:{e}")
- return False
- if __name__ == "__main__":
- app = __hug__.http.server()
- run(app=app, reloader=True, port=7007)
|