-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi_mtgox.py
80 lines (67 loc) · 2.72 KB
/
api_mtgox.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
from urllib import urlencode
import urllib2
import time
from hashlib import sha512
from hmac import HMAC
import base64
import json
def get_nonce():
return int(time.time()*100000)
def sign_data(secret, data):
return base64.b64encode(str(HMAC(secret, data, sha512).digest()))
class requester:
def __init__(self, auth_key, auth_secret, url=None):
self.auth_key = auth_key
self.auth_secret = base64.b64decode(auth_secret)
self.url = url or "https://mtgox.com/api/0/"
def build_query(self, req={}):
req["nonce"] = get_nonce()
post_data = urlencode(req)
headers = {}
headers["User-Agent"] = "GoxApi"
headers["Rest-Key"] = self.auth_key
headers["Rest-Sign"] = sign_data(self.auth_secret, post_data)
return (post_data, headers)
def perform(self, path, args):
data, headers = self.build_query(args)
req = urllib2.Request(self.url+path, data, headers)
res = urllib2.urlopen(req, data)
return json.load(res)
class api():
def __init__(self, site=None):
self.maxp = 100
self.minp = 0.1
self.maxq = 1000
self.minq = 0
self.site = (site or 'mtgox').strip().lower()
if self.site in ['mtgox','bitfl']:
# separate the truly secret from the open API specs
s = __import__('secrets.'+self.site) # will s then be a temporary variable?
self.a = __import__('api.'+self.site)
self.r = requester(auth_key=s.key, auth_secret=s.secret, url=self.a.url)
def info(param=None):
data = self.r.perform(path = self.a.path['info'], args={})
if param and param in data:
return data[param]
return data
def trade(quantity=None, price=0, direction=None):
if ( self.minq<quantity<=self.maxq and self.minp<=price<=self.maxp
and direction in ['buy','sell'] ):
return self.r.perform(path = self.a.path[direction],
args = self.a.args(direction=direction, quantity=quantity, price=price))
def buy(quantity=None,price=0):
return trade(quantity=quantity, price=price, direction='buy')
def sell(quantity=None,price=0):
return trade(quantity=quantity, price=price, direction='sell')
def test():
from pprint import pprint
import secrets.mtgox as s
import api.mtgox as a
r = requester(auth_key=s.key, auth_secret=s.secret, url=a.url)
pprint(r.perform(path = a.path['info'], args={}))
def test1():
from pprint import pprint
import secrets.mtgox as s
import api.mtgox as a
r = requester(auth_key=s.key, auth_secret=s.secret, url=a.url1)
pprint(r.perform(path = a.path1['ticker'], args={}))