-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
58 lines (39 loc) · 1.7 KB
/
main.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
import base64, hashlib, hmac, time, json
from urllib.request import urlopen, Request
base_url = 'https://api.btcmarkets.net'
def request(action, key, signature, timestamp, path, data):
header = {
'Accept': 'application/json',
'Accept-Charset': 'UTF-8',
'Content-Type': 'application/json',
'apikey': key,
'timestamp': timestamp,
'signature': signature,
}
http_request = Request(base_url + path, data, header)
if action == 'post':
response = urlopen(http_request, data)
else:
response = urlopen(http_request)
return json.loads(response.read())
def get_request(key, secret, path):
nowInMilisecond = str(int(time.time() * 1000))
stringToSign = path + "\n" + nowInMilisecond + "\n"
presignature = base64.b64encode(hmac.new(secret, stringToSign.encode('utf-8'), digestmod=hashlib.sha512).digest())
signature = presignature.decode('utf8')
return request('get', key, signature, nowInMilisecond, path, None)
def post_request(key, secret, path, postData):
nowInMilisecond = str(int(time.time() * 1000))
stringToSign = path + "\n" + nowInMilisecond + "\n" + postData
signature = base64.b64encode(hmac.new(secret, stringToSign.encode('utf-8'), digestmod=hashlib.sha512).digest())
return request('post', key, signature, nowInMilisecond, path, postData)
class BTCMarkets:
def __init__(self, key, secret):
self.key = key
self.secret = base64.b64decode(secret)
def account_balance(self):
return get_request(self.key, self.secret, '/account/balance')
api_key = 'your key'
private_key = 'your private key'
client = BTCMarkets (api_key, private_key)
print (client.account_balance())