forked from match5/thsauto
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.py
168 lines (146 loc) · 4.41 KB
/
server.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
import functools
from flask import Flask, request, jsonify
from thsauto import ThsAuto
import time
import sys
import threading
import os
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
auto = ThsAuto()
client_path = None
def run_client():
os.system('start ' + client_path)
lock = threading.Lock()
next_time = 0
interval = 0.5
def interval_call(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
global interval
global lock
global next_time
lock.acquire()
now = time.time()
if now < next_time:
time.sleep(next_time - now)
try:
rt = func(*args, **kwargs)
except Exception as e:
rt = ({'code': 1, 'status': 'failed', 'msg': '{}'.format(e)}, 400)
next_time = time.time() + interval
lock.release()
return rt
return wrapper
@app.route('/thsauto/balance', methods = ['GET'])
@interval_call
def get_balance():
auto.active_mian_window()
result = auto.get_balance()
return jsonify(result), 200
@app.route('/thsauto/position', methods = ['GET'])
@interval_call
def get_position():
auto.active_mian_window()
result = auto.get_position()
return jsonify(result), 200
@app.route('/thsauto/orders/active', methods = ['GET'])
@interval_call
def get_active_orders():
auto.active_mian_window()
result = auto.get_active_orders()
return jsonify(result), 200
@app.route('/thsauto/orders/filled', methods = ['GET'])
@interval_call
def get_filled_orders():
auto.active_mian_window()
result = auto.get_filled_orders()
return jsonify(result), 200
@app.route('/thsauto/sell', methods = ['GET'])
@interval_call
def sell():
auto.active_mian_window()
stock = request.args['stock_no']
amount = request.args['amount']
price = request.args.get('price', None)
if price is not None:
price = float(price)
result = auto.sell(stock_no=stock, amount=int(amount), price=price)
return jsonify(result), 200
@app.route('/thsauto/buy', methods = ['GET'])
@interval_call
def buy():
auto.active_mian_window()
stock = request.args['stock_no']
amount = request.args['amount']
price = request.args.get('price', None)
if price is not None:
price = float(price)
result = auto.buy(stock_no=stock, amount=int(amount), price=price)
return jsonify(result), 200
@app.route('/thsauto/buy/kc', methods = ['GET'])
@interval_call
def buy_kc():
auto.active_mian_window()
stock = request.args['stock_no']
amount = request.args['amount']
price = request.args.get('price', None)
if price is not None:
price = float(price)
result = auto.buy_kc(stock_no=stock, amount=int(amount), price=price)
return jsonify(result), 200
@app.route('/thsauto/sell/kc', methods = ['GET'])
@interval_call
def sell_kc():
auto.active_mian_window()
stock = request.args['stock_no']
amount = request.args['amount']
price = request.args.get('price', None)
if price is not None:
price = float(price)
result = auto.sell_kc(stock_no=stock, amount=int(amount), price=price)
return jsonify(result), 200
@app.route('/thsauto/cancel', methods = ['GET'])
@interval_call
def cancel():
auto.active_mian_window()
entrust_no = request.args['entrust_no']
result = auto.cancel(entrust_no=entrust_no)
return jsonify(result), 200
@app.route('/thsauto/client/kill', methods = ['GET'])
@interval_call
def kill_client():
auto.active_mian_window()
auto.kill_client()
return jsonify({'code': 0, 'status': 'succeed'}), 200
@app.route('/thsauto/client/restart', methods = ['GET'])
@interval_call
def restart_client():
auto.active_mian_window()
auto.kill_client()
run_client()
time.sleep(5)
auto.bind_client()
if auto.hwnd_main is None:
return jsonify({'code': 1, 'status': 'failed'}), 200
else:
return jsonify({'code': 0, 'status': 'succeed'}), 200
@app.route('/thsauto/test', methods = ['GET'])
@interval_call
def test():
auto.active_mian_window()
auto.test()
return jsonify({}), 200
if __name__ == '__main__':
host = '127.0.0.1'
port = 5000
if len(sys.argv) > 1:
host = sys.argv[1]
if len(sys.argv) > 2:
port = int(sys.argv[2])
if len(sys.argv) > 3:
client_path = sys.argv[3]
auto.bind_client()
if auto.hwnd_main is None and client_path is not None:
restart_client()
app.run(host=host, port=port)