This repository has been archived by the owner on Feb 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
MtGoxRequester.py
69 lines (58 loc) · 2.44 KB
/
MtGoxRequester.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
import time
from hashlib import sha512
from hmac import HMAC
import base64
import urllib
from Requester import Requester
from Account import Account
from Depth import Depth
# Manage every requests to MtGox API
class MtGoxRequester(Requester):
def __init__(self, authId, authPass):
Requester.__init__(self, "https://mtgox.com/api/1")
self._authKey = authId
self._authSecret = base64.b64decode(authPass.encode())
# for now, we will manage only EUR.
#self._availableCurrencies = {"USD", "EUR", "JPY", "CAD", "GBP", "CHF", "RUB", "AUD", "SEK", "DKK", "HKD", "PLN", "CNY", "SGD", "THB", "NZD", "NOK"}
self._availableCurrencies = {"EUR"}
def GetNonce(self):
return int(time.time()*100000)
def SignData(self, secret, data):
return base64.b64encode(HMAC(secret, data.encode(), sha512).digest())
def BuildQuery(self, req={}, withAuth=0):
req["nonce"] = self.GetNonce()
postData = urllib.parse.urlencode(req)
headers = {}
headers["User-Agent"] = "GoxApi"
if withAuth == 1:
headers["Rest-Key"] = self._authKey
headers["Rest-Sign"] = self.SignData(self._authSecret, postData)
return (postData, headers)
def Perform(self, url, args, withAuth):
res = Requester.Perform(self, url, args, withAuth)
if res["result"] != "success":
raise Exception("Request failed.")
return res
def GetAccount(self):
# get account infos
res = self.Perform("/generic/private/info", {}, 1)
result = res["return"]
a = Account()
a.tradeFee = result["Trade_Fee"]
a.wallets["BTC"] = result["Wallets"]["BTC"]["Balance"]["value"]
a.wallets["EUR"] = result["Wallets"]["EUR"]["Balance"]["value"]
# get btc address
res = self.Perform("/generic/bitcoin/address", {}, 1)
a.btcAddress = res["return"]["addr"];
return a
def UpdateDepth(self, depth):
for currency in self._availableCurrencies:
res = self.Perform("/BTC" + currency + "/depth", {}, 0)
res = res["return"]
bids = res["bids"]
asks = res["asks"]
currentDepth = depth[currency] = Depth(currency)
for bid in bids:
currentDepth.bids.append([bid["stamp"], bid["amount"], bid["price"]])
for ask in asks:
currentDepth.asks.append([ask["stamp"], ask["amount"], ask["price"]])