-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsend.py
193 lines (161 loc) · 4.74 KB
/
send.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import threading
from io import BytesIO, StringIO
import win32con as wcon
import win32api as wapi
import win32gui as wgui
import win32clipboard as wclip
from PIL import Image
import pyautogui as pg
import pywinauto
import pygetwindow as gw
import pyperclip
import time
import queue
message_que = queue.Queue()
kakao_window = 0
class TextMessage():
data = []
def __init__(self):
self.data = []
def text(self, text: str):
self.data.append({
'type': 'text',
'data': text
})
return self
def mention(self, target: str):
self.data.append({
'type': 'mention',
'data': target
})
return self
def lw(self):
self.data.append({
'type': 'lw',
'data': ''
})
return self
def init():
global kakao_window
kakao_window = wgui.FindWindow(None, "카카오톡")
if kakao_window == 0:
print("카카오톡이 실행되지 않았습니다.")
exit(0)
sender_thread = threading.Thread(target=sender, args=(message_que,))
sender_thread.start()
# 엔터 입력
def enter(win):
wapi.PostMessage(win, wcon.WM_KEYDOWN, wcon.VK_RETURN, 0)
time.sleep(0.01)
wapi.PostMessage(win, wcon.WM_KEYUP, wcon.VK_RETURN, 0)
# 채팅창 선택
def set_room(room):
chat_window = wgui.FindWindow(None, room)
if chat_window == 0:
chat_window = open_room(room)
if chat_window == 0:
return -1
for i in range(10):
try:
chat_box = wgui.FindWindowEx(chat_window, None, "RICHEDIT50W", None)
if chat_box == -1:
raise ()
return chat_box
except Exception as e:
time.sleep(0.1)
return -1
# 채팅창 열기
def open_room(room):
global kakao_window
win1 = wgui.FindWindowEx(kakao_window, None, "EVA_ChildWindow", None)
win2_friends = wgui.FindWindowEx(win1, None, "EVA_Window", None)
win2_rooms = wgui.FindWindowEx(win1, win2_friends, "EVA_Window", None)
friends_search = wgui.FindWindowEx(win2_friends, None, "Edit", None)
rooms_search = wgui.FindWindowEx(win2_rooms, None, "Edit", None)
wapi.SendMessage(rooms_search, wcon.WM_SETTEXT, 0, room)
time.sleep(0.15)
for i in range(3):
enter(rooms_search)
time.sleep(0.1)
chat_window = wgui.FindWindow(None, room)
if chat_window != 0:
return chat_window
wapi.SendMessage(friends_search, wcon.WM_SETTEXT, 0, room)
time.sleep(0.15)
for i in range(3):
enter(friends_search)
time.sleep(0.1)
chat_window = wgui.FindWindow(None, room)
if chat_window != 0:
return chat_window
return -1
def send_message(room, message: TextMessage):
message_que.put({
"type": 'text',
"room": room,
"data": message
})
def send_file(room, filepath):
message_que.put({
"type": "file",
"room": room,
"data": filepath
})
def sender(q):
while True:
message = q.get()
start = time.perf_counter()
room = message["room"]
message_type = message["type"]
if message_type == 'text':
sender_text(room, message["data"])
elif message_type == 'file':
sender_file(room, message["data"])
end = time.perf_counter()
print(f'{(end - start) * 1000}ms')
def sender_text(room, message: TextMessage):
win = gw.getWindowsWithTitle(room)
if not win:
set_room(room)
time.sleep(0.3)
win = gw.getWindowsWithTitle(room)
win = win[0]
if win.isActive == False:
pywinauto.application.Application().connect(handle=win._hWnd).top_window().set_focus()
win.activate()
text = ""
for data in message.data:
if data["type"] == "text":
text += data["data"]
elif data["type"] == "lw":
text += "\u200b" * 500
elif data["type"] == "mention":
pyperclip.copy(text + " @" + data['data'])
pg.hotkey('ctrl', 'v')
time.sleep(0.4)
text = ""
pg.press('tab')
if not text == "":
pyperclip.copy(text)
pg.hotkey('ctrl', 'v')
pg.press('enter')
return
def sender_file(room, filepath):
win = gw.getWindowsWithTitle(room)
if not win:
set_room(room)
time.sleep(0.3)
win = gw.getWindowsWithTitle(room)
win = win[0]
if win.isActive == False:
pywinauto.application.Application().connect(handle=win._hWnd).top_window().set_focus()
win.activate()
time.sleep(0.2)
pg.hotkey('ctrl', 't')
time.sleep(0.5)
pyperclip.copy(filepath)
pg.hotkey('ctrl', 'v')
time.sleep(0.05)
pg.press('enter')
time.sleep(0.2)
return