slush

Untitled

Nov 19th, 2017
187
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. def main():
  4.     API_KEY = 'YOUR-API-KEY'
  5.     SECRET = 'you-api-secret'
  6.     DAILY_LIMIT_USD = 24000
  7.     WITHDRAW_ADDRESS = 'your-xmr-address'
  8.     WITHDRAW_CURRENCY = 'XMR'
  9.     WITHDRAW_CURRENCY_NAME = 'monero'
  10.  
  11.     amount = DAILY_LIMIT_USD / usdValue(WITHDRAW_CURRENCY_NAME.lower())
  12.  
  13.     # Documentation: https://poloniex.com/support/api/
  14.     p = poloniex(API_KEY, SECRET)
  15.  
  16.     print("Withdrawing %s" % amount)
  17.     print(p.api_query('withdraw', {'currency': WITHDRAW_CURRENCY,
  18.                                     'amount': amount,
  19.                                     'address': WITHDRAW_ADDRESS}))
  20.  
  21. ## -----------------------------------------------------
  22.  
  23. import urllib
  24. import urllib2
  25. import json
  26. import time
  27. import hmac,hashlib
  28.  
  29. def usdValue(currency):
  30.     ret = urllib2.urlopen(urllib2.Request('https://api.coinmarketcap.com/v1/ticker/%s/?convert=USD' % currency))
  31.     data = json.loads(ret.read())
  32.     return float(data[0]['price_usd'])
  33.  
  34. def createTimeStamp(datestr, format="%Y-%m-%d %H:%M:%S"):
  35.     return time.mktime(time.strptime(datestr, format))
  36.  
  37. class poloniex:
  38.     def __init__(self, APIKey, Secret):
  39.         self.APIKey = APIKey
  40.         self.Secret = Secret
  41.  
  42.     def post_process(self, before):
  43.         after = before
  44.  
  45.         # Add timestamps if there isnt one but is a datetime
  46.         if('return' in after):
  47.             if(isinstance(after['return'], list)):
  48.                 for x in xrange(0, len(after['return'])):
  49.                     if(isinstance(after['return'][x], dict)):
  50.                         if('datetime' in after['return'][x] and 'timestamp' not in after['return'][x]):
  51.                             after['return'][x]['timestamp'] = float(createTimeStamp(after['return'][x]['datetime']))
  52.                            
  53.         return after
  54.  
  55.     def api_query(self, command, req={}):
  56.  
  57.         if(command == "returnTicker" or command == "return24Volume"):
  58.             ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/public?command=' + command))
  59.             return json.loads(ret.read())
  60.         elif(command == "returnOrderBook"):
  61.             ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/public?command=' + command + '&currencyPair=' + str(req['currencyPair'])))
  62.             return json.loads(ret.read())
  63.         elif(command == "returnMarketTradeHistory"):
  64.             ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/public?command=' + "returnTradeHistory" + '&currencyPair=' + str(req['currencyPair'])))
  65.             return json.loads(ret.read())
  66.         else:
  67.             req['command'] = command
  68.             req['nonce'] = int(time.time()*1000)
  69.             post_data = urllib.urlencode(req)
  70.  
  71.             sign = hmac.new(self.Secret, post_data, hashlib.sha512).hexdigest()
  72.             headers = {
  73.                 'Sign': sign,
  74.                 'Key': self.APIKey
  75.             }
  76.  
  77.             ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/tradingApi', post_data, headers))
  78.             jsonRet = json.loads(ret.read())
  79.             return self.post_process(jsonRet)
  80.  
  81. if __name__ == '__main__':
  82.     main()
RAW Paste Data