forked from viorels/mtgox-trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
115 lines (98 loc) · 4.23 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from httplib2 import Http
import simplejson as json
from urlparse import urlunparse
from urllib import urlencode
class ServerError(Exception):
def __init__(self, ret):
self.ret = ret
def __str__(self):
return "Server error: %s" % self.ret
class UserError(Exception):
def __init__(self, errmsg):
self.errmsg = errmsg
def __str__(self):
return self.errmsg
class MTGox:
"""MTGox API"""
def __init__(self, user, password):
self.user = user
self.password = password
self.server = "mtgox.com"
self.timeout = 10
self.actions = {"_get_ticker": ("GET", "/code/data/ticker.php"),
"get_depth": ("GET", "/code/data/getDepth.php"),
"get_trades": ("GET", "/code/data/getTrades.php"),
"get_balance": ("POST", "/code/getFunds.php"),
"buy_btc": ("POST", "/code/buyBTC.php"),
"sell_btc": ("POST", "/code/sellBTC.php"),
"_get_orders": ("POST", "/code/getOrders.php"),
"_cancel_order": ("POST", "/code/cancelOrder.php"),
"_withdraw": ("POST", "/code/withdraw.php")}
for action, (method, _) in self.actions.items():
def _handler(action=action, **args):
return self._request(action, method=method, args=args)
setattr(self, action, _handler)
def get_ticker(self):
return self._get_ticker()["ticker"]
def get_orders(self):
return self._get_orders()["orders"] # can also return balance
def cancel_order(self, oid, typ=None):
orders = self.get_orders()
if typ is None:
order = [o for o in orders if o["oid"] == oid]
if order:
typ = order[0]["type"]
else:
raise UserError("unknown order/type")
return self._cancel_order(oid=oid, type=typ)
def withdraw(self, amount, btca, group1="BTC"):
return self._withdraw(amount=amount, btca=btca, group1=group1)["status"] # can also return balance
def _request(self, action, method="GET", args={}):
query = args.copy()
data = None
headers = {}
if method == "GET":
url = self._url(action)
if method == "POST":
url = self._url(action, scheme="https")
query["name"] = self.user
query["pass"] = self.password
data = urlencode(query)
headers['Content-type'] = 'application/x-www-form-urlencoded'
h = Http(cache=None, timeout=self.timeout)
try:
#print "%s %s\n> |%s|" % (method, url, data)
resp, content = h.request(url, method, headers=headers, body=data)
#print "< %s (%s)" % (content, resp)
if resp.status == 200:
data = json.loads(content)
if "error" in data:
raise UserError(data["error"])
else:
return data
else:
raise ServerError(content)
except AttributeError, e: # 'NoneType' object has no attribute 'makefile'
raise ServerError("timeout/refused")
except ValueError, e:
raise ServerError("%s: %s" % (e, content))
def _url(self, action, scheme="http", args={}):
url = urlunparse((scheme,
self.server,
self.actions[action][1], # path
'',
urlencode(args),
''))
return url
class ExchB(MTGox):
def __init__(self,user,password):
MTGox.__init__(self,user,password)
self.server = "www.exchangebitcoins.com"
self.actions = {"_get_ticker": ("GET", "/data/ticker"),
"get_depth": ("GET", "/data/depth"),
"get_trades": ("GET", "/data/recent"),
"get_balance": ("POST", "/data/getFunds"),
"buy_btc": ("POST", "/data/buyBTC"),
"sell_btc": ("POST", "/data/sellBTC"),
"_get_orders": ("POST", "/data/getOrders"),
"_cancel_order": ("POST", "/data/cancelOrder")}