diff --git a/btcmarkets.py b/btcmarkets.py index f2ef506..79c1a7b 100644 --- a/btcmarkets.py +++ b/btcmarkets.py @@ -1,4 +1,4 @@ -import base64, hashlib, hmac, urllib2, time, urllib, json +import base64, hashlib, hmac, time, urllib, urllib.request, json from collections import OrderedDict base_url = 'https://api.btcmarkets.net' @@ -15,20 +15,19 @@ def request(action, key, signature, timestamp, path, data): 'timestamp': timestamp, } - request = urllib2.Request(base_url + path, data, header) + request = urllib.request.Request(base_url + path, data, header) if action == 'post': - response = urllib2.urlopen(request, data) + response = urllib.request.urlopen(request, data) else: - response = urllib2.urlopen(request) - return json.load(response) + response = urllib.request.urlopen(request) + return json.loads(response.read().decode('utf-8')) def get_request(key, secret, path): nowInMilisecond = str(int(time.time() * 1000)) stringToSign = path + "\n" + nowInMilisecond + "\n" - - signature = base64.b64encode(hmac.new(secret, stringToSign, digestmod=hashlib.sha512).digest()) + signature = base64.b64encode(hmac.new(secret, stringToSign.encode('utf-8'), digestmod=hashlib.sha512).digest()) return request('get', key, signature, nowInMilisecond, path, None) @@ -50,13 +49,13 @@ def __init__(self, key, secret): self.secret = base64.b64decode(secret) def trade_history(self, currency, instrument, limit, since): - + data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)]) postData = json.dumps(data, separators=(',', ':')) return post_request(self.key, self.secret, '/order/trade/history', postData) def order_create(self, currency, instrument, price, volume, side, order_type, client_request_id): - + data = OrderedDict([('currency', currency),('instrument', instrument), ('price', price),('volume', volume),('orderSide', side),('ordertype', order_type), ('clientRequestId', client_request_id)]) @@ -65,19 +64,19 @@ def order_create(self, currency, instrument, price, volume, side, order_type, cl def order_history(self, currency, instrument, limit, since): - + data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)]) postData = json.dumps(data, separators=(',', ':')) return post_request(self.key, self.secret, '/order/history', postData) def order_open(self, currency, instrument, limit, since): - + data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)]) postData = json.dumps(data, separators=(',', ':')) return post_request(self.key, self.secret, '/order/open', postData) def order_detail(self, order_ids): - data_obj = {'orderIds':order_ids} + data_obj = {'orderIds':order_ids} postData = json.dumps(data_obj, separators=(',', ':')) return post_request(self.key, self.secret, '/order/detail', postData) diff --git a/main.py b/main.py index 57ba64e..56bf891 100644 --- a/main.py +++ b/main.py @@ -13,5 +13,5 @@ #print client.order_create('AUD', 'LTC', 100000000, 100000000, 'Bid', 'Limit', '1') -print client.get_market_tick('ETH','AUD') +print(client.get_market_tick('ETH','AUD'))