
    6iW+                     4   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dl mZ d dl	m	Z	 dZ
	 d dl
Z
	 d dlmZmZ dZdZ	 d dlmZmZ eZeZd dlZd dlmZmZmZmZ d d	lmZ d d
lmZ  G d d      Zy# e$ r Y Hw xY w# e$ r d dlmZmZ Y Rw xY w# e$ r Y Lw xY w)    N)gaierror)Optional)sleep)random)ConnectionClosedErrorConnectionClosedOK)Proxyproxy_connect)BinanceWebsocketClosedBinanceWebsocketUnableToConnectBinanceWebsocketQueueOverflowReadLoopClosed)get_loop)WSListenerStatec                       e Zd ZdZdZdZdZdZ	 	 	 	 	 	 d dede	e   ded	e
d
e	e   defdZdefdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdedefdZd Zd Zy)!ReconnectingWebsocket   <   皙?
   Nurlpathprefix	is_binaryhttps_proxymax_queue_sizec                 p   t               | _        t        j                  t              | _        || _        || _        || _        || _	        d| _
        || _        d | _        d | _        d | _        t        j                   | _        t%        j&                         | _        d | _        || _        || _        || _        y )Nr   )r   _looplogging	getLogger__name___log_path_url
_exit_coro_prefix_reconnects
_is_binary_conn_socketwsr   INITIALISINGws_stateasyncioQueue_queue_handle_read_loop_https_proxy
_ws_kwargsr   )	selfr   r   r   r   	exit_coror   r   kwargss	            _/var/www/app/trading-bot/venv/lib/python3.12/site-packages/binance/ws/reconnecting_websocket.py__init__zReconnectingWebsocket.__init__4   s     Z
%%h/	
	##
8<'44mmo!%' ,    returnc                     t         r$t        j                  |      j                  d      S t        j                  |      S )Nzutf-8)orjsondumpsdecodejsonr4   msgs     r7   
json_dumpsz ReconnectingWebsocket.json_dumpsQ   s-    <<$++G44zz#r9   c                 b    t         rt        j                  |      S t        j                  |      S N)r<   loadsr?   r@   s     r7   
json_loadsz ReconnectingWebsocket.json_loadsV   s"    <<$$zz#r9   c                 B   K   | j                          d {    | S 7 wrD   )connectr4   s    r7   
__aenter__z ReconnectingWebsocket.__aenter__[   s     lln 	s   c                 F   K   | j                  d d d        d {    y 7 wrD   )	__aexit__rI   s    r7   closezReconnectingWebsocket.close_   s     nnT4...s   !!c                 :  K   | j                   j                  d| j                   | j                   | j                          | j
                  r| j                          d {    | j                  r#| j                  | j                         d {    | j                  r"| j                  j                          d {    | j                  r;t        | j                  d      r%| j                  j                  |||       d {    d | _        y 7 7 7 W7 w)NzClosing Websocket protocol)r"   debugr$   r&   r#   r1   _kill_read_loopr%   r+   rM   r)   hasattrrL   )r4   exc_typeexc_valexc_tbs       r7   rL   zReconnectingWebsocket.__aexit__b   s     		,TYYK~djj\RS!!&&(((??//$**---77''--/!!::'$**j9**&&x&AAA )-!AsI   A D"D#.DD-D?D ADDDDDDc                   K   | j                   j                  d       t        j                  | _        | j                          d {    | j                   t        | dd       t        | dd       }| j                  rUt        rt        st        d      t        j                  | j                        }t        |fd|d| j                  | _        n't        j                   |fddi| j                  | _        	 | j                  j#                          d {   | _        t        j(                  | _        d
| _        | j-                          d {    | j.                  s>| j0                  j3                  t4        j6                  | j9                               | _        y y 7 \7 # t$        $ r:}| j                   j'                  d	|        t        j                  | _        |d }~ww xY w7 w)Nz%Establishing new WebSocket connectionr&    r#   zmwebsockets_proxy is not installed, please install it to use a websockets proxy (pip install websockets_proxy)r   )close_timeoutproxyrX   z Failed to connect to websocket: r   )r"   rP   r   RECONNECTINGr-   _before_connectr$   getattrr2   r	   r
   ImportErrorfrom_urlr3   r)   r+   rH   rJ   	Exceptionerror	STREAMINGr'   _after_connectr1   r   call_soon_threadsafer.   create_task
_read_loop)r4   ws_urlrY   es       r7   rH   zReconnectingWebsocket.connectn   s    		?@'44""$$$ yyk'$	267gr8R7ST 	
 ! D  NN4#4#45E&&):>//DJ FQ#QQDJ	 JJ1133DG
 (11!!###%%%)ZZ%D%D##T__%6&D" &9 	%( 4 	IIOO>qcBC+88DMG	 	$sV   AG/F"B4G/<F' F%	F' #/G/G-AG/%F' '	G*05G%%G**G/c                    K   t         j                  | _        | j                  r t	        d       d {    | j                  r | j
                  j                  d       y 7 -w)Nr   zFinished killing read_loop)r   EXITINGr-   r1   r   r"   rP   rI   s    r7   rQ   z%ReconnectingWebsocket._kill_read_loop   sJ     '//$$* $$		45 s   0A"A A"A"c                    K   y wrD    rI   s    r7   r[   z%ReconnectingWebsocket._before_connect   	        c                    K   y wrD   rk   rI   s    r7   rb   z$ReconnectingWebsocket._after_connect   rl   rm   c                 8   | j                   r	 t        j                  |      }	 | j                  |      S # t        t        f$ r$}| j
                  j                  d|         d }~wt        $ r$}| j
                  j                  d|         d }~ww xY w# t        $ r$}| j
                  j                  d|         d }~wt        $ r$}| j
                  j                  d|         d }~wt        $ r$}| j
                  j                  d|         d }~ww xY w)NzFailed to decompress message: z Unexpected decompression error: z)JSON Value Error parsing message: Error: z(JSON Type Error parsing message. Error: z)Unexpected error parsing message. Error: )
r(   gzip
decompress
ValueErrorOSErrorr"   r`   r_   rF   	TypeError)r4   evtrg   s      r7   _handle_messagez%ReconnectingWebsocket._handle_message   s    ??ooc*
	??3'' ( 		"@! FG 		"BA% HI
  	IIOOGuMN 	IIOOFeLM 	IIOOGuMN	sP   5 B BA##B/BB	DB>>D
C))D5DDc                 	  K   	 	 	 | j                   t        j                  k(  r6| j                          d {    | j                   t        j                  k(  r6| j                   t        j                  k(  r7| j
                  j                  d| j                   d| j                           n| j                  j                  t        j                  j                  j                  k(  rt        j                  d       d {    | j                  j                  t        j                  j                  j                  k(  r| j!                          t#        d      | j                   t        j$                  k(  r| j                  sJ t        j&                  | j                  j)                         | j*                         d {   }| j-                  |      }| j
                  j                  d|        |r~| j.                  j1                         | j2                  k  r$| j.                  j5                  |       d {    n3t7        d| j.                  j1                          d| j2                         ud | _'        d| _(        y 7 S7 7 7 Q# t        j8                  $ r, | j
                  j                  d	| j*                   d
       Y Yt        j:                  $ rf}| j
                  j                  d|        | j.                  j5                  d|j<                  j>                   | d       d {  7   Y d }~d }~wt        j@                  tB        tD        tF        t"        f$ r~}| j
                  jI                  |j<                  j>                   d| d       | j.                  j5                  d|j<                  j>                   | d       d {  7   Y d }~pd }~wtJ        t6        tL        f$ r~}| j
                  jI                  d|j<                  j>                   d| d       | j.                  j5                  d|j<                  j>                  | d       d {  7   Y d }~d }~ww xY w# tL        $ rA}| j
                  jI                  d|j<                  j>                   d| d       Y d }~Kd }~ww xY w# d | _'        d| _(        w xY ww)Nz_read_loop z break for r   z"Connection closed. Reconnecting...timeoutzReceived message: zMessage queue size z exceeded maximum no message in  secondsz_read_loop cancelled error r`   )rg   typemz ()zUnknown exception: r   ))r-   r   rZ   _run_reconnectri   r"   rP   r#   r+   staterO   StateCLOSINGr.   r   CLOSED
_reconnectr   ra   wait_forrecvTIMEOUTrv   r0   qsizer   putr   TimeoutErrorCancelledError	__class__r!   IncompleteReadErrorr   r   r   r`   r   r_   r1   r'   )r4   resrg   s      r7   re   z ReconnectingWebsocket._read_loop   s    K	!D--?+G+GG"11333 --?+G+GG }}(?(??		)$**[P "++*;*;*C*CC%mmC000 "++*;*;*B*BB)4@  /*C*CC#www$+$4$4 GGLLNDLL%  #2237		*<SE(BC#{{002T5H5HH&*kkooc&: : :&C&9$++:K:K:M9NN`aeatat`u$v'" !"; R &*D" DO 4 1 !;
 ++ MIIOOnT\\N($KL-- IIOO&A!$EF++//$#$;;#7#7"8 !s+   
 //)&*  IIOOq{{';';&<Bqc$CD++//$#$;;#7#7"8 !s+    41  IIOO&9!++:N:N9OrRSQTTU$VW++//$ ! 4 4 !s+   
   	PIIOO1!++2F2F1Gr!ANOO	P &*D" Ds"  S R  0J J	!J AJ +R  -AJ  JJ R  B=J JA:J ?J 8J 8R  :S 	J J J J <Q=R  Q=#AM9L<:M?R  +Q=/A-O(OO("R  (Q=?A-Q8,Q/-Q82R  8Q==R   	S
	6S?S S

S SS c                   K   | j                          d {    | j                  | j                  k  r| j                  | j                        }| j                  j                  d| j                  | j                  z
   d|        t        j                  |       d {    	 | j                          d {    y | j                  j                  d| j                   d       t        7 7 O7 8# t        $ r
}Y d }~y d }~ww xY ww)Nzwebsocket reconnecting. z reconnects left - waiting zMax reconnections z	 reached:)before_reconnectr'   MAX_RECONNECTS_get_reconnect_waitr"   rP   r.   r   rH   r_   r`   r   )r4   reconnect_waitrg   s      r7   r   z$ReconnectingWebsocket._run_reconnect  s     ##%%%d111!55d6F6FGNIIOO*4+>+>AQAQ+Q*R S)*, --///lln$$ IIOO01D1D0EYOP11 	& 0$ sX   D	C-BD	C/ D	%C3 8C19C3 =1D	/D	1C3 3	D<D	DD	c                 V  K   d }|s\| j                   st        d      	 t        j                  | j                  j                         | j                         d {   }|s\|S 7 	# t        j                  $ r, | j                  j                  d| j                   d       Y Ew xY ww)NzaRead loop has been closed, please reset the websocket connection and listen to the message error.rx   rz   r{   )
r1   r   r.   r   r0   getr   r   r"   rP   )r4   r   s     r7   r   zReconnectingWebsocket.recv  s     ))$w I#,,T[[__->UU  
 V'' I		.h GHIs@   B)<A' A%A'  B)#B)%A' '<B&#B)%B&&B)c                 &  K   | j                   t        j                  k7  rn| j                   t        j                  k7  rPt	        d       d {    | j                   t        j                  k7  r| j                   t        j                  k7  rNy y y y 7 Cw)Nr   )r-   r   ra   ri   r   rI   s    r7   _wait_for_reconnectz)ReconnectingWebsocket._wait_for_reconnect!  so     MM_666!8!88* MM_666!8!88 78 7 s   A	BB>BBattemptsc                 l    d|z  }t        t               t        | j                  |dz
        z  dz         S )N      )roundr   minMAX_RECONNECT_SECONDS)r4   r   expos      r7   r   z)ReconnectingWebsocket._get_reconnect_wait(  s3    ({VXD$>$>q IIAMNNr9   c                    K   | j                   rd | _         | j                  r;t        | j                  d      r%| j                  j                  d d d        d {    | xj                  dz  c_        y 7 w)NrO   r   )r+   r)   rR   rL   r'   rI   s    r7   r   z&ReconnectingWebsocket.before_reconnect,  sZ     77DG::'$**j9**&&tT4888A 9s   AA5A3A5c                 .    t         j                  | _        y rD   )r   rZ   r-   rI   s    r7   r   z ReconnectingWebsocket._reconnect5  s    '44r9   )Nzws/FNNd   )r!   
__module____qualname__r   r   MIN_RECONNECT_WAITr   NO_MESSAGE_RECONNECT_TIMEOUTstrr   boolintr8   rB   rF   rJ   rM   rL   rH   rQ   r[   rb   rv   re   r   r   r   r   r   r   rk   r9   r7   r   r   -   s    NG#% 
 #%)!-- sm- 	-
 - c]- -: 

/
"H6,L!\2$OC OC O5r9   r   ) r.   rp   r?   r   socketr   typingr   r   r   r<   r]   websockets.exceptionsr   r   
websocketsr	   r
   websockets_proxyw_Proxyw_proxy_connectr+   binance.exceptionsr   r   r   r   binance.helpersr   binance.ws.constantsr   r   rk   r9   r7   <module>r      s            
	EO
 		SE#M   % 0I5 I5?  		
  EDDE  		s3   A1 A< B 1A98A9<BBBB