1- from threading import Timer
21import requests
32import time
43import hmac
54import hashlib
65import json
76import websocket
87import threading
9- import os
10- import signal
11- import sys
128import datetime
139
1410debugging = False
@@ -369,9 +365,9 @@ def subscribe(self):
369365 ws = websocket .WebSocketApp (self .wsUrl ,
370366 on_message = self .on_message ,
371367 on_error = self .on_error ,
372- on_close = self .on_close )
368+ on_close = self .on_close ,
369+ on_open = self .on_open )
373370 self .ws = ws
374- ws .on_open = self .on_open
375371
376372 self .receiveThread = receiveThread (ws , self )
377373 self .receiveThread .daemon = True
@@ -394,27 +390,27 @@ def waitForSocket(self, ws, message, private):
394390 self .waitForSocket (ws , message , private )
395391
396392 def doSend (self , ws , message , private = False ):
397- if ( private and self .APIKEY == '' ) :
393+ if private and self .APIKEY == '' :
398394 errorToConsole ('You did not set the API key, but requested a private function.' )
399395 return
400396 self .waitForSocket (ws , message , private )
401397 ws .send (message )
402398 debugToConsole ('SENT: ' + message )
403399
404- def on_message (ws , msg ):
400+ def on_message (self , ws , msg ):
405401 debugToConsole ('RECEIVED: ' + msg )
406402 msg = json .loads (msg )
407- callbacks = ws .callbacks
403+ callbacks = self .callbacks
408404
409- if ( 'error' in msg ) :
410- if ( msg ['errorCode' ] == 105 ) :
405+ if 'error' in msg :
406+ if msg ['errorCode' ] == 105 :
411407 ws .bitvavo .updateRateLimit (msg )
412- if ( 'error' in callbacks ) :
408+ if 'error' in callbacks :
413409 callbacks ['error' ](msg )
414410 else :
415411 errorToConsole (json .dumps (msg , indent = 2 ))
416412
417- if ( 'action' in msg ) :
413+ if 'action' in msg :
418414 if (msg ['action' ] == 'getTime' ):
419415 callbacks ['time' ](msg ['response' ])
420416 elif (msg ['action' ] == 'getMarkets' ):
@@ -499,13 +495,13 @@ def on_message(ws, msg):
499495 if ('subscriptionTrades' in callbacks ):
500496 callbacks ['subscriptionTrades' ][market ](msg )
501497
502- def on_error (ws , error ):
503- if ( 'error' in callbacks ) :
504- ws .callbacks ['error' ](error )
498+ def on_error (self , ws , error ):
499+ if 'error' in self . callbacks :
500+ self .callbacks ['error' ](error )
505501 else :
506502 errorToConsole (error )
507503
508- def on_close (self ):
504+ def on_close (self , ws ):
509505 self .receiveThread .exit ()
510506 debugToConsole ('Closed Websocket.' )
511507
@@ -533,11 +529,11 @@ def checkReconnect(self):
533529 for market in self .callbacks ['subscriptionBookUser' ]:
534530 self .subscriptionBook (market , self .callbacks ['subscriptionBookUser' ][market ])
535531
536- def on_open (self ):
532+ def on_open (self , ws ):
537533 now = int (time .time ()* 1000 )
538534 self .open = True
539535 self .reconnectTimer = 0.5
540- if ( self .APIKEY != '' ) :
536+ if self .APIKEY != '' :
541537 self .doSend (self .ws , json .dumps ({ 'window' :str (self .ACCESSWINDOW ), 'action' : 'authenticate' , 'key' : self .APIKEY , 'signature' : createSignature (now , 'GET' , '/websocket' , {}, self .APISECRET ), 'timestamp' : now }))
542538 if self .reconnect :
543539 debugToConsole ("we started reconnecting " + str (self .checkReconnect ))
0 commit comments