diff --git a/.project b/.project new file mode 100755 index 0000000..4e21efe --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + cex.io-api-python + + + + + + org.python.pydev.PyDevBuilder + + + + + + org.python.pydev.pythonNature + + diff --git a/.pydevproject b/.pydevproject new file mode 100755 index 0000000..d001f0a --- /dev/null +++ b/.pydevproject @@ -0,0 +1,5 @@ + + +Default +python interpreter + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100755 index 0000000..69defe9 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding//cexapi/cexapi.py=utf-8 diff --git a/CHANGES.txt b/CHANGES.txt old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/cexapi/.cexapi.py.swp b/cexapi/.cexapi.py.swp new file mode 100644 index 0000000..5d3c204 Binary files /dev/null and b/cexapi/.cexapi.py.swp differ diff --git a/cexapi/__init__.py b/cexapi/__init__.py old mode 100644 new mode 100755 index 010ce9b..f74459d --- a/cexapi/__init__.py +++ b/cexapi/__init__.py @@ -1 +1 @@ -from cexapi import * +from .cexapi import * diff --git a/cexapi/cexapi.py b/cexapi/cexapi.py old mode 100644 new mode 100755 index 8b86b88..cfdcba0 --- a/cexapi/cexapi.py +++ b/cexapi/cexapi.py @@ -1,81 +1,87 @@ -# -*- coding: utf-8 -*- -# Author: t0pep0 -# e-mail: t0pep0.gentoo@gmail.com -# Jabber: t0pep0@jabber.ru -# BTC : 1ipEA2fcVyjiUnBqUx7PVy5efktz2hucb -# donate free =) -import hmac -import hashlib -import time -import urllib -import urllib2 -import json - - -class API(object): - __username = '' - __api_key = '' - __api_secret = '' - __nonce_v = '' - - # Init class - def __init__(self, username, api_key, api_secret): - self.__username = username - self.__api_key = api_key - self.__api_secret = api_secret - - # get timestamp as nonce - def __nonce(self): - self.__nonce_v = '{:.10f}'.format(time.time() * 1000).split('.')[0] - - # generate segnature - def __signature(self): - string = self.__nonce_v + self.__username + self.__api_key # create string - signature = hmac.new(self.__api_secret, string, digestmod=hashlib.sha256).hexdigest().upper() # create signature - return signature - - def __post(self, url, param): # Post Request (Low Level API call) - params = urllib.urlencode(param) - req = urllib2.Request(url, params, {'User-agent': 'bot-cex.io-' + self.__username}) - page = urllib2.urlopen(req).read() - return page - - def api_call(self, method, param={}, private=0, couple=''): # api call (Middle level) - url = 'https://cex.io/api/' + method + '/' # generate url - if couple != '': - url = url + couple + '/' # set couple if needed - if private == 1: # add auth-data if needed - self.__nonce() - param.update({ - 'key': self.__api_key, - 'signature': self.__signature(), - 'nonce': self.__nonce_v}) - answer = self.__post(url, param) # Post Request - return json.loads(answer) # generate dict and return - - def ticker(self, couple='GHS/BTC'): - return self.api_call('ticker', {}, 0, couple) - - def order_book(self, couple='GHS/BTC'): - return self.api_call('order_book', {}, 0, couple) - - def trade_history(self, since=1, couple='GHS/BTC'): - return self.api_call('trade_history', {"since": str(since)}, 0, couple) - - def balance(self): - return self.api_call('balance', {}, 1) - - def current_orders(self, couple='GHS/BTC'): - return self.api_call('open_orders', {}, 1, couple) - - def cancel_order(self, order_id): - return self.api_call('cancel_order', {"id": order_id}, 1) - - def place_order(self, ptype='buy', amount=1, price=1, couple='GHS/BTC'): - return self.api_call('place_order', {"type": ptype, "amount": str(amount), "price": str(price)}, 1, couple) - - def price_stats(self, last_hours, max_resp_arr_size, couple='GHS/BTC'): - return self.api_call( - 'price_stats', - {"lastHours": last_hours, "maxRespArrSize": max_resp_arr_size}, - 0, couple) +# -*- coding: utf-8 -*- +# Author: t0pep0 +# e-mail: t0pep0.gentoo@gmail.com +# Jabber: t0pep0@jabber.ru +# BTC : 1ipEA2fcVyjiUnBqUx7PVy5efktz2hucb +# donate free =) +import hashlib +import hmac +import time +import requests + + +class API(object): + __username = '' + __api_key = '' + __api_secret = '' + __nonce_v = '' + + # Init class + def __init__(self, username="", api_key="", api_secret=""): + self.__username = username + self.__api_key = api_key + self.__api_secret = api_secret + + # get timestamp as nonce + def __nonce(self): + self.__nonce_v = '{:.10f}'.format(time.time() * 1000).split('.')[0] + + # generate segnature + def __signature(self): + string = self.__nonce_v + self.__username + self.__api_key # create string + signature = hmac.new(self.__api_secret, string, digestmod=hashlib.sha256).hexdigest( + ).upper() # create signature + return signature + + def __post(self, url, params): # Post Request (Low Level API call) + req = requests.post( + url, data=params, headers={'User-agent': 'bot-cex.io-' + self.__username}) + page = req.json() + return page + + def api_call(self, method, param={}, private=0, couple=''): # api call (Middle level) + url = 'https://cex.io/api/' + method + '/' # generate url + if couple != '': + url = url + couple + '/' # set couple if needed + if private == 1: # add auth-data if needed + self.__nonce() + param.update({ + 'key': self.__api_key, + 'signature': self.__signature(), + 'nonce': self.__nonce_v}) + answer = self.__post(url, param) # Post Request + return answer # generate dict and return + + def ticker(self, couple='GHS/BTC'): + return self.api_call('ticker', {}, 0, couple) + + def order_book(self, couple='GHS/BTC'): + return self.api_call('order_book', {}, 0, couple) + + def trade_history(self, since=1, couple='GHS/BTC'): + return self.api_call('trade_history', {"since": str(since)}, 0, couple) + + def balance(self): + return self.api_call('balance', {}, 1) + + def current_orders(self, couple='GHS/BTC'): + return self.api_call('open_orders', {}, 1, couple) + + def cancel_order(self, order_id): + return self.api_call('cancel_order', {"id": order_id}, 1) + + def place_order(self, ptype='buy', amount=1, price=1, couple='GHS/BTC'): + return self.api_call('place_order', {"type": ptype, "amount": str(amount), "price": str(price)}, 1, couple) + + def place_market_order(self, ptype='buy', amount=1, couple='GHS/BTC'): + return self.api_call('place_order', {"type": ptype, "amount": str(amount), "order_type": "market"}, 1, couple) + + def markets(self): + response = self.api_call("currency_limits", ) + return [pair['symbol1'] + "/" + pair['symbol2'] for pair in response['data']['pairs']] + + def price_stats(self, last_hours, max_resp_arr_size, couple='GHS/BTC'): + return self.api_call( + 'price_stats', + {"lastHours": last_hours, "maxRespArrSize": max_resp_arr_size}, + 0, couple) diff --git a/cexapi/test.py b/cexapi/test.py old mode 100644 new mode 100755 index 60886a7..ef62484 --- a/cexapi/test.py +++ b/cexapi/test.py @@ -1,28 +1,28 @@ -# -*- coding: utf-8 -*- -import cexapi - -demo = cexapi.api(username, api_key, api_secret) -print "Ticker (GHS/BTC)" -print demo.ticker() ## or demo.ticker('GHS/BTC') -print "Ticker (BF1/BTC)" -print demo.ticker('BF1/BTC') -print "Order book (GHS/BTC)" -print demo.order_book() ## or demo.order_book('GHS/BTC') -print "Order book (BF1/BTC)" -print demo.order_book('BF1/BTC') -print "Trade history since=100 (GHS/BTC)" -print demo.trade_history(100) ## or (100,'GHS/BTC') -print "Trade history since=100 (BF1/BTC)" -print demo.trade_history(100,'BF1/BTC') -print "Balance" -print demo.balance() -print "Open orders (GHS/BTC)" -print demo.current_orders() ## or ('GHS/BTC') -print "Open orders (BF1/BTC)" -print demo.current_orders('BF1/BTC') -print "Cancel order (order_id=100)" -print demo.cancel_order(100) -print "Plaсe order buy 4GHS/0.1BTC)" -print demo.place_order('buy',1,0.1) ## or ('buy',1,0.1,'GHS/BTC') -print "Open orders sell 1BF1/1.5BTC" -print demo.place_order('sell',1,1.5,'BF1/BTC') +# -*- coding: utf-8 -*- +from . import cexapi + +demo = cexapi.api(username, api_key, api_secret) +print("Ticker (GHS/BTC)") +print(demo.ticker()) ## or demo.ticker('GHS/BTC') +print("Ticker (BF1/BTC)") +print(demo.ticker('BF1/BTC')) +print("Order book (GHS/BTC)") +print(demo.order_book()) ## or demo.order_book('GHS/BTC') +print("Order book (BF1/BTC)") +print(demo.order_book('BF1/BTC')) +print("Trade history since=100 (GHS/BTC)") +print(demo.trade_history(100)) ## or (100,'GHS/BTC') +print("Trade history since=100 (BF1/BTC)") +print(demo.trade_history(100,'BF1/BTC')) +print("Balance") +print(demo.balance()) +print("Open orders (GHS/BTC)") +print(demo.current_orders()) ## or ('GHS/BTC') +print("Open orders (BF1/BTC)") +print(demo.current_orders('BF1/BTC')) +print("Cancel order (order_id=100)") +print(demo.cancel_order(100)) +print("Plaсe order buy 4GHS/0.1BTC)") +print(demo.place_order('buy',1,0.1)) ## or ('buy',1,0.1,'GHS/BTC') +print("Open orders sell 1BF1/1.5BTC") +print(demo.place_order('sell',1,1.5,'BF1/BTC')) diff --git a/setup.cfg b/setup.cfg old mode 100644 new mode 100755 diff --git a/setup.py b/setup.py old mode 100644 new mode 100755