From 4c4506aece8442d7c6432d972cb6024cb1d88d3f Mon Sep 17 00:00:00 2001 From: Aaron Cowper Date: Mon, 8 Jan 2018 23:18:51 +1100 Subject: [PATCH 1/2] Updated for Python 3 --- btcmarkets.py | 136 +++++++++++++++++++++++++------------------------- main.py | 2 +- 2 files changed, 69 insertions(+), 69 deletions(-) diff --git a/btcmarkets.py b/btcmarkets.py index f2ef506..2761d42 100644 --- a/btcmarkets.py +++ b/btcmarkets.py @@ -1,101 +1,101 @@ -import base64, hashlib, hmac, urllib2, time, urllib, json +import base64, hashlib, hmac, time, urllib, urllib.request, json from collections import OrderedDict +import ipdb base_url = 'https://api.btcmarkets.net' def request(action, key, signature, timestamp, path, data): - - header = { - 'accept': 'application/json', - 'Content-Type': 'application/json', - 'User-Agent': 'btc markets python client', - 'accept-charset': 'utf-8', - 'apikey': key, - 'signature': signature, - 'timestamp': timestamp, - } - - request = urllib2.Request(base_url + path, data, header) - if action == 'post': - response = urllib2.urlopen(request, data) - else: - response = urllib2.urlopen(request) - return json.load(response) + + header = { + 'accept': 'application/json', + 'Content-Type': 'application/json', + 'User-Agent': 'btc markets python client', + 'accept-charset': 'utf-8', + 'apikey': key, + 'signature': signature, + 'timestamp': timestamp, + } + + request = urllib.request.Request(base_url + path, data, header) + if action == 'post': + response = urllib.request.urlopen(request, data) + else: + 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" + + nowInMilisecond = str(int(time.time() * 1000)) + stringToSign = path + "\n" + nowInMilisecond + "\n" + signature = base64.b64encode(hmac.new(secret, stringToSign.encode('utf-8'), digestmod=hashlib.sha512).digest()) - signature = base64.b64encode(hmac.new(secret, stringToSign, digestmod=hashlib.sha512).digest()) - - return request('get', key, signature, nowInMilisecond, path, None) + 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 + + nowInMilisecond = str(int(time.time() * 1000)) + stringToSign = path + "\n" + nowInMilisecond + "\n" + postData - signature = base64.b64encode(hmac.new(secret, stringToSign, digestmod=hashlib.sha512).digest()) + signature = base64.b64encode(hmac.new(secret, stringToSign, digestmod=hashlib.sha512).digest()) - return request('post', key, signature, nowInMilisecond, path, postData) + return request('post', key, signature, nowInMilisecond, path, postData) class BTCMarkets: - def __init__(self, key, secret): - self.key = key - self.secret = base64.b64decode(secret) + def __init__(self, key, secret): + self.key = key + 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 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)]) - postData = json.dumps(data, separators=(',', ':')) - return post_request(self.key, self.secret, '/order/create', 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)]) + postData = json.dumps(data, separators=(',', ':')) + return post_request(self.key, self.secret, '/order/create', postData) - 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_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_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} - postData = json.dumps(data_obj, separators=(',', ':')) - return post_request(self.key, self.secret, '/order/detail', postData) + def order_detail(self, order_ids): + data_obj = {'orderIds':order_ids} + postData = json.dumps(data_obj, separators=(',', ':')) + return post_request(self.key, self.secret, '/order/detail', postData) - def account_balance(self): + def account_balance(self): - return get_request(self.key, self.secret, '/account/balance') + return get_request(self.key, self.secret, '/account/balance') - def get_market_tick(self,currency_in,currency_out): - - return get_request(self.key, self.secret, '/market/%s/%s/tick' % (currency_in,currency_out)) + def get_market_tick(self,currency_in,currency_out): + + return get_request(self.key, self.secret, '/market/%s/%s/tick' % (currency_in,currency_out)) - def get_market_orderbook(self,currency_in,currency_out): - - return get_request(self.key, self.secret, '/market/%s/%s/orderbook' % (currency_in,currency_out)) + def get_market_orderbook(self,currency_in,currency_out): + + return get_request(self.key, self.secret, '/market/%s/%s/orderbook' % (currency_in,currency_out)) - def get_market_trades(self,currency_in,currency_out): + def get_market_trades(self,currency_in,currency_out): - return get_request(self.key, self.secret, '/market/%s/%s/trades' % (currency_in,currency_out)) + return get_request(self.key, self.secret, '/market/%s/%s/trades' % (currency_in,currency_out)) 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')) From 0951795be1b7051fd3e2e88a2f15b79494656262 Mon Sep 17 00:00:00 2001 From: Aaron Cowper Date: Mon, 8 Jan 2018 23:21:58 +1100 Subject: [PATCH 2/2] Updated for Python 3 --- btcmarkets.py | 133 +++++++++++++++++++++++++------------------------- 1 file changed, 66 insertions(+), 67 deletions(-) diff --git a/btcmarkets.py b/btcmarkets.py index 2761d42..79c1a7b 100644 --- a/btcmarkets.py +++ b/btcmarkets.py @@ -1,101 +1,100 @@ import base64, hashlib, hmac, time, urllib, urllib.request, json from collections import OrderedDict -import ipdb base_url = 'https://api.btcmarkets.net' def request(action, key, signature, timestamp, path, data): - - header = { - 'accept': 'application/json', - 'Content-Type': 'application/json', - 'User-Agent': 'btc markets python client', - 'accept-charset': 'utf-8', - 'apikey': key, - 'signature': signature, - 'timestamp': timestamp, - } - - request = urllib.request.Request(base_url + path, data, header) - if action == 'post': - response = urllib.request.urlopen(request, data) - else: - response = urllib.request.urlopen(request) - return json.loads(response.read().decode('utf-8')) + + header = { + 'accept': 'application/json', + 'Content-Type': 'application/json', + 'User-Agent': 'btc markets python client', + 'accept-charset': 'utf-8', + 'apikey': key, + 'signature': signature, + 'timestamp': timestamp, + } + + request = urllib.request.Request(base_url + path, data, header) + if action == 'post': + response = urllib.request.urlopen(request, data) + else: + 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.encode('utf-8'), digestmod=hashlib.sha512).digest()) + + nowInMilisecond = str(int(time.time() * 1000)) + stringToSign = path + "\n" + nowInMilisecond + "\n" + signature = base64.b64encode(hmac.new(secret, stringToSign.encode('utf-8'), digestmod=hashlib.sha512).digest()) - return request('get', key, signature, nowInMilisecond, path, None) + 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 + + nowInMilisecond = str(int(time.time() * 1000)) + stringToSign = path + "\n" + nowInMilisecond + "\n" + postData - signature = base64.b64encode(hmac.new(secret, stringToSign, digestmod=hashlib.sha512).digest()) + signature = base64.b64encode(hmac.new(secret, stringToSign, digestmod=hashlib.sha512).digest()) - return request('post', key, signature, nowInMilisecond, path, postData) + return request('post', key, signature, nowInMilisecond, path, postData) class BTCMarkets: - def __init__(self, key, secret): - self.key = key - self.secret = base64.b64decode(secret) + def __init__(self, key, secret): + self.key = key + 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 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)]) - postData = json.dumps(data, separators=(',', ':')) - return post_request(self.key, self.secret, '/order/create', 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)]) + postData = json.dumps(data, separators=(',', ':')) + return post_request(self.key, self.secret, '/order/create', postData) - 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_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_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} - postData = json.dumps(data_obj, separators=(',', ':')) - return post_request(self.key, self.secret, '/order/detail', postData) + def order_detail(self, order_ids): + data_obj = {'orderIds':order_ids} + postData = json.dumps(data_obj, separators=(',', ':')) + return post_request(self.key, self.secret, '/order/detail', postData) - def account_balance(self): + def account_balance(self): - return get_request(self.key, self.secret, '/account/balance') + return get_request(self.key, self.secret, '/account/balance') - def get_market_tick(self,currency_in,currency_out): - - return get_request(self.key, self.secret, '/market/%s/%s/tick' % (currency_in,currency_out)) + def get_market_tick(self,currency_in,currency_out): + + return get_request(self.key, self.secret, '/market/%s/%s/tick' % (currency_in,currency_out)) - def get_market_orderbook(self,currency_in,currency_out): - - return get_request(self.key, self.secret, '/market/%s/%s/orderbook' % (currency_in,currency_out)) + def get_market_orderbook(self,currency_in,currency_out): + + return get_request(self.key, self.secret, '/market/%s/%s/orderbook' % (currency_in,currency_out)) - def get_market_trades(self,currency_in,currency_out): + def get_market_trades(self,currency_in,currency_out): - return get_request(self.key, self.secret, '/market/%s/%s/trades' % (currency_in,currency_out)) + return get_request(self.key, self.secret, '/market/%s/%s/trades' % (currency_in,currency_out))