Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojay committed Feb 10, 2014
1 parent efbc119 commit 56ee727
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
*.pyc
*.pyo
*.pid
*.swp
.idea/
log/*
temp/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
huobi
=====

python wrapper for huibo.com
python wrapper for huibo.com api (http://www.huobi.com/help/index.php?a=api_help)
63 changes: 63 additions & 0 deletions huobi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#coding=utf-8
import time, re, requests, md5, urllib, urllib2, json

URL = 'https://api.huobi.com/api.php'
def get_depth(currency='BTC', timeout=3):
if currency == 'BTC':
url = 'http://market.huobi.com/staticmarket/detail.html'
resp = requests.get(url, timeout=timeout)
return json.loads(re.search('view_detail\((.+)\)', resp.text).group(1))

class HuoBi(object):
def __init__(self, access_key, secret_key, request_timeout=3):
self.exchange = 'HUOBI'
self.access_key = access_key
self.secret_key = secret_key
self.request_timeout = request_timeout

def _sign(self, params):
params = '&'.join(sorted(["%s=%s"%(k, v) for k, v in params.items()]))
#print 's:',s
return md5.new(params).hexdigest().lower()

def _request(self, params):
params['access_key'] = self.access_key
params['secret_key'] = self.secret_key
params['created'] = str(int(time.time()))
sign = self._sign(params)
params['sign'] = sign
#print params
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
r = requests.post(URL, data=params, headers=headers, timeout=self.request_timeout)
return r.json()

def get_account_info(self):
params = {'method': 'get_account_info'}
return self._request(params)

def buy(self, price, amount, currency='BTC'):
params = {'method': 'buy',
'price': price,
'amount': amount}
return self._request(params)

def sell(self, price, amount, currency='BTC'):
params = {'method': 'sell',
'price': price,
'amount': amount}
return self._request(params)

def cancel_order(self, order_id, currency='BTC'):
params = {'method':'cancel_delegation',
'id':order_id}
return self._request(params)

def get_depth(self, currency, timeout=None):
if not timeout:
timeout = self.request_timeout
return get_depth(currency, timeout)

def get_order(self, order_id, currency='BTC'):
params = {'method':'delegation_info',
'id':order_id}
return self._request(params)

0 comments on commit 56ee727

Please sign in to comment.