-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbcoinex.py
65 lines (54 loc) · 2.41 KB
/
bcoinex.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
import time
import hashlib
import json as complex_json
import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings(InsecureRequestWarning)
http = urllib3.PoolManager(timeout=urllib3.Timeout(connect=1, read=2))
class RequestClient(object):
__headers = {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
}
def __init__(self, headers={}):
self.access_id = '' # replace
self.secret_key = '' # replace
self.url = 'https://api.coinex.com'
self.headers = self.__headers
self.headers.update(headers)
@staticmethod
def get_sign(params, secret_key):
sort_params = sorted(params)
data = []
for item in sort_params:
data.append(item + '=' + str(params[item]))
str_params = "{0}&secret_key={1}".format('&'.join(data), secret_key)
token = hashlib.md5(str_params.encode('utf-8')).hexdigest().upper()
return token
def set_authorization(self, params):
params['access_id'] = self.access_id
params['tonce'] = int(time.time()*1000)
self.headers['AUTHORIZATION'] = self.get_sign(params, self.secret_key)
def request(self, method, url, params={}, data='', json={}):
method = method.upper()
if method in ['GET', 'DELETE']:
self.set_authorization(params)
result = http.request(method, url, fields=params, headers=self.headers)
else:
if data:
json.update(complex_json.loads(data))
self.set_authorization(json)
encoded_data = complex_json.dumps(json).encode('utf-8')
result = http.request(method, url, body=encoded_data, headers=self.headers)
return result
def get_account():
request_client = RequestClient()
response = request_client.request('GET', '{url}/v1/balance/'.format(url=request_client.url))
response_dict = complex_json.loads(response.data.decode("utf-8"))
balance_dict = response_dict["data"]
for coin in balance_dict:
balance = float(balance_dict[coin]["available"]) + float(balance_dict[coin]["frozen"])
print(coin.ljust(16), '{0:.18f}'.format(balance).rjust(37).rstrip('0').rstrip('.'))
if __name__ == '__main__':
get_account()