-
Notifications
You must be signed in to change notification settings - Fork 3
/
qq.py
273 lines (229 loc) · 8.5 KB
/
qq.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# coding=gbk
import win32con
import win32gui
import win32api
import win32clipboard
import os
import time
import re
try:
import user_data
user_list = user_data.user_list
qq_shortcut = user_data.qq_shortcut
except:
print 'No user data, use default value'
user_list = {}
user_list['ly'] = {'name': 'ly', 'email': 'xxx@something.com', 'spell': 'osvtzhuli'}
qq_shortcut = '"C:\Program Files (x86)\Tencent\QQ\Bin\QQScLauncher.exe" /uin:XXXXXXXXX /quicklunch:449BDE2DE4BA357E6E9168FD55AB24059BB3CABB1EEF93699642E5891DC173715F5B4E3EDF68B41F'
# Set the clipboard with a str.
def QQ_setClipboardText(str):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_TEXT, str)
win32clipboard.CloseClipboard()
# Print plain text.
def QQ_PrintText(str):
print "Send text = " + str.decode('gbk')
QQ_setClipboardText(str)
win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0);
win32api.keybd_event(ord('V'), 0, 0, 0);
win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_KEYUP, 0);
win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0);
# AT a name.
def QQ_AtPerson(name):
win32api.keybd_event(win32con.VK_SHIFT, 0, 0, 0);
win32api.keybd_event(ord('2'), 0, 0, 0);
win32api.keybd_event(ord('2'), 0, win32con.KEYEVENTF_KEYUP, 0);
win32api.keybd_event(win32con.VK_SHIFT, 0, win32con.KEYEVENTF_KEYUP, 0);
time.sleep(0.5)
QQ_PrintText(user_list[name]['spell'])
time.sleep(0.8)
win32api.keybd_event(win32con.VK_RETURN, 0, 0, 0);
win32api.keybd_event(win32con.VK_RETURN, 0, win32con.KEYEVENTF_KEYUP, 0);
# Parse out the AT directives.
def QQ_parseText(str):
pos = str.find('@')
if pos == -1:
return [str]
for name in user_list.keys():
if str.startswith('@' + user_list[name]['name'], pos):
return [str[:pos]] + [str[pos:pos + 1 + len(user_list[name]['name'])]] + QQ_parseText(str[pos + 1 + len(user_list[name]['name']):])
def QQ_PrintTextWithAt(str):
phrase_list = QQ_parseText(str)
temp_list = []
for phrase in phrase_list:
if phrase != '':
temp_list.append(phrase)
phrase_list = temp_list
print 'Sending: ',
for phrase in phrase_list:
print '"' + phrase + '",',
# Populate the textbox of QQ.
for phrase in phrase_list:
if phrase.startswith('@'):
QQ_AtPerson(phrase[1:])
else:
QQ_PrintText(phrase)
# Press the "Enter' key.
def QQ_Enter():
win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0);
win32api.keybd_event(win32con.VK_RETURN, 0, 0, 0);
win32api.keybd_event(win32con.VK_RETURN, 0, win32con.KEYEVENTF_KEYUP, 0);
win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0);
# The interface function to send QQ a message.
def QQ_SendTextWithAt(str):
os.system(qq_shortcut)
try_time = 0
while True:
time.sleep(0.5)
hwnd = win32gui.FindWindow(None, '操作系统&虚拟化小组')
# hwnd = win32gui.FindWindow(None, 'OSVT小O测试群')
print('try_time = %d, hwnd = %d' % (try_time, hwnd))
if hwnd != 0:
break
elif try_time >= 60:
print ('SendTextToQQ Error.')
return
else:
try_time = try_time + 1
win32gui.SetForegroundWindow(hwnd)
QQ_PrintTextWithAt(str)
QQ_Enter()
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, ord('v'), 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, ord('v'), 0)
#win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, None, 'aaa')
#win32gui.SetWindowText(hwnd, 'aaa')
#win32gui.ReplaceSel()
#win32gui.PostMessage(hwnd, win32con.WM_CHAR, '他', 3)
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_CONTROL, 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, ord('V'), 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, ord('V'), 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_CONTROL, 0)
def QQ_checkTargetWindow():
window_name = '客户0'
hwnd = win32gui.FindWindow(None, window_name)
if hwnd == 0:
return 0
else:
print "find window = " + window_name.decode('gbk')
return hwnd
def QQ_OpenActiveWindow():
hwnd = QQ_checkTargetWindow()
if hwnd == 0:
return False
win32gui.SetForegroundWindow(hwnd)
return True
# Press the "Alt + H' key.
def QQ_ToggleMessageRecord():
win32api.keybd_event(win32con.VK_MENU, 0, 0, 0);
win32api.keybd_event(ord('H'), 0, 0, 0);
win32api.keybd_event(ord('H'), 0, win32con.KEYEVENTF_KEYUP, 0);
win32api.keybd_event(win32con.VK_MENU, 0, win32con.KEYEVENTF_KEYUP, 0);
# Copy the last message.
def QQ_CopyText():
win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0);
win32api.keybd_event(ord('C'), 0, 0, 0);
win32api.keybd_event(ord('C'), 0, win32con.KEYEVENTF_KEYUP, 0);
win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0);
# Close the window.
def QQ_CloseWindow():
win32api.keybd_event(win32con.VK_MENU, 0, 0, 0);
win32api.keybd_event(win32con.VK_F4, 0, 0, 0);
win32api.keybd_event(win32con.VK_F4, 0, win32con.KEYEVENTF_KEYUP, 0);
win32api.keybd_event(win32con.VK_MENU, 0, win32con.KEYEVENTF_KEYUP, 0);
# Get the clipboard into a str.
def QQ_GetClipboardText():
win32clipboard.OpenClipboard()
try:
message_text = win32clipboard.GetClipboardData(win32con.CF_TEXT)
except Exception, e:
print "win32clipboard.GetClipboardData() failed"
print Exception, ": ", e
win32clipboard.CloseClipboard()
return ""
win32clipboard.EmptyClipboard()
win32clipboard.CloseClipboard()
return message_text
def QQ_GetMessageRecordText():
try_time = 0
while True:
QQ_CopyText()
time.sleep(0.5)
message_text = QQ_GetClipboardText()
print('try_time = %d, message_text = %s' % (try_time, message_text.decode('gbk')))
if message_text != "":
break
elif try_time >= 10:
print ('QQ_GetClipboardText Error.')
return ""
else:
try_time += 1
return message_text
def getDocumentName(message_text):
print "received message = \n" + message_text.decode('gbk')
tmp_list = message_text.split('\r\n')
title = tmp_list[1]
print "document title = " + title.decode('gbk')
return title
# Paste the file.
def QQ_PasteFile():
win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0);
win32api.keybd_event(ord('V'), 0, 0, 0);
win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_KEYUP, 0);
win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0);
def QQ_SendOffline():
QQ_Enter()
time.sleep(0.2)
for i in range(0, 10):
print "tab = " + str(i)
win32api.keybd_event(win32con.VK_TAB, 0, 0, 0);
win32api.keybd_event(win32con.VK_TAB, 0, win32con.KEYEVENTF_KEYUP, 0);
time.sleep(0.2)
QQ_Enter()
def do_get_document_name():
if QQ_OpenActiveWindow():
time.sleep(1.0)
QQ_ToggleMessageRecord()
time.sleep(1.0)
message_text = QQ_GetMessageRecordText()
QQ_ToggleMessageRecord()
return getDocumentName(message_text)
else:
return ""
def do_send_document():
if QQ_OpenActiveWindow():
time.sleep(1.0)
QQ_PasteFile()
QQ_SendOffline()
else:
print "do_send_document() error, not sending document!"
def do_close_session():
hwnd = QQ_checkTargetWindow()
if hwnd == 0:
print "Window doesn't exist, no need to close session."
return
else:
print "move the window to front."
win32gui.SetForegroundWindow(hwnd)
time.sleep(1.0)
for try_time in range(0, 10, 1):
print "try to close the current session, try_time = " + str(try_time)
QQ_CloseWindow()
time.sleep(1.0)
if QQ_checkTargetWindow() != 0:
QQ_CloseWindow()
print "close fails, probably the file is still in transmission."
time.sleep(5.0)
else:
print "session closed."
return
print "can't close the current session, abort."
if __name__ == '__main__':
# do_get_document_name()
# message_text = u"OSVT助理小O 12:26:54 AM\n中德两国高中生数学能力的分析及比较\n\n"
# getDocumentName(message_text)
# do_send_document()
do_close_session()
## QQ_SendTextWithAt(u'大家好,我是@ly,请多指教!')