1 Star 18 Fork 12

老张/thsauto

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.py 4.41 KB
一键复制 编辑 原始数据 按行查看 历史
match5 提交于 2020-11-03 00:06 . 科创板
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)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/better319/thsauto.git
git@gitee.com:better319/thsauto.git
better319
thsauto
thsauto
master

搜索帮助