
    g                     ^    d dl Z d dlZd dlmZ d dlZd dlmZ  G d dej                        Zy)    N)Callable)logc                   N     e Zd ZdZdZddededef fdZd Zd Z	d	 Z
d
 Z xZS )AlertListenera   Creates a websocket connection to the PlexServer to optionally receive alert notifications.
        These often include messages from Plex about media scans as well as updates to currently running
        Transcode Sessions. This class implements threading.Thread, therefore to start monitoring
        alerts you must call .start() on the object once it's created. When calling
        `PlexServer.startAlertListener()`, the thread will be started for you.

        Known `state`-values for timeline entries, with identifier=`com.plexapp.plugins.library`:

            :0: The item was created
            :1: Reporting progress on item processing
            :2: Matching the item
            :3: Downloading the metadata
            :4: Processing downloaded metadata
            :5: The item processed
            :9: The item deleted

        When metadata agent is not set for the library processing ends with state=1.

        Parameters:
            server (:class:`~plexapi.server.PlexServer`): PlexServer this listener is connected to.
            callback (func): Callback function to call on received messages. The callback function
                will be sent a single argument 'data' which will contain a dictionary of data
                received from the server. :samp:`def my_callback(data): ...`
            callbackError (func): Callback function to call on errors. The callback function
                will be sent a single argument 'error' which will contain the Error object.
                :samp:`def my_callback(error): ...`
            ws_socket (socket): Socket to use for the connection. If not specified, a new socket will be created.
    z/:/websockets/notificationscallbackcallbackError	ws_socketc                 ~    t         t        |           d| _        || _        || _        || _        || _        d | _        y )NT)	superr   __init__daemon_server	_callback_callbackError_socket_ws)selfserverr   r   r	   	__class__s        "/opt/Tautulli/lib/plexapi/alert.pyr   zAlertListener.__init__)   s;    mT+-!+     c                    	 dd l }| j                  j                  | j                  d      j                  dd      }t        j                  d|       |j                  || j                  | j                  | j                        | _        | j                  j                          y # t        $ r t        j                  d       Y y w xY w)	Nr   z-Can't use the AlertListener without websocketT)includeTokenhttpwszStarting AlertListener: %s)
on_messageon_errorsocket)	websocketImportErrorr   warningr   urlkeyreplaceinfoWebSocketApp
_onMessage_onErrorr   r   run_forever)r   r   r"   s      r   runzAlertListener.run2   s    	
 lltxxd;CCFDQ-s3))#$//TXTaTajnjvjv)w  	KKGH	s   B& &CCc                 b    t        j                  d       | j                  j                          y)z Stop the AlertListener thread. Once the notifier is stopped, it cannot be directly
            started again. You must call :func:`~plexapi.server.PlexServer.startAlertListener`
            from a PlexServer instance.
        zStopping AlertListener.N)r   r%   r   close)r   s    r   stopzAlertListener.stop@   s    
 	*+r   c                     |d   }	 t        j                  |      d   }t        j                  dg|  | j                  r| j	                  |       yy# t
        $ r }t        j                  d|       Y d}~yd}~ww xY w)z Called when websocket message is received.

            We are assuming the last argument in the tuple is the message.
        NotificationContainerzAlert: %s %s %szAlertListener Msg Error: %sN)jsonloadsr   debugr   	Exceptionerror)r   argsmessagedataerrs        r   r'   zAlertListener._onMessageH   sr    
 r(	:::g&'>?DII'/$/~~t$  	:II3S99	:s   AA 	A=A88A=c                     |d   }	 t        j                  d|       | j                  r| j                  |       yy# t        $ r }t        j                  d|       Y d}~yd}~ww xY w)z~ Called when websocket error is received.

            We are assuming the last argument in the tuple is the message.
        r/   zAlertListener Error: %szAlertListener Error: Error: %sN)r   r5   r   r4   )r   r6   r9   s      r   r(   zAlertListener._onErrorV   s_    
 2h	=II/5""##C( # 	=II6<<	=s   3< 	A%A  A%)NNN)__name__
__module____qualname____doc__r#   r   r   r   r*   r-   r'   r(   __classcell__)r   s   @r   r   r   
   s>    8 (C  ek :=r   r   )	r1   r   typingr   	threadingplexapir   Threadr    r   r   <module>rE      s(        W=I$$ W=r   