python websocketmtgoxclient


SUBMITTED BY: Guest

DATE: Jan. 28, 2013, 4:40 a.m.

FORMAT: Text only

SIZE: 3.3 kB

HITS: 2004

  1. #please donate: 1DQNJWxVsbiVwTbxVwHXqnN5gfjs8VK2Kk
  2. import threading
  3. import websocket
  4. import cjson,json
  5. import time,binascii
  6. import md5,base64,hmac,hashlib
  7. CHANNELS = {
  8. "USD":{
  9. "trade":"dbf1dee9-4f2e-4a08-8cb7-748919a71b21",
  10. "ticker":"d5f06780-30a8-4a48-a2f8-7ed181b4a13f",
  11. "depth":"24e67e0d-1cad-4cc0-9e7a-f8523ef460fe",
  12. }
  13. }
  14. def serialize(obj):
  15. return cjson.encode(obj)
  16. def deserialize(msg):
  17. return cjson.decode(msg)
  18. class MtGoxAPI(threading.Thread):
  19. def __init__(self, apikey, secret):
  20. self.counter = 0
  21. self.apikey = apikey
  22. self.secret = secret
  23. threading.Thread.__init__(self)
  24. def run(self):
  25. self.open_socket("ws://websocket.mtgox.com/mtgox")
  26. def nonce(self):
  27. self.counter += 1
  28. return ( int( time.time() ) * 1000 ) + self.counter
  29. def encode_and_sign(self,data):
  30. binary = binascii.unhexlify( self.apikey.replace("-","") )
  31. signed = hmac.new( base64.b64decode(self.secret), data, hashlib.sha512 ).digest()
  32. encoded = base64.b64encode( "%s%s%s" % (binary, sign, data) )
  33. return encoded
  34. def subscribe(self,channel):
  35. output = serialize({"op":"subscribe",
  36. "channel":channel,
  37. })
  38. def unsubscribe(self,channel):
  39. output = serialize({"op":"unsubscribe",
  40. "channel":channel,
  41. })
  42. def request(self,apicall,cry="USD",params=list(),item="BTC"):
  43. nonce = self.nonce()
  44. id = md5.new( str(nonce) ).hexdigest()
  45. query = {"id":id,
  46. "call":apicall,
  47. "nonce":nonce,
  48. "currency":cry,
  49. "parameters":params,
  50. "item":item
  51. }
  52. output = serialize({"op":"call",
  53. "id":id,
  54. "call":self.encode_and_sign(serialize(query)),
  55. "context":"mtgox.com"
  56. })
  57. self.ws.send(output)
  58. return id
  59. def handle_result(self,data):
  60. print(data)
  61. def handle_depth(self,data):
  62. print(data)
  63. def handle_ticker(self,data):
  64. print(data)
  65. def on_message(self,ws,message):
  66. data = deserialize(message)
  67. op = data.get("op")
  68. if op == "private":
  69. if private == "depth": self.handle_depth(data)
  70. elif private == "trade": self.trades.append(data)
  71. elif private == "ticker": self.handle_ticker(data)
  72. elif private == "result": self.handle_result(data)
  73. def on_open(self,ws):
  74. print "### Connection opened ###"
  75. def on_close(self,ws):
  76. print "### Connection closed ###"
  77. def on_error(self,ws, error):
  78. print error
  79. def open_socket(self,url):
  80. websocket.enableTrace(False)
  81. self.ws = websocket.WebSocketApp(url,
  82. on_message = self.on_message,
  83. on_error = self.on_error,
  84. on_close = self.on_close)
  85. self.ws.on_open = self.on_open
  86. self.ws.run_forever()
  87. if __name__ == "__main__":
  88. apikey = u""
  89. secret = u""
  90. main = MtGoxAPI(apikey,secret).start()

comments powered by Disqus