1414from threading import Thread
1515from websocket import create_connection , WebSocketConnectionClosedException
1616from pymongo import MongoClient
17+ from gdax .gdax_auth import get_auth_headers
1718
18- class WebsocketClient (object ):
19- def __init__ (self , url = "wss://ws-feed.gdax.com" , products = None , message_type = "subscribe" , mongo_collection = None ,
20- should_print = True , auth = False , api_key = "" , api_secret = "" , api_passphrase = "" , channels = None ):
2119
20+ class WebsocketClient (object ):
21+ def __init__ (self , url = "wss://ws-feed.gdax.com" , products = None , message_type = "subscribe" , mongo_collection = None ,
22+ should_print = True , auth = False , api_key = "" , api_secret = "" , api_passphrase = "" , channels = None ):
2223 self .url = url
2324 self .products = products
2425 self .channels = channels
@@ -55,22 +56,14 @@ def _connect(self):
5556 self .url = self .url [:- 1 ]
5657
5758 if self .channels is None :
58- sub_params = {'type' : 'subscribe' , 'product_ids' : self .products }
59+ sub_params = {'type' : 'subscribe' , 'product_ids' : self .products }
5960 else :
60- sub_params = {'type' : 'subscribe' , 'product_ids' : self .products , 'channels' : self .channels }
61-
61+ sub_params = {'type' : 'subscribe' , 'product_ids' : self .products , 'channels' : self .channels }
6262
6363 if self .auth :
6464 timestamp = str (time .time ())
6565 message = timestamp + 'GET' + '/users/self'
66- message = message .encode ('ascii' )
67- hmac_key = base64 .b64decode (self .api_secret )
68- signature = hmac .new (hmac_key , message , hashlib .sha256 )
69- signature_b64 = base64 .b64encode (signature .digest ())
70- sub_params ['signature' ] = signature_b64
71- sub_params ['key' ] = self .api_key
72- sub_params ['passphrase' ] = self .api_passphrase
73- sub_params ['timestamp' ] = timestamp
66+ sub_params .update (get_auth_headers (timestamp , message , self .api_key , self .api_secret , self .api_passphrase ))
7467
7568 self .ws = create_connection (self .url )
7669 self .ws .send (json .dumps (sub_params ))
@@ -81,7 +74,6 @@ def _connect(self):
8174 sub_params = {"type" : "heartbeat" , "on" : False }
8275 self .ws .send (json .dumps (sub_params ))
8376
84-
8577 def _listen (self ):
8678 while not self .stop :
8779 try :
@@ -123,19 +115,21 @@ def on_close(self):
123115 def on_message (self , msg ):
124116 if self .should_print :
125117 print (msg )
126- if self .mongo_collection : # dump JSON to given mongo collection
118+ if self .mongo_collection : # dump JSON to given mongo collection
127119 self .mongo_collection .insert_one (msg )
128120
129121 def on_error (self , e , data = None ):
130122 self .error = e
131123 self .stop
132124 print ('{} - data: {}' .format (e , data ))
133125
126+
134127if __name__ == "__main__" :
135128 import sys
136129 import gdax
137130 import time
138131
132+
139133 class MyWebsocketClient (gdax .WebsocketClient ):
140134 def on_open (self ):
141135 self .url = "wss://ws-feed.gdax.com/"
@@ -150,6 +144,7 @@ def on_message(self, msg):
150144 def on_close (self ):
151145 print ("-- Goodbye! --" )
152146
147+
153148 wsClient = MyWebsocketClient ()
154149 wsClient .start ()
155150 print (wsClient .url , wsClient .products )
0 commit comments