forked from match5/thsauto
-
Notifications
You must be signed in to change notification settings - Fork 9
/
thsauto.py
581 lines (518 loc) · 18.8 KB
/
thsauto.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import win32api
import win32gui
import win32ui
import win32con
import win32clipboard
import win32process
import ctypes
import time
import math
import os
from PIL import Image
import ddddocr
DdddOcr = ddddocr.DdddOcr()
from const import VK_CODE, BALANCE_CONTROL_ID_GROUP
sleep_time = 0.2
refresh_sleep_time = 0.5
retry_time = 10
window_title = u'国金全能行远航版'
def get_clipboard_data():
win32clipboard.OpenClipboard()
try:
data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
finally:
win32clipboard.CloseClipboard()
return data
def hot_key(keys):
time.sleep(sleep_time)
for key in keys:
win32api.keybd_event(VK_CODE[key], 0, 0, 0)
for key in reversed(keys):
win32api.keybd_event(VK_CODE[key], 0, win32con.KEYEVENTF_KEYUP, 0)
def set_text(hwnd, string):
win32gui.SetForegroundWindow(hwnd)
win32api.SendMessage(hwnd, win32con.EM_SETSEL, 0, -1)
win32api.keybd_event(VK_CODE['backspace'], 0, 0, 0)
win32api.keybd_event(VK_CODE['backspace'], 0, win32con.KEYEVENTF_KEYUP, 0)
for char in string:
if char.isupper():
win32api.keybd_event(0xA0, 0, 0, 0)
win32api.keybd_event(VK_CODE[char.lower()], 0, 0, 0)
win32api.keybd_event(VK_CODE[char.lower()], 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0xA0, 0, win32con.KEYEVENTF_KEYUP, 0)
else:
win32api.keybd_event(VK_CODE[char], 0, 0, 0)
win32api.keybd_event(VK_CODE[char], 0, win32con.KEYEVENTF_KEYUP, 0)
def get_text(hwnd):
length = ctypes.windll.user32.SendMessageW(hwnd, win32con.WM_GETTEXTLENGTH)
buf = ctypes.create_unicode_buffer(length + 1)
ctypes.windll.user32.SendMessageW(hwnd, win32con.WM_GETTEXT, length, ctypes.byref(buf))
return buf.value
def parse_table(text):
lines = text.split('\t\r\n')
keys = lines[0].split('\t')
result = []
for i in range(1, len(lines)):
info = {}
items = lines[i].split('\t')
for j in range(len(keys)):
info[keys[j]] = items[j]
result.append(info)
return result
class ThsAuto:
def __init__(self):
self.hwnd_main = None
def bind_client(self):
hwnd = win32gui.FindWindow(None, window_title)
if hwnd > 0:
win32gui.SetForegroundWindow(hwnd)
self.hwnd_main = hwnd
def kill_client(self):
self.hwnd_main = None
retry = 5
while(retry > 0):
hwnd = win32gui.FindWindow(None, window_title)
if hwnd == 0:
time.sleep(1)
break
else:
win32gui.SetForegroundWindow(hwnd)
time.sleep(sleep_time)
hot_key(['alt', 'F4'])
time.sleep(1)
retry -= 1
def get_tree_hwnd(self):
hwnd = self.hwnd_main
hwnd = win32gui.FindWindowEx(hwnd, None, 'AfxMDIFrame140s', None)
hwnd = win32gui.FindWindowEx(hwnd, None, 'AfxWnd140s', None)
hwnd = win32gui.FindWindowEx(hwnd, None, None, "HexinScrollWnd")
hwnd = win32gui.FindWindowEx(hwnd, None, 'AfxWnd140s', None)
hwnd = win32gui.FindWindowEx(hwnd, None, 'SysTreeView32', None)
return hwnd
def get_right_hwnd(self):
hwnd = self.hwnd_main
hwnd = win32gui.FindWindowEx(hwnd, None, 'AfxMDIFrame140s', None)
hwnd = win32gui.GetDlgItem(hwnd, 0xE901)
return hwnd
def get_left_bottom_tabs(self):
hwnd = self.hwnd_main
hwnd = win32gui.FindWindowEx(hwnd, None, 'AfxMDIFrame140s', None)
hwnd = win32gui.FindWindowEx(hwnd, None, 'AfxWnd140s', None)
hwnd = win32gui.FindWindowEx(hwnd, None, 'CCustomTabCtrl', None)
return hwnd
def get_ocr_hwnd(self):
tid, pid = win32process.GetWindowThreadProcessId(self.hwnd_main)
def enum_children(hwnd, results):
try:
if (win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd)):
win32gui.EnumChildWindows(hwnd, handler, results)
except Exception:
return
def handler(hwnd, results):
if win32gui.GetClassName(hwnd) == 'Static':
results.append(hwnd)
return False
enum_children(hwnd, results)
return len(results) == 0
def callback1(hwnd, _):
if win32gui.GetClassName(hwnd) == "#32770":
windows.append(hwnd)
return True
def callback2(hwnd, _):
if win32gui.GetClassName(hwnd) == "Static":
windows.append(hwnd)
return True
windows = []
win32gui.EnumWindows(callback1, None)
for window in windows:
win32gui.EnumChildWindows(window, callback2, None)
for ctrl in windows:
text = get_text(ctrl)
if u"检测到您正在拷贝数据" in text:
return ctypes.windll.user32.GetWindow(ctrl, win32con.GW_HWNDNEXT)
return 0
def get_balance(self):
self.switch_to_normal()
hot_key(['F4'])
self.refresh()
hwnd = self.get_right_hwnd()
data = {}
for key, cid in BALANCE_CONTROL_ID_GROUP.items():
ctrl = win32gui.GetDlgItem(hwnd, cid)
if ctrl > 0 and win32gui.IsWindowVisible(ctrl):
data[key] = get_text(ctrl)
return {
'code': 0, 'status': 'succeed',
'data': data,
}
def empty_clipboard(self):
win32clipboard.OpenClipboard()
try:
win32clipboard.EmptyClipboard()
finally:
win32clipboard.CloseClipboard()
def get_position(self):
self.switch_to_normal()
hot_key(['F1'])
hot_key(['F6'])
self.refresh()
hwnd = self.get_right_hwnd()
ctrl = win32gui.GetDlgItem(hwnd, 0x417)
self.copy_table(ctrl)
data = None
retry = 0
#self.empty_clipboard()
while not data and retry < retry_time:
retry += 1
time.sleep(sleep_time)
data = get_clipboard_data()
if data:
return {
'code': 0, 'status': 'succeed',
'data': parse_table(data),
}
return {'code': 1, 'status': 'failed'}
def get_active_orders(self):
self.switch_to_normal()
hot_key(['F1'])
hot_key(['F8'])
self.refresh()
hwnd = self.get_right_hwnd()
ctrl = win32gui.GetDlgItem(hwnd, 0x417)
self.copy_table(ctrl)
data = None
retry = 0
while not data and retry < retry_time:
retry += 1
time.sleep(sleep_time)
data = get_clipboard_data()
if data:
return {
'code': 0, 'status': 'succeed',
'data': parse_table(data),
}
return {'code': 1, 'status': 'failed'}
def get_filled_orders(self):
self.switch_to_normal()
hot_key(['F2'])
hot_key(['F7'])
self.refresh()
hwnd = self.get_right_hwnd()
ctrl = win32gui.GetDlgItem(hwnd, 0x417)
self.copy_table(ctrl)
data = None
retry = 0
while not data and retry < retry_time:
retry += 1
time.sleep(sleep_time)
data = get_clipboard_data()
if data:
return {
'code': 0, 'status': 'succeed',
'data': parse_table(data),
}
return {'code': 1, 'status': 'failed'}
def sell(self, stock_no, amount, price):
self.switch_to_normal()
hot_key(['F2'])
time.sleep(sleep_time)
hwnd = self.get_right_hwnd()
ctrl = win32gui.GetDlgItem(hwnd, 0x408)
set_text(ctrl, stock_no)
time.sleep(sleep_time)
if price is not None:
time.sleep(sleep_time)
price = '%.3f' % price
ctrl = win32gui.GetDlgItem(hwnd, 0x409)
set_text(ctrl, price)
time.sleep(sleep_time)
ctrl = win32gui.GetDlgItem(hwnd, 0x40A)
set_text(ctrl, str(amount))
time.sleep(sleep_time)
hot_key(['enter'])
result = None
retry = 0
while retry < retry_time:
time.sleep(sleep_time)
result = self.get_result()
if result:
hot_key(['enter'])
return result
hot_key(['y'])
retry += 1
return {
'code': 2,
'status': 'unknown',
'msg': '获取结果失败,请自行确认订单状态',
}
def buy(self, stock_no, amount, price):
self.switch_to_normal()
hot_key(['F1'])
time.sleep(sleep_time)
hwnd = self.get_right_hwnd()
ctrl = win32gui.GetDlgItem(hwnd, 0x408)
set_text(ctrl, stock_no)
time.sleep(sleep_time)
if price is not None:
time.sleep(sleep_time)
price = '%.3f' % price
ctrl = win32gui.GetDlgItem(hwnd, 0x409)
set_text(ctrl, price)
time.sleep(sleep_time)
ctrl = win32gui.GetDlgItem(hwnd, 0x40A)
set_text(ctrl, str(amount))
time.sleep(sleep_time)
hot_key(['enter'])
result = None
retry = 0
while retry < retry_time:
time.sleep(sleep_time)
result = self.get_result()
if result:
hot_key(['enter'])
return result
hot_key(['y'])
retry += 1
return {
'code': 2,
'status': 'unknown',
'msg': '获取结果失败,请自行确认订单状态',
}
def sell_kc(self, stock_no, amount, price):
self.switch_to_kechuang()
self.click_kc_sell()
hwnd = self.get_right_hwnd()
ctrl = win32gui.GetDlgItem(hwnd, 0x408)
set_text(ctrl, stock_no)
time.sleep(sleep_time)
if price is not None:
time.sleep(sleep_time)
price = '%.3f' % price
ctrl = win32gui.GetDlgItem(hwnd, 0x409)
set_text(ctrl, price)
time.sleep(sleep_time)
ctrl = win32gui.GetDlgItem(hwnd, 0x40A)
set_text(ctrl, str(amount))
time.sleep(sleep_time)
hot_key(['enter'])
result = None
retry = 0
while retry < retry_time:
time.sleep(sleep_time)
result = self.get_result()
if result:
hot_key(['enter'])
return result
hot_key(['y'])
retry += 1
return {
'code': 2,
'status': 'unknown',
'msg': '获取结果失败,请自行确认订单状态',
}
def buy_kc(self, stock_no, amount, price):
self.switch_to_kechuang()
self.click_kc_buy()
hwnd = self.get_right_hwnd()
ctrl = win32gui.GetDlgItem(hwnd, 0x408)
set_text(ctrl, stock_no)
time.sleep(sleep_time)
if price is not None:
time.sleep(sleep_time)
price = '%.3f' % price
ctrl = win32gui.GetDlgItem(hwnd, 0x409)
set_text(ctrl, price)
time.sleep(sleep_time)
ctrl = win32gui.GetDlgItem(hwnd, 0x40A)
set_text(ctrl, str(amount))
time.sleep(sleep_time)
hot_key(['enter'])
result = None
retry = 0
while retry < retry_time:
time.sleep(sleep_time)
result = self.get_result()
if result:
hot_key(['enter'])
return result
hot_key(['y'])
retry += 1
return {
'code': 2,
'status': 'unknown',
'msg': '获取结果失败,请自行确认订单状态',
}
def cancel(self, entrust_no):
self.switch_to_normal()
hot_key(['F3'])
self.refresh()
hwnd = self.get_right_hwnd()
ctrl = win32gui.GetDlgItem(hwnd, 0x417)
self.copy_table(ctrl)
data = None
retry = 0
while not data and retry < retry_time:
retry += 1
time.sleep(sleep_time)
data = get_clipboard_data()
if data:
entrusts = parse_table(data)
find = None
for i, entrust in enumerate(entrusts):
if str(entrust['合同编号']) == str(entrust_no):
find = i
break
if find is None:
return {'code': 1, 'status': 'failed', 'msg': u'没找到指定订单'}
left, top, right, bottom = win32gui.GetWindowRect(ctrl)
x = 50 + left
y = 30 + 16 * find + top
win32api.SetCursorPos((x, y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
time.sleep(sleep_time)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
time.sleep(sleep_time)
hot_key(['enter'])
return {'code': 0, 'status': 'succeed'}
return {'code': 1, 'status': 'failed'}
def get_result(self, cid=0x3EC):
tid, pid = win32process.GetWindowThreadProcessId(self.hwnd_main)
def enum_children(hwnd, results):
try:
if (win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd)):
win32gui.EnumChildWindows(hwnd, handler, results)
except Exception:
return
def handler(hwnd, results):
if (win32api.GetWindowLong(hwnd, win32con.GWL_ID) == cid and
win32gui.GetClassName(hwnd) == 'Static'):
results.append(hwnd)
return False
enum_children(hwnd, results)
return len(results) == 0
popups = []
windows = []
win32gui.EnumThreadWindows(tid, lambda hwnd, l: l.append(hwnd), windows)
for hwnd in windows:
if not handler(hwnd, popups):
break
if popups:
ctrl = popups[0]
text = get_text(ctrl)
if u'已成功提交' in text:
return {
'code': 0,
'status': 'succeed',
'msg': text,
'entrust_no': text.split(u'合同编号:')[1].split('。')[0],
}
else:
return {
'code': 1,
'status': 'failed',
'msg': text,
}
def refresh(self):
hot_key(['F5'])
time.sleep(refresh_sleep_time)
def active_mian_window(self):
if self.hwnd_main is not None:
ctypes.windll.user32.SwitchToThisWindow(self.hwnd_main, True)
time.sleep(sleep_time)
def right_click_menu(self, hwnd, x, y, idx=None, key=None):
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
x = left + x if x > 0 else right + x
y = top + y if y > 0 else bottom + y
win32api.SetCursorPos((x, y))
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
time.sleep(sleep_time)
if idx is not None:
while (idx >= 0):
hot_key(['down_arrow'])
idx -= 1
hot_key(['enter'])
elif hot_key is not None:
if isinstance(key, list):
hot_key(key)
else:
hot_key([key])
def switch_to_normal(self):
tabs = self.get_left_bottom_tabs()
left, top, right, bottom = win32gui.GetWindowRect(tabs)
x = left + 10
y = top + 5
win32api.SetCursorPos((x, y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
time.sleep(sleep_time)
def switch_to_kechuang(self):
tabs = self.get_left_bottom_tabs()
left, top, right, bottom = win32gui.GetWindowRect(tabs)
x = left + 200
y = top + 5
win32api.SetCursorPos((x, y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
time.sleep(sleep_time)
def click_kc_buy(self):
tree = self.get_tree_hwnd()
left, top, right, bottom = win32gui.GetWindowRect(tree)
x = left + 10
y = top + 10
win32api.SetCursorPos((x, y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
time.sleep(sleep_time)
def click_kc_sell(self):
tree = self.get_tree_hwnd()
left, top, right, bottom = win32gui.GetWindowRect(tree)
x = left + 10
y = top + 30
win32api.SetCursorPos((x, y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
time.sleep(sleep_time)
def copy_table(self, hwnd):
win32gui.SetForegroundWindow(hwnd)
os.system('echo off | clip')
hot_key(['ctrl', 'c'])
time.sleep(0.2)
self.input_ocr()
def input_ocr(self):
ocr = self.get_ocr_hwnd()
if ocr > 0:
self.capture_window(ocr, 'ocr.png')
with open('ocr.png', 'rb') as f:
data = f.read()
code = DdddOcr.classification(data)
ctrl = ctypes.windll.user32.GetWindow(ocr, win32con.GW_HWNDNEXT)
ctrl = ctypes.windll.user32.GetWindow(ctrl, win32con.GW_HWNDNEXT)
ctrl = ctypes.windll.user32.GetWindow(ctrl, win32con.GW_HWNDNEXT)
set_text(ctrl, code)
hot_key(['enter'])
def capture_window(self, hwnd, file_name):
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
width = right - left + 40
height = bottom - top + 10
hdc = win32gui.GetWindowDC(hwnd)
dc = win32ui.CreateDCFromHandle(hdc)
cdc = dc.CreateCompatibleDC()
bmp = win32ui.CreateBitmap()
bmp.CreateCompatibleBitmap(dc, width, height)
cdc.SelectObject(bmp)
cdc.BitBlt((0, 0), (width, height), dc, (0, 0), win32con.SRCCOPY)
info = bmp.GetInfo()
bits = bmp.GetBitmapBits(True)
img = Image.frombuffer("RGB", (info['bmWidth'], info['bmHeight']), bits, 'raw', 'BGRX', 0, 1)
win32gui.DeleteObject(bmp.GetHandle())
dc.DeleteDC()
cdc.DeleteDC()
win32gui.ReleaseDC(hwnd, hdc)
img.save(file_name)
def test(self):
pass