Advertisement
Guest User

mtgox sign API v2 /1 /0

a guest
Mar 22nd, 2013
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. def perform(self, path, params,JSON=True,API_VERSION=0):
  2.     self.__url_parts = "https://data.mtgox.com/api/"
  3.         self.throttle()
  4.         nonce =  str(int(time.time()*1000))
  5.         if params != None:
  6.             params = params.items()
  7.         else:
  8.             params = []
  9.  
  10.         params += [(u'nonce',nonce)]
  11.         post_data = urllib.urlencode(params)
  12.         ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),post_data,hashlib.sha512).digest()))
  13.  
  14.         if API_VERSION == 0:
  15.             url = self.__url_parts + '0/' + path
  16.         elif API_VERSION == 1:
  17.             url = self.__url_parts + '1/' + path
  18.         else: #assuming API_VERSION 2
  19.             url = self.__url_parts + '2/' + path
  20.             api2postdatatohash = path + chr(0) + post_data          #new way to hash for API 2, includes path + NUL
  21.             ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest()))
  22.        
  23.         # Create header for auth-requiring operations
  24.         header = {
  25.             "User-Agent": 'genBTC-bot',
  26.             "Rest-Key": self.key,
  27.             "Rest-Sign": ahmac
  28.             }
  29.  
  30.         while True:
  31.             try:
  32.                 req = urllib2.Request(url, post_data, header)
  33.                 with closing(urllib2.urlopen(req, post_data,timeout=self.timeout)) as res:
  34.                     if JSON == True:
  35.                         try:
  36.                             data = json.load(res,object_hook=json_ascii.decode_dict)
  37.                             if "error" in data:
  38.                                 if data["error"] == "Not logged in.":
  39.                                     print UserError(data["error"])
  40.                                 else:
  41.                                     print ServerError(data["error"])
  42.                             else:
  43.                                 return data
  44.                         except ValueError as e:
  45.                             print "JSON Error: %s. Most likely BLANK Data. Still trying to figure out what happened here." % e
  46.                             data = "dummy"
  47.                             unchobj = res
  48.                             print unchobj.read()
  49.                     else:
  50.                         data = res.read()
  51.                         return data
  52.             except urllib2.HTTPError as e:
  53.                 print e
  54.             except urllib2.URLError as e:
  55.                 print "URL Error %s: %s." % (e.reason,e)
  56.             except ssl.SSLError as e:
  57.                 print "SSL Error: %s." % e
  58.             except Exception as e:
  59.                 print "General Error: %s" % e
  60.             print "Retrying Connection...."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement