Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def perform(self, path, params,JSON=True,API_VERSION=0):
- self.__url_parts = "https://data.mtgox.com/api/"
- self.throttle()
- nonce = str(int(time.time()*1000))
- if params != None:
- params = params.items()
- else:
- params = []
- params += [(u'nonce',nonce)]
- post_data = urllib.urlencode(params)
- ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),post_data,hashlib.sha512).digest()))
- if API_VERSION == 0:
- url = self.__url_parts + '0/' + path
- elif API_VERSION == 1:
- url = self.__url_parts + '1/' + path
- else: #assuming API_VERSION 2
- url = self.__url_parts + '2/' + path
- api2postdatatohash = path + chr(0) + post_data #new way to hash for API 2, includes path + NUL
- ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest()))
- # Create header for auth-requiring operations
- header = {
- "User-Agent": 'genBTC-bot',
- "Rest-Key": self.key,
- "Rest-Sign": ahmac
- }
- while True:
- try:
- req = urllib2.Request(url, post_data, header)
- with closing(urllib2.urlopen(req, post_data,timeout=self.timeout)) as res:
- if JSON == True:
- try:
- data = json.load(res,object_hook=json_ascii.decode_dict)
- if "error" in data:
- if data["error"] == "Not logged in.":
- print UserError(data["error"])
- else:
- print ServerError(data["error"])
- else:
- return data
- except ValueError as e:
- print "JSON Error: %s. Most likely BLANK Data. Still trying to figure out what happened here." % e
- data = "dummy"
- unchobj = res
- print unchobj.read()
- else:
- data = res.read()
- return data
- except urllib2.HTTPError as e:
- print e
- except urllib2.URLError as e:
- print "URL Error %s: %s." % (e.reason,e)
- except ssl.SSLError as e:
- print "SSL Error: %s." % e
- except Exception as e:
- print "General Error: %s" % e
- print "Retrying Connection...."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement