Python使用win32组件实现企业微信抢课插件

利用win32组件实现对程序的监控,实现抢课

# -*- coding: UTF-8 -*-
#
# 目的实现抢课
# 依赖:win32、autopy、PIL、aircv、opencv
# pip install win32 -i https://mirrors.aliyun.com/pypi/simple/
# pip install autopy -i https://mirrors.aliyun.com/pypi/simple/
# pip install Pillow -i https://mirrors.aliyun.com/pypi/simple/
# pip install aircv -i https://mirrors.aliyun.com/pypi/simple/
# pip install opencv-python -i https://mirrors.aliyun.com/pypi/simple/
# v2.0 移除autopy,采用win32底层实现
#
import win32gui
import win32api
import win32con
import os
from PIL import Image
from PIL import ImageGrab
import aircv as ac
import time

# 获取窗口,设置为最前并移到left:0;top:0


def getWindowMovie(pos,windowName='******'):
    hwnd = win32gui.FindWindow(None, windowName)
    if hwnd != 0 and win32gui.IsWindowEnabled(hwnd):
        # 在最前方显示
        win32gui.SetForegroundWindow(hwnd)
        move(pos, hwnd)
        return 1
    return 0
# 监视屏幕是否出现报名


def listenWindows(pos):
    print('监听屏幕....\n')
    while True:
        img = ImageGrab.grab(pos)
        img.save("./config/pic.png")
        match_result = matchImg("./config/pic.png", "./config/obj.png")        
        if match_result is not None:
            print('捕捉报名',match_result)
            # 出现报名点击报名
            if 0==getWindowMovie([0, 0, 576, 576],'填表'):
                click([match_result['result'][0],match_result['result'][1]])
            # 移动报名窗口到0 0 0 0
            if 0!=getWindowMovie([0, 0, 576, 576],'填表'):
                click([145,140])
                # 多次输入,提高输入成功率
                win32api.keybd_event(97,0,0,0)
                win32api.keybd_event(98,0,0,0)
                win32api.keybd_event(99,0,0,0)
                print('填写表格....\n')
                time.sleep(0.5)
                click([485,550])
                #win32api.SetCursorPos([485,550])
                break
            
    pass



# 点击资料
# 点击提交

# 移动窗口方法


def move(pos, hwnd):
    if len(pos) == 4:
        win32gui.MoveWindow(hwnd, pos[0], pos[1], pos[2], pos[3], 1)
    if len(pos) == 2:
        win_size = get_bbox_size(hwnd)
        win32gui.MoveWindow(hwnd, pos[0], pos[1], win_size[0], win_size[1], 1)


def get_bbox_size(hwnd):
    bbox = create_boundingbox(hwnd)
    bbox_size = bbox[2] - bbox[0], bbox[3] - bbox[1]
    return bbox_size


def create_boundingbox(hwnd):
    bbox = win32gui.GetWindowRect(hwnd)
    return bbox


def click(pos):
    win32api.SetCursorPos([int(pos[0]),int(pos[1])])
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    pass


def matchImg(imgsrc, imgobj):
    imsrc = ac.imread(imgsrc)
    imobj = ac.imread(imgobj)
    # {'confidence': 0.5435812473297119, 'rectangle': ((394, 384), (394, 416), (450, 384), (450, 416)), 'result': (422.0, 400.0)}
    match_result = ac.find_template(imsrc, imobj)
    if match_result is not None:
        match_result['shape'] = (imsrc.shape[1], imsrc.shape[0])  # 0为高,1为宽
    return match_result


if __name__ == '__main__':
    windowName = '******'
    if getWindowMovie([0, 0, 665, 1000],windowName):
        listenWindows([0, 0,  665, 1000])
        print("success")
    else:
        print("faild")
    pass

 

你可能感兴趣的:(【常用工具】)