HASHNET API HELPER v1.2


SUBMITTED BY: Guest

DATE: March 10, 2015, 2:06 p.m.

FORMAT: Python

SIZE: 3.7 kB

HITS: 1188

  1. #DONATION 16RdQZ86NUWQbE6rM86JbxnnaCDcAKqa9
  2. ####HASHNET API HELPER####
  3. #ver:1.2
  4. # delete_all_orders funtion is wrong
  5. #reply bad request!
  6. #still missing error checking
  7. from urllib import urlencode
  8. import urllib2
  9. import time
  10. import hashlib
  11. import hmac
  12. import base64
  13. import json
  14. import os
  15. class hashnest_connector:
  16. BASEURL='https://www.hashnest.com/api/'
  17. APIVERSION='v1'
  18. URL=BASEURL+APIVERSION+'/'
  19. CONNECTION_RETRY=3
  20. SELL='sale'
  21. BUY='purchase'
  22. logfile='/storage/emulated/0/logs/hashnestapilog.txt'
  23. def __init__(self,username,key,secret):
  24. self.username=username
  25. self.key=key
  26. self.secret=secret
  27. def printlog(self, nonce,url,r,data):
  28. try:
  29. f = open(self.logfile, 'a')
  30. f.write('['+str(nonce)+',"'+str(url)+'",'+str(r)+',"'+data+'"]'+"\n")
  31. f.close()
  32. except Exception,e:
  33. print "ERROR: can't write log file :" + str(Exception) + '---' + str(e)
  34. def get_nonce(self):
  35. return int(time.time()*100000);
  36. def check_result(self,result):
  37. pass
  38. def perform_private(self,url,req={}):
  39. nonce= self.get_nonce()
  40. message = str(nonce) + self.username + self.key
  41. req['access_key']=self.key
  42. req['nonce']=nonce
  43. req['signature']= hmac.new( self.secret, msg=message, digestmod=hashlib.sha256).hexdigest()
  44. data = urlencode(req)
  45. headers = {'User-Agent':'ApeGoxApi'}
  46. url=self.URL+url
  47. for i in range(0,self.CONNECTION_RETRY):
  48. try:
  49. req = urllib2.Request(url, data, headers)
  50. res = urllib2.urlopen(req,data)
  51. r=res.read()
  52. self.printlog(nonce,url,r,data)
  53. return json.loads(r)
  54. except Exception,e:
  55. self.printlog(nonce,url,str(e),data)
  56. print 'ERROR:' +str(i+1)+'/'+str(self.CONNECTION_RETRY)+':' + str(Exception) + '---' + str(e)+"\n"+url+"\n"+data
  57. time.sleep(1)
  58. def get_account_info(self):
  59. return self.perform_private('account');
  60. def get_account_balance(self):
  61. return self.perform_private('currency_accounts')
  62. def get_account_hashrate(self):
  63. return self.perform_private('hash_accounts')
  64. def get_orders(self,cmi):
  65. param={'currency_market_id':cmi}
  66. return self.perform_private('orders/active',param)
  67. def get_history(self,cmi,page=None,page_amount=None):
  68. param={'currency_market_id':cmi}
  69. if page is not None:param['page']=page
  70. if page_amount is not None:param['page_per_amount']=page_amount
  71. return self.perform_private('orders/history',param)
  72. def create_order(self,cmi,amount,ppc,category):
  73. param={'currency_market_id':cmi}
  74. param['amount']=amount
  75. param['ppc']=ppc
  76. param['category']=category
  77. return self.perform_private('orders',param)
  78. def delete_order(self,order_id):
  79. param={'order_id':order_id}
  80. return self.perform_private('orders/revoke',param)
  81. def delete_all_orders(self,cmi):
  82. param={'currency_market_id':cmi}
  83. return self.perform_private('orders/quick_revoke',param)
  84. def get_opened_markets(self):
  85. return self.perform_private('currency_markets')
  86. def get_currency_orders(self,cmi,category='sale'):
  87. param={'currency_market_id':cmi}
  88. param['category']=category
  89. return self.perform_private('currency_markets/orders',param)
  90. def get_currency_trades(self,cmi):
  91. param={'currency_market_id':cmi}
  92. return self.perform_private('currency_markets/order_history',param)

comments powered by Disqus