-
Notifications
You must be signed in to change notification settings - Fork 353
/
commonFunction.py
512 lines (441 loc) · 18.4 KB
/
commonFunction.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
import mysql.connector
import socket
import json
import requests
import time
import oss2
from websocket import create_connection
from config import *
from mysql.connector.pooling import MySQLConnectionPool
from mysql.connector import connect
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
class FunctionClient(object):
def __init__(self, **params):
"""
Create the request client instance.
:param kwargs: The option of request connection.
api_key: The public key applied from Binance.
secret_key: The private key applied from Binance.
server_url: The URL name like "https://api.binance.com".
"""
self.larkMsgSymbol = ""
if "larkMsgSymbol" in params:
self.larkMsgSymbol = params["larkMsgSymbol"]
self.larkAppID = FEISHU_APP_ID
self.larkAppSecret = FEISHU_APP_SECRET
self.mysqlConnect = {}
if "connectMysql" in params and params["connectMysql"]:
self.mysqlConnect = mysql.connector.connect(**MYSQL_CONFIG)
self.mysqlPoolConnect = {}
if "connectMysqlPool" in params and params["connectMysqlPool"]:
self.mysqlPoolConnect = MySQLConnectionPool(pool_name = "mypool",pool_size = 30,**MYSQL_CONFIG)
self.wsConnectionA = {}
if "connectWsA" in params and params["connectWsA"]:
self.wsConnectionA = create_connection(WS_ADDRESS_A)
self.wsConnectionB = {}
if "connectWsB" in params and params["connectWsB"]:
self.wsConnectionB = create_connection(WS_ADDRESS_B)
self.lastSendLarkTs = 0
self.privateIP = self.get_private_ip()
self.updateMachineStatusTs = 0
oss_auth = oss2.Auth(ALIYUN_API_KEY, ALIYUN_API_SECRET)
self.oss_bucket = oss2.Bucket(oss_auth, 'http://oss-cn-hongkong.aliyuncs.com', 'zuibite-api')
self.serverName = self.getServerName()
def send_lark_msg(self,content):
global FEISHU_APP_ID,FEISHU_APP_SECRET
try:
header = {"Content-Type": "application/json"}
body = {
"app_id":self.larkAppID,
"app_secret":self.larkAppSecret
}
url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
response = requests.request("POST", url, timeout=3, headers=header, data=json.dumps(body)).json()
TEAM_ID = 'oc_d2fc6f3c0ff4d45811dfc774daec528c'
url = "https://open.feishu.cn/open-apis/message/v4/send/"
Authorization = "Bearer "+response['tenant_access_token']
header = {"Authorization": Authorization,"Content-Type":"application/json"}
sendText = "【"+self.larkMsgSymbol+"】"+content+"【"+self.privateIP+"】"
body = {
"chat_id":TEAM_ID,
"msg_type":"text",
"content":{
"text":sendText
}
}
data = bytes(json.dumps(body), encoding='utf8')
response = requests.request("POST", url, timeout=3, headers=header, data=data).json()
except Exception as e:
print(e)
print("sendMsg")
def send_lark_msg_limit_one_min(self,content):
now = int(time.time())
if now - self.lastSendLarkTs>60:
self.lastSendLarkTs = now
self.send_lark_msg(content)
def turn_ts_to_time(self,initValue):
if str(type(initValue))=="<class 'str'>":
timeArray = time.strptime(initValue, "%Y-%m-%d %H:%M:%S")
timestamp = time.mktime(timeArray)
return timestamp
else:
if initValue>99999999999:
initValue = int(initValue/1000)
time_local = time.localtime(initValue)
dt = time.strftime("%Y-%m-%d %H:%M:00",time_local)
return dt
def turn_ts_to_day_time(self,initValue):
if str(type(initValue))=="<class 'str'>":
timeArray = time.strptime(initValue, "%Y-%m-%d %H:%M:%S")
timestamp = time.mktime(timeArray)
return timestamp
else:
if initValue>99999999999:
initValue = int(initValue/1000)
time_local = time.localtime(initValue)
dt = time.strftime("%Y-%m-%d 00:00:00",time_local)
return dt
def turn_ts_to_min(self,initValue):
if initValue>99999999999:
initValue = int(initValue/1000)
time_local = time.localtime(initValue)
dt = time.strftime("%M",time_local)
return dt
def generate_ts_with_min(self,min):
now = int(time.time())
time_local = time.localtime(now)
dt = time.strftime("%Y-%m-%d %H:"+str(min)+":00",time_local)
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
timestamp = int(time.mktime(timeArray))
return timestamp
def mysql_select(self,sql,params):
res = ()
normal = False
try:
self.mysqlConnect.ping()
except Exception as e:
self.mysqlConnect=mysql.connector.connect(**MYSQL_CONFIG)
while not normal:
try:
cursor=self.mysqlConnect.cursor()
print("1")
cursor.execute(sql,params)
print("2")
res = cursor.fetchall()
print("3")
normal = True
cursor.close()
except Exception as e:
self.send_lark_msg("mysql ex,"+str(e)+","+sql+","+str(params))
print("mysql error")
print(sql)
print(e)
try:
self.mysqlConnect.ping()
except Exception as e:
self.mysqlConnect=mysql.connector.connect(**MYSQL_CONFIG)
time.sleep(3)
return res
def mysql_commit(self,sql,params):
normal = False
try:
self.mysqlConnect.ping()
except Exception as e:
self.mysqlConnect=mysql.connector.connect(**MYSQL_CONFIG)
while not normal:
try:
cursor=self.mysqlConnect.cursor()
cursor.execute(sql,params)
self.mysqlConnect.commit()
normal = True
cursor.close()
except Exception as e:
self.send_lark_msg("mysql ex,"+str(e)+","+sql+","+str(params))
print("mysql error")
print(sql)
try:
self.mysqlConnect.ping()
except Exception as e:
self.mysqlConnect=mysql.connector.connect(**MYSQL_CONFIG)
time.sleep(3)
def mysql_pool_select(self,q,params):
res = ()
con = self.mysqlPoolConnect.get_connection()
c = con.cursor()
try:
c.execute(q,params)
res = c.fetchall()
normal = True
except Exception as e:
print(e)
print(q)
print("doing error")
normal = False
try:
con.close()
except Exception as e:
print(q)
print(e)
return res
def mysql_pool_commit(self,q,params):
con = self.mysqlPoolConnect.get_connection()
c = con.cursor()
try:
c.execute(q,params)
con.commit()
c.close()
normal = True
except Exception as e:
self.send_lark_msg_limit_one_min(str(e))
print(q)
print(e)
normal = False
try:
con.close()
except Exception as e:
print(q)
print(e)
return normal
def get_private_ip(self):
privateIP = ""
try:
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.connect(('8.8.8.8',80))
privateIP = s.getsockname()[0]
finally:
s.close()
return privateIP
def send_to_ws_a(self,msg):
try:
print(msg)
self.wsConnectionA.send(msg);
except Exception as e:
print(e)
try:
self.wsConnectionA = create_connection(WS_ADDRESS_A)
self.wsConnectionA.send(msg);
except Exception as e:
print(e)
time.sleep(0.1)
def get_from_ws_a(self,msg):
result = {}
try:
self.wsConnectionA.send(msg);
result = self.wsConnectionA.recv()
return result
except Exception as e:
print(e)
try:
self.wsConnectionA = create_connection(WS_ADDRESS_A)
self.wsConnectionA.send(msg)
result = self.wsConnectionA.recv()
return result
except Exception as e:
self.send_lark_msg_limit_one_min(str(e))
return result
def send_to_ws_b(self,msg):
try:
self.wsConnectionB.send(msg);
except Exception as e:
try:
self.wsConnectionB = create_connection(WS_ADDRESS_B)
self.wsConnectionB.send(msg);
except Exception as e:
time.sleep(0.1)
def get_from_ws_b(self,msg):
result = {}
try:
self.wsConnectionB.send(msg);
result = self.wsConnectionB.recv()
return result
except Exception as e:
try:
self.wsConnectionB = create_connection(WS_ADDRESS_B)
self.wsConnectionB.send(msg)
result = self.wsConnectionB.recv()
print(result)
return result
except Exception as e:
print(e)
self.send_lark_msg_limit_one_min(str(e))
return result
def get_aliyun_public_ip_arr_by_name(self,name):
publicIPArr = []
nowPage =1
emptyReq =False
while not emptyReq:
client = AcsClient(ALIYUN_API_KEY, ALIYUN_API_SECRET,ALIYUN_POINT)
client.add_endpoint(ALIYUN_POINT,'Ecs',"ecs."+ALIYUN_POINT+".aliyuncs.com")
request = DescribeInstancesRequest()
request.set_PageNumber(nowPage)
request.set_PageSize(100)
request.set_accept_format('json')
# request.modify_point('cn-hongkong','ecs',"ecs.cn-hongkong.aliyuncs.com")
# request.set_Endpoint("ecs.cn-hongkong.aliyuncs.com")
instanceInfoArr = client.do_action_with_exception(request)
instanceInfoArr=json.loads(str(instanceInfoArr, encoding='utf-8'))
instanceInfoArr=instanceInfoArr["Instances"]["Instance"]
if len(instanceInfoArr)==0:
emptyReq=True
else:
for i in range(len(instanceInfoArr)):
if instanceInfoArr[i]["InstanceName"].find(name)>=0:
publicIPArr.append(instanceInfoArr[i]["PublicIpAddress"]["IpAddress"][0])
nowPage = nowPage+1
return publicIPArr
def get_aliyun_private_ip_arr_by_name(self,name):
privateIPArr = []
nowPage =1
emptyReq =False
while not emptyReq:
client = AcsClient(ALIYUN_API_KEY, ALIYUN_API_SECRET,ALIYUN_POINT)
client.add_endpoint(ALIYUN_POINT,'Ecs',"ecs."+ALIYUN_POINT+".aliyuncs.com")
request = DescribeInstancesRequest()
request.set_PageNumber(nowPage)
request.set_PageSize(100)
request.set_accept_format('json')
# request.modify_point('cn-hongkong','ecs',"ecs.cn-hongkong.aliyuncs.com")
# request.set_Endpoint("ecs.cn-hongkong.aliyuncs.com")
instanceInfoArr = client.do_action_with_exception(request)
instanceInfoArr=json.loads(str(instanceInfoArr, encoding='utf-8'))
instanceInfoArr=instanceInfoArr["Instances"]["Instance"]
if len(instanceInfoArr)==0:
emptyReq=True
else:
for i in range(len(instanceInfoArr)):
if instanceInfoArr[i]["InstanceName"].find(name)>=0:
privateIPArr.append(instanceInfoArr[i]["VpcAttributes"]["PrivateIpAddress"]["IpAddress"][0])
nowPage = nowPage+1
return privateIPArr
def update_machine_status(self):
now = int(time.time())
if now - self.updateMachineStatusTs>60:
self.updateMachineStatusTs = now
try:
url = "http://"+WEB_ADDRESS+":8888/update_machine_status"
print(url)
postDataObj = {'privateIP':self.privateIP,'symbol':self.larkMsgSymbol }
response = requests.request("POST", url,timeout=(0.5,0.5),data=postDataObj)
except Exception as e:
print(e)
def update_trade_status(self,status,runTime):
try:
url = "http://"+WEB_ADDRESS+":8888/update_trade_status"
print(url)
postDataObj = {'privateIP':self.privateIP,'status':status,"runTime":runTime}
response = requests.request("POST", url,timeout=(0.5,0.5),data=postDataObj)
except Exception as e:
print(e)
def cancel_binance_orders_by_web_server(self,symbol,key,secret):
try:
url = "http://"+WEB_ADDRESS+":8888/cancel_binance_orders"
print(url)
postDataObj = {'privateIP':self.privateIP,'symbol':symbol,'key':key,'secret':secret}
response = requests.request("POST", url,timeout=(3,3),data=postDataObj)
print(response)
except Exception as e:
print(e)
def cancel_binance_order_by_web_server(self,symbol,key,secret,clientOrderId):
try:
url = "http://"+CANCEL_WEB_ADDRESS+":8888/cancel_binance_order"
print(url)
postDataObj = {'privateIP':self.privateIP,'symbol':symbol,'key':key,'secret':secret,'clientOrderId':clientOrderId}
response = requests.request("POST", url,timeout=(3,3),data=postDataObj)
print(response)
except Exception as e:
print(e)
def open_take_binance_orders_by_web_server(self,symbol,direction,key,secret,price,openTime,positionValue,volMultiple):
try:
url = "http://"+TRADE_WEB_ADDRESS+":8888/take_open"
postDataObj = {'privateIP':self.privateIP,'volMultiple':volMultiple,'symbol':symbol,'direction':direction,'key':key,'secret':secret,'price':price,'openTime':openTime,'positionValue':positionValue}
response = requests.request("POST", url,timeout=(3,3),data=postDataObj)
except Exception as e:
print(e)
def end_open_by_web_server(self,symbol):
try:
url = "http://"+TRADE_WEB_ADDRESS+":8888/end_open"
postDataObj = {'privateIP':self.privateIP,'symbol':symbol}
response = requests.request("POST", url,timeout=(3,3),data=postDataObj)
except Exception as e:
print(e)
def get_percent_num(self,num, total):
if total ==0:
return 0
else:
return num / total *100;
def oss_put_obj(self,obj,name):
try:
inputData= json.dumps(obj,ensure_ascii=False)
ossResult = self.oss_bucket.put_object(name, inputData)
except Exception as e:
print(e)
def oss_get_obj(self,name):
try:
object_stream = self.oss_bucket.get_object(name)
readObj = object_stream.read()
readObj = json.loads(str(readObj,'utf-8'))
return readObj
except Exception as e:
print(e)
def getServerName(self):
serverName = ""
privateIP = ""
try:
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.connect(('8.8.8.8',80))
privateIP = s.getsockname()[0]
finally:
s.close()
nowPage =1
emptyReq =False
while serverName=="" and not emptyReq:
client = AcsClient(ALIYUN_API_KEY, ALIYUN_API_SECRET,ALIYUN_POINT)
client.add_endpoint(ALIYUN_POINT,'Ecs',"ecs."+ALIYUN_POINT+".aliyuncs.com")
request = DescribeInstancesRequest()
request.set_PageNumber(nowPage)
request.set_PageSize(100)
request.set_accept_format('json')
# request.modify_point('cn-hongkong','ecs',"ecs.cn-hongkong.aliyuncs.com")
# request.set_Endpoint("ecs.cn-hongkong.aliyuncs.com")
instanceInfoArr = client.do_action_with_exception(request)
instanceInfoArr=json.loads(str(instanceInfoArr, encoding='utf-8'))
instanceInfoArr=instanceInfoArr["Instances"]["Instance"]
if len(instanceInfoArr)==0:
emptyReq=True
for i in range(len(instanceInfoArr)):
if instanceInfoArr[i]["VpcAttributes"]["PrivateIpAddress"]["IpAddress"][0]==privateIP:
serverName = instanceInfoArr[i]["InstanceName"]
nowPage = nowPage+1
return serverName
def begin_trade_record(self,volMultiple,standardRate,symbol,klineArr,nowOpenRate,machineNumber,direction,myTradeType,longsConditionA,shortsConditionA,shortsConditionB,btcNowOpenRate,ethNowOpenRate,clientBeginPrice,clientEndPrice):
try:
url = "http://"+WEB_ADDRESS+":8888/begin_trade_record"
postDataObj = {
'volMultiple':volMultiple,
'standardRate':standardRate,
'symbol':symbol,
'klineArr':json.dumps(klineArr),
'nowOpenRate':nowOpenRate,
'machineNumber':machineNumber,
'direction':direction,
'myTradeType':myTradeType,
'longsConditionA':longsConditionA,
'shortsConditionA':shortsConditionA,
'shortsConditionB':shortsConditionB,
'btcNowOpenRate':btcNowOpenRate,
'ethNowOpenRate':ethNowOpenRate,
'clientBeginPrice':clientBeginPrice,
'clientEndPrice':clientEndPrice,
'privateIP':self.privateIP,
}
print(postDataObj)
postDataObj = postDataObj
print(postDataObj)
response = requests.request("POST", url,timeout=(3,3),data=postDataObj)
except Exception as e:
self.send_lark_msg_limit_one_min(str(e))
print(e)