Here is a full API v0 example using the authentication code posted earlier, if anyone is interested...
It's only missing withdraw, btcAddress, and history_[CUR].
from urllib import urlencode
import urllib2
import time
from hashlib import sha512
from hmac import HMAC
import base64
import json
from pprint import pprint
class MtGox:
def __init__(self):
self.key = 'x'
self.secret = 'x'
self.auth = Authenticate(self.key,self.secret)
def getTrades(self, since):
path = "getTrades.php"
args = {'since': since}
return self.auth.run(path,args)
def getDepth(self):
path = "data/getDepth.php"
args = {'Currency': 'USD'}
return self.auth.run(path,args)
def getFunds(self):
path = "getFunds.php"
args = {}
return self.auth.run(path,args)
def buyBTC(self, amount, price):
path = "buyBTC.php"
args = {'amount': amount, 'price': price, 'Currency':'USD'}
return self.auth.run(path,args)
def sellBTC(self, amount, price):
path = "sellBTC.php"
args = {'amount': amount, 'price': price, 'Currency':'USD'}
return self.auth.run(path,args)
def getOrders(self):
path = "getOrders.php"
args = {}
return self.auth.run(path,args)
def cancelOrder(self, oid, type):
path = "cancelOrder.php"
args = {'oid': oid, 'type': type}
return self.auth.run(path,args)
def redeemCode(self, code):
path = "redeemCode.php"
args = {'code': code}
return self.auth.run(path,args)
def getInfo(self):
path = "info.php"
args = {}
return self.auth.run(path,args)
def getTicker(self):
path = "data/ticker.php"
args = {}
return self.auth.run(path,args)
def printDic(self, dic):
pprint(dic)
#MtGox End
def get_nonce():
return int(time.time()*100000)
def sign_data(secret, data):
return base64.b64encode(str(HMAC(secret, data, sha512).digest()))
class Authenticate:
def __init__(self, auth_key, auth_secret):
self.auth_key = auth_key
self.auth_secret = base64.b64decode(auth_secret)
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 run(self, path, args):
data, headers = self.build_query(args)
req = urllib2.Request("https://mtgox.com/api/0/"+path, data, headers)
res = urllib2.urlopen(req, data)
return json.load(res)
#Authenticate End