聚币网API[Python2版]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聚币网API[Python2版]相关的知识,希望对你有一定的参考价值。
聚币 现货 API [Python3版]
一、utils.py,基础类,包括HTTP 请求、签名等
# -*- coding: utf-8 -*- import hashlib import hmac import time import urllib import urllib2 import json from datetime import datetime from uuid import UUID from objutil import dict_obj # import requests def http_get(url, data_wrap, encode=False): if encode is True: data_wrap = urllib.urlencode(data_wrap) req = urllib2.Request(url, data=data_wrap) resp = urllib2.urlopen(req).read() dic = json.loads(resp) return dict_obj(dic) def get_signature(private_key, data): data_en = urllib.urlencode(data) md5_hash = getHash(private_key) msg = bytes(data_en).encode(‘utf-8‘) key = bytes(md5_hash).encode(‘utf-8‘) signature = hmac.new(key, msg, digestmod=hashlib.sha256).digest() last_warp = "%s&signature=%s" % (data_en, toHex(signature)) return last_warp def get_nonce_time(): curr_stamp = time.time() * 100 return str(long(curr_stamp)) def getHash(s): m = hashlib.md5() m.update(s) return m.hexdigest() def toHex(str): lst = [] for ch in str: hv = hex(ord(ch)).replace(‘0x‘, ‘‘) if len(hv) == 1: hv = ‘0‘ + hv lst.append(hv) return reduce(lambda x, y: x + y, lst) def getUserData(cfg_file): f = open(cfg_file, ‘r‘) account = {} for i in f.readlines(): ctype, passwd = i.split(‘=‘) account[ctype.strip()] = passwd.strip() return account class CJsonEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, datetime): return obj.strftime(‘%Y-%m-%d %H:%M:%S‘) elif isinstance(obj, UUID): return str(obj) else: return json.JSONEncoder.default(self, obj) def json_dumps(result): return json.dumps(result, cls=CJsonEncoder)
二、jubi.py,聚币网API
# -*- coding: utf-8 -*- # @Author: wujiyu # @Date: 2017-07-09 10:44:41 # @Last Modified by: far # @Last Modified time: 2017-07-25 09:28:12 from utils import * BASE_API = "https://www.jubi.com/api/v1" TICKER_API = "%s/ticker" % BASE_API DEPTH_API = "%s/depth" % BASE_API ORDERS_API = "%s/orders" % BASE_API BALANCE_API = "%s/balance" % BASE_API TRADLIST_API = "%s/trade_list" % BASE_API TRADEVIEW_API = "%s/trade_view" % BASE_API TRADECANCEL_API = "%s/trade_cancel" % BASE_API TRADEADD_API = "%s/trade_add" % BASE_API class JuBi(object): """docstring for JuBi""" def __init__(self): super(JuBi, self).__init__() cfg = getUserData(‘data.cfg‘) self.public_key = cfg[‘public_key‘] self.private_key = cfg[‘private_key‘] def get_ticker(self, coin): data_wrap = {‘coin‘: coin} return http_get(TICKER_API, data_wrap, True) def get_depth(self, coin): data_wrap = {‘coin‘: coin} return http_get(DEPTH_API, data_wrap, True) def get_orders(self, coin): data_wrap = {‘coin‘: coin} return http_get(ORDERS_API, data_wrap, True) def get_balance(self): nonce = get_nonce_time() data_wrap = {‘nonce‘: nonce, ‘key‘: self.public_key} all_data = get_signature(self.private_key, data_wrap) return http_get(BALANCE_API, all_data) def get_trade_list(self, coin): # open:正在挂单, all:所有挂单 trade_type = "open" since = "0" nonce = get_nonce_time() data_wrap = {‘nonce‘: nonce, ‘type‘: trade_type, ‘coin‘: coin, ‘since‘: since, ‘key‘: self.public_key} all_data = get_signature(self.private_key, data_wrap) return http_get(TRADLIST_API, all_data) def get_trade_view_list(self, coin, id): nonce = get_nonce_time() data_wrap = {‘nonce‘: nonce, ‘coin‘: coin, ‘key‘: self.public_key, ‘id‘: id} all_data = get_signature(self.private_key, data_wrap) return http_get(TRADEVIEW_API, all_data) def cancel(self, coin, id): nonce = get_nonce_time() data_wrap = {‘nonce‘: nonce, ‘coin‘: coin, ‘key‘: self.public_key, ‘id‘: id} all_data = get_signature(self.private_key, data_wrap) return http_get(TRADECANCEL_API, all_data) def trade_add(self, coin, amount, price, sell_type): nonce = get_nonce_time() data_wrap = {‘nonce‘: nonce, ‘coin‘: coin, ‘key‘: self.public_key, ‘amount‘: amount, "price": price, "type": sell_type} all_data = get_signature(self.private_key, data_wrap) return http_get(TRADEADD_API, all_data) def sell(self, coin, amount, price): return self.trade_add(coin, amount, price, "sell") def buy(self, coin, amount, price): return self.trade_add(coin, amount, price, "buy") def cancel_all(self, coin, sell_type="all"): lst = self.get_trade_list(coin) print("当前挂单!!!!!!!!!!:%s" % (lst)) for item in lst: if sell_type == "all" or sell_type == item["type"]: self.cancel(coin, item["id"]) print("取消挂单成功!!!!!!!!!") print("当前挂单!!!!!!!!!!:%s" % (self.get_trade_list(coin))) return True def cancel_all_sell(self, coin): return self.cancel_all(coin, "sell") def cancel_all_buy(self, coin): return self.cancel_all(coin, "buy")
三、调用方法
coin = "btc" jubi = JuBi() print(jubi.get_ticker(coin)) # print(jubi.get_depth(coin)) # print(jubi.get_orders(coin)) # print(jubi.get_balance()) # print(jubi.get_trade_list(coin)) # print(jubi.get_trade_view_list(coin, "1")) # print(jubi.get_trade_cancel_list(coin, "1")) # print(jubi.sell(coin, 10000, 0.001)) # print(jubi.buy(coin, 100, 0.2)) # print(jubi.get_trade_cancel_list(coin, "1")) # print(jubi.cancel(coin, 940591))
四、下载地址
http://www.cnblogs.com/fangbei/p/jubi-api-python.html
http://files.cnblogs.com/files/fangbei/jubi-api-python2.rar
以上是关于聚币网API[Python2版]的主要内容,如果未能解决你的问题,请参考以下文章