-
Notifications
You must be signed in to change notification settings - Fork 6
/
Bit2cClient.py
238 lines (192 loc) · 8.06 KB
/
Bit2cClient.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
import time
import json
import hmac
import hashlib
import pycurl
from datetime import datetime
from Balance import *
from Enums.PairType import PairType
from ExchangesTrade import *
from Ticker import *
from OrderBook import *
from AccountRaw import *
from AddOrderResponse import *
from Orders import *
from od import *
from OrderData import *
from CheckoutLinkModel import *
from CheckoutResponse import *
class Bit2cClient:
def __init__(self,Url, Key, Secret):
self.Key = Key
self.Secret = Secret
self.nonce = int(time.time())
self.Url = Url
def ComputeHash(self, message):
sign = hashlib.sha512(message.encode("utf-8"))
import base64
return base64.b64encode(hmac.new(bytearray(self.Secret.upper(), "ASCII") , bytearray(message,"ASCII") , hashlib.sha512).digest()).decode("ASCII").replace("\n", "")
def Query(self, qString, url, sign):
try:
from io import BytesIO
except ImportError:
from StringIO import StringIO as BytesIO
buf = BytesIO()
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.SSL_VERIFYPEER, 0)
curl.setopt(pycurl.SSL_VERIFYHOST, 0)
curl.setopt(pycurl.WRITEFUNCTION, buf.write)
curl.setopt(pycurl.POSTFIELDS, qString)
curl.setopt(pycurl.HTTPHEADER, ['Key:'+self.Key,
'Sign:'+(sign), 'Content-Type:application/x-www-form-urlencoded'])
curl.perform()
res = buf.getvalue()
buf.close()
return res
def Balance(self):
qString = "nonce=" + str(self.nonce)
sign = self.ComputeHash(qString)
url = self.Url + "Account/Balance"
response = self.Query(qString, url, sign)
_json = json.loads(response.decode("utf-8"))
return Balance(_json['BalanceNIS'], _json['BalanceLTC'], _json['BalanceBTC'])
def DownloadString(self, url):
try:
from io import BytesIO
except ImportError:
from StringIO import StringIO as BytesIO
buf = BytesIO()
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.SSL_VERIFYPEER, 0)
curl.setopt(pycurl.SSL_VERIFYHOST, 0)
curl.setopt(pycurl.WRITEFUNCTION, buf.write)
curl.perform()
res = buf.getvalue()
buf.close()
return res
def GetTrades(self, Pair, since = 0, date = 0):
url = self.Url+ "Exchanges/" + str(Pair) + "/trades.json"
response = self.DownloadString(url)
_json = json.loads(response.decode("utf-8"))
ExchangesTrades = []
for jsonObj in _json:
exTrade = ExchangesTrade(jsonObj['date'], jsonObj['price'], jsonObj['amount'], jsonObj['tid'])
ExchangesTrades.append(exTrade)
return ExchangesTrades
def GetTicker(self, Pair = PairType.BtcNis):
url = self.Url + "Exchanges/" + str(Pair) + "/Ticker.json"
response = self.DownloadString(url)
_json = json.loads(response.decode("utf-8"))
return Ticker(_json['h'], _json['l'], _json['ll'], _json['a'], _json['av'])
def GetOrderBook(self, Pair = PairType.BtcNis):
url = self.Url + "Exchanges/" + str(Pair) + "/orderbook.json"
response = self.DownloadString(url)
_json = json.loads(response.decode("utf-8"))
orderBook = OrderBook()
jsonAsks = _json['asks']
jsonBids = _json['bids']
for ask in jsonAsks:
orderBook.asks.append(ask)
for bid in jsonBids:
orderBook.bids.append(bid)
return orderBook
def AddOrder(self, data):
qString = "Amount=" + str(data.Amount) + "&Price=" + str(data.Price) + "&Total=" + str(data.Total) + "&IsBid=" + str(data.IsBid) + "&Pair=" + str(data.Pair) + "&nonce=" + (str(int(time.time())))
sign = self.ComputeHash(qString)
url = self.Url + "Order/AddOrder"
response = self.Query(qString, url, sign)
_json = json.loads(response.decode("utf-8"))
orderResponse = OrderResponse()
orderResponse.HasError = bool(_json['OrderResponse']['HasError'])
orderResponse.Error = _json['OrderResponse']['Error']
newOrder = od()
newOrder.a = _json['NewOrder']['a']
newOrder.aa = _json['NewOrder']['aa']
newOrder.d = _json['NewOrder']['d']
newOrder.id = _json['NewOrder']['id']
newOrder.p = _json['NewOrder']['p']
newOrder.p1 = _json['NewOrder']['p1']
newOrder.s = _json['NewOrder']['s']
newOrder.t = _json['NewOrder']['t']
addOrder = AddOrderResponse()
addOrder.NewOrder = newOrder
addOrder.OrderResponse = orderResponse
return addOrder
def MyOrders(self, Pair):
qString = "pair=" + str(Pair) + "&nonce=" + (str(int(time.time())))
sign = self.ComputeHash(qString)
url = self.Url + "Order/MyOrders"
response = self.Query(qString, url, sign)
_json = json.loads(response.decode("utf-8"))
orders = Orders()
if 'bids' in _json:
for bid in _json['bids']:
tOrder = TradeOrder()
tOrder.a = bid['a']
tOrder.d = bid['d']
tOrder.id = bid['id']
tOrder.isBid = bid['isBid']
tOrder.p = bid['p']
tOrder.pair = bid['pair']
tOrder.s = bid['s']
orders.bids.append(tOrder)
for ask in _json['asks']:
tOrder = TradeOrder()
tOrder.a = ask['a']
tOrder.d = ask['d']
tOrder.id = ask['id']
tOrder.isBid = ask['isBid']
tOrder.p = ask['p']
tOrder.pair = ask['pair']
tOrder.s = ask['s']
orders.asks.append(tOrder)
return orders
def CancelOrder(self, id):
qString = "id=" + str(id) + "&nonce=" + (str(int(time.time())))
sign = self.ComputeHash(qString)
url = self.Url + "Order/CancelOrder"
response = self.Query(qString, url, sign)
def ClearMyOrders(self, Pair):
myOrders = self.MyOrders(Pair)
for bid in myOrders.bids:
if Pair is bid.pair:
self.CancelOrder(bid.id)
for ask in myOrders.asks:
if Pair is ask.pair:
self.CancelOrder(ask.id)
def AccountHistory(self, fromTime, toTime):
qString = "fromTime=" + fromTime + "&toTime=" + toTime + "&nonce=" + (str(int(time.time())))
sign = self.ComputeHash(qString)
url = self.Url + "Order/AccountHistory"
response = self.Query(qString, url, sign)
_json = json.loads(response.decode("utf-8"))
accountRaws = []
for raw in _json:
accountRaw = AccountRaw()
accountRaw.BalanceBTC = raw['BalanceBTC']
accountRaw.BalanceLTC = raw['BalanceLTC']
accountRaw.BalanceNIS = raw['BalanceNIS']
accountRaw.Created = raw['Created']
accountRaw.Fee = raw['Fee']
accountRaw.FeeInNIS = raw['FeeInNIS']
accountRaw.id = raw['id']
accountRaw.OrderCreated = raw['OrderCreated']
accountRaw.PricePerCoin = raw['PricePerCoin']
accountRaw.Ref = raw['Ref']
accountRaw.TypeId = raw['TypeId']
accountRaws.append(accountRaw)
return accountRaws
def CreateCheckout(self, data):
qString = "Price=" + str(data.Price) + "&Description=" + str(data.Description) + "&CoinType=" + str(data.CoinType) + "&ReturnURL=" + str(data.ReturnURL) + "&CancelURL=" + str(data.CancelURL) + "&NotifyByEmail=" + str(data.NotifyByEmail) + "&nonce=" + (str(int(time.time())))
sign = self.ComputeHash(qString)
url = self.Url + "Merchant/CreateCheckout"
response = self.Query(qString, url, sign)
checkoutResponse = CheckoutResponse()
_json = json.loads(response)
if 'error' in _json:
checkoutResponse.Error = _json['error']
if 'id' in _json:
checkoutResponse.id = _json['id']
return checkoutResponse