聚币网API[Python2版]
聚币 现货 API [Python2版]
一、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 = ' + 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 = "
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.zip
聚币网API[Python2版]的更多相关文章
- 聚币网API[Python3版]
代码 #!/usr/bin/env python # -*- coding:utf-8 -*- import hashlib import requests import time import ur ...
- 聚币网API使用教程 demo
原文 http://30daydo.com/article/181 目前还在完善,等功能完善了,就更新到csdn. 更新 2017-05-27 官方有API的文档,可是这个文档就像一个草稿一样,两个基 ...
- 火币网现货API[Python3版]
火币 期货 现货 API [Python3版] 一.Util.py,基础类,包括参数配置.签名,HTTP 请求方法,发送信息到API #coding=utf-8 import hashlib impo ...
- 淘宝网触屏版 - 学习笔记(1 - 关于meta)
注:本文是学习笔记,并不是教程,所以会有很多我不理解或猜测的问题,也会有不尽详实之处,望见谅. <meta charset="utf-8"> <meta cont ...
- 淘宝网触屏版 - 学习笔记(0 - 关于dpr)
注:本文是学习笔记,并不是教程,所以会有很多我不理解或猜测的问题,也会有不尽详实之处,望见谅. 对于pc端网页设计师来说,移动端的网页制作,我之前只是简单的加了一个 <meta name=&qu ...
- [转载]中国天气网API
最近在做个网站要用到天气网的api,在网上找了些参考资料,这篇文章对天气网api的介绍比较详细,所以转载之,谢谢原作者的辛勤劳动和奉献精神. 原文地址:http://g.kehou.com/t1033 ...
- OKCoin期货现货API[Python3版]
OKCoin 期货 现货 API [Python版] 一.HttpMD5Util.py,基础类,包括MD5签名,HTTP Post及HTTP Get方法 #!/usr/bin/python # -*- ...
- 火币Huobi API Websocket
本文介绍火币Huobi API Websocket WebSocket API简介 WebSocket协议是基于TCP的一种新的网络协议.它实现了客户端与服务器之间在单个 tcp 连接上的全双工通信, ...
- 火币Huobi API
本文介绍火币Huobi API REST API 简介 火币为用户提供了一套全新的API,可以帮用户快速接入火币PRO站及HADAX站的交易系统,实现程序化交易. 访问地址 适用站点 适用功能 适用交 ...
随机推荐
- zip & tar 压缩文件时排除某个文件夹
确实是用参数 -x aaa bbb 两个文件夹要写全路径. 如 zip -r test.zip test -x /test/aaa/* -x /test/bbb/* 既包含了文件夹,也可以 zip - ...
- 我眼中的ASP.NET Core之微服务 (二)
前言 接上一篇. 上一篇未完待续的原因是当时刚好是6-30号晚上马上12点了还没写完,然后我想赶在7月1号之前发出去,所以当时就发了.然后在发的时候出了一点问题,结果发出去的时候刚好是 7.1号 00 ...
- python基础(6):列表和字典类型
前面我们所接触的数据类型都是存单个值,今天我们来看看可以存多个值的列表和字典. 预习: 1.有列表data=['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日 ...
- 关于EF第一次加载慢或过一段时间不访问时再次访问加载慢问题的总结
优化方案 1.安装Application Initialization 这是在iis8出来后才有的,iis8内置的功能,而对于iis7.5也提供了一个扩展以支持这个功能. Application In ...
- 定时任务FluentScheduler 学习笔记 .net
第一步添加引用 GitHub源码地址 与详细用法 https://github.com/fluentscheduler/FluentScheduler 下面开始简单的实现 /// <summar ...
- [js高手之路] es6系列教程 - var, let, const详解
function show( flag ){ console.log( a ); if( flag ){ var a = 'ghostwu'; return a; } else { console.l ...
- 有关cxf与安卓客户端或者C客户端的中文乱码问题
前段时间在与C的客户端和安卓的客户端进行联调,首先我的方法接收的是C客户端所传递的数据,但是传递到方法内的中文就变成了乱码(我的方法的编码是utf8),最后通过String a = new Strin ...
- 在drawRect:方法中绘制图片,文字以及Core Graphics 框架的了解
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...
- HDU-2017-字符串统计
/* Name: HDU-2017-字符串统计 Date: 18/04/17 20:19 Description: 水过 */ #include<bits/stdc++.h> using ...
- 微信开发获取用户OpenID
第一次开发微信版网页,对最重要的获取微信OpenId,特此记录下来 1.首先得有appid和appsecret . public class WeiXin { public static string ...