Author

Topic: Improved Python code for MTGox API (HTTP auth) (Read 1752 times)

newbie
Activity: 33
Merit: 0
Your code is for using the old http api to do commands?
I ask since now you can use the (more or less) same commands over the streaming api too..

Yes, this is for the HTTP API. It's much simpler to implement than the streaming API. It's also good to have a backup, as reports suggest the streaming API is somewhat unstable. I'm rewriting the streaming code from here as well.
legendary
Activity: 2126
Merit: 1001
Thank you, both!

Your code is for using the old http api to do commands?
I ask since now you can use the (more or less) same commands over the streaming api too..

I may find you the thread for this if interested.

Ente
member
Activity: 105
Merit: 10
I posted it in that thread for you.
newbie
Activity: 33
Merit: 0
I found this thread to be extremely helpful, so I'm returning the favor by posting an improved and simplified version of the code. Unfortunately, my forum account is limited to the Newbie section. Mods, please move this post to the thread above.
Code:
from hashlib import sha512
from hmac import HMAC
from base64 import b64decode, b64encode
from urllib import urlencode

from time import time

import requests

class MTGox:
def __init__(self, key, secret, base):
self.key = key
self.secret = b64decode(secret)
self.base = base

def query(self, path, args={}):
args['nonce'] = int(time()*100000)
headers = {
'User-Agent': 'BitcoinTalk',
'Rest-Key': self.key,
'Rest-Sign': b64encode(str(HMAC(self.secret, urlencode(args), sha512).digest()))
}
r = requests.post(self.base + path, data=args, headers=headers)
print r.text
Jump to: