-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
104 lines (90 loc) · 3.35 KB
/
main.py
File metadata and controls
104 lines (90 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# coding='utf-8'
from time import sleep
from pywinauto.application import Application
from pywinauto import mouse
from psutil import process_iter
from pyautogui import hotkey
from pyperclip import copy
class WeiXin(object):
def __init__(self):
self.app = Application(backend='uia')
self.pid = self.__get_pid()
self.app.connect(process=self.pid)
self.weixin_pc_window = self.app.window(
class_name="WeChatMainWndForPC")
self.weixin_pc_window.set_focus()
self.weixin_pc_window.draw_outline(colour='green')
# print(self.weixin_pc_window.dump_tree())
def __get_pid(self):
PID = process_iter()
name = ''
pid_num = 0
for pid_temp in PID:
pid_dic = pid_temp.as_dict(attrs=['pid', 'name'])
if pid_dic['name'] == 'WeChat.exe':
name = pid_dic['name']
pid_num = pid_dic['pid']
break
if name == 'WeChat.exe':
return pid_num
else:
return False
def __get_element_postion(self, element):
"""获取元素的中心点位置"""
# 元素坐标
element_position = element.rectangle()
# 计算中心点位置
center_position = (int((element_position.left + element_position.right) / 2),
int((element_position.top + element_position.bottom) / 2))
return center_position
def start_chat(self):
chat_list_element = self.weixin_pc_window.child_window(
title="聊天", control_type="Button")
mouse.click(button='left', coords=self.__get_element_postion(
chat_list_element))
def get_users(self):
user_list = []
try:
chat_list = self.weixin_pc_window.child_window(
control_type='List', title='会话')
mouse.click(button='left',
coords=self.__get_element_postion(chat_list))
users = chat_list.children()
for user in users:
user_list.append(user.window_text())
except:
pass
return user_list
def find_user(self, user: str):
user_element = self.weixin_pc_window.child_window(
title=user, control_type='Text')
mouse.click(button='left',
coords=self.__get_element_postion(user_element))
sleep(0.3)
def search_user(self, user_name: str):
search = self.weixin_pc_window.child_window(
title="搜索", control_type='Edit')
mouse.click(button='left', coords=self.__get_element_postion(search))
sleep(0.1)
self.weixin_pc_window.type_keys(user_name)
sleep(0.6)
self.weixin_pc_window.type_keys('{ENTER}')
def send_msg(self, texts: str = ""):
edit_element = self.weixin_pc_window.child_window(
title=r"输入", control_type="Edit")
for text in texts.split('\n'):
if text.isalnum():
copy(text.strip())
hotkey('ctrl', 'v')
else:
edit_element.type_keys(text)
sleep(1)
hotkey('ctrl', 'enter')
hotkey('enter')
def teardown(self):
self.app.kill()
if __name__ == '__main__':
weixin = WeiXin()
weixin.start_chat()
weixin.search_user('文件传输助手')
weixin.send_msg('测试消息')