hashnest api helper


SUBMITTED BY: Guest

DATE: March 9, 2015, 7:16 a.m.

FORMAT: Python

SIZE: 3.1 kB

HITS: 1490

  1. #DONATION 16RdQZ86NUWQbE6rM86JbxnnaCDcAKqa9
  2. ####HASHNET API HELPER####
  3. from urllib import urlencode
  4. import urllib2
  5. import time
  6. from datetime import datetime
  7. import hashlib
  8. import hmac
  9. import base64
  10. import json
  11. USERNAME=''
  12. ACCESS_KEY=''
  13. SECRET_KEY=''
  14. BASEURL='https://www.hashnest.com/api/'
  15. APIVERSION='v1'
  16. URL=BASEURL+APIVERSION+'/'
  17. def get_nonce():
  18. return int(time.time()*100000);
  19. def get_signature(nonce):
  20. global ACCESS_KEY,USERNAME,SECRET_KEY
  21. message = str(nonce) + USERNAME + ACCESS_KEY
  22. return hmac.new(SECRET_KEY, msg=message, digestmod=hashlib.sha256).hexdigest()
  23. def check_result(result):
  24. pass
  25. def get_verification():
  26. out={}
  27. global ACCESS_KEY
  28. out['access_key']=ACCESS_KEY
  29. out['nonce']=get_nonce()
  30. out['signature']=get_signature(out['nonce'])
  31. return out
  32. def build_query(req={}):
  33. req.update(get_verification())
  34. post_data = urlencode(req)
  35. headers = {}
  36. headers['User-Agent'] = 'ApeGoxApi'
  37. return (post_data, headers)
  38. def print_log(req='',param={}):
  39. print('req:'+str(datetime.now())+' : '+req+' : params : '+str(param))
  40. def perform_query(url,data,headers={}):
  41. global URL
  42. url=URL+url
  43. count=0
  44. while True:
  45. if count==3: exit(0)
  46. count+=1
  47. try:
  48. print_log(url,data)
  49. req = urllib2.Request(url, data, headers)
  50. res = urllib2.urlopen(req,data)
  51. return json.load(res)
  52. except Exception,e:
  53. print 'ERROR:' + str(Exception) + '---' + str(e)
  54. time.sleep(10)
  55. def perform_private(url,req={}):
  56. data, headers = build_query(req)
  57. return perform_query(url,data,headers)
  58. def get_account_info():
  59. return perform_private('account');
  60. def get_account_balance():
  61. return perform_private('currency_accounts')
  62. def get_account_hashrate():
  63. return perform_private('hash_accounts')
  64. def get_orders(cmi):
  65. param={'currency_market_id':cmi}
  66. return perform_private('orders/active',param)
  67. def get_history(cmi,page=None,page_amount=None):
  68. param={'currency_market_id':cmi}
  69. param['page']=page
  70. param['page_per_amount']=page_amount
  71. return perform_private('orders/history',param)
  72. def create_order(cmi,amount,ppc,category):
  73. param={'currency_market_id':cmi}
  74. param['amount']=amount
  75. param['ppc']=ppc
  76. param['category']=category
  77. return perform_private('orders')
  78. def cancel_order(order_id):
  79. param={'order_id':order_id}
  80. return perform_private('orders/revoke')
  81. def cancell_all_orders(cmi):
  82. param={'currency_market_id':cmi}
  83. return perform_private('orders/quick_revoke',param)
  84. def get_opened_markets():
  85. return perform_private('currency_markets')
  86. def get_currency_orders(cmi,category):
  87. param={'currency_market_id':cmi}
  88. param['category']=category
  89. return perform_private('currency_markets/order_history',param)

comments powered by Disqus