
    bf/                         d Z ddlZddlZ	 ddlmZ n#  Y nxY wddlmZ ddlmZm	Z	m
Z
 ddlmZmZmZ  G d de          Z G d	 d
e          ZdS )z
Handlers for the Streaming API:
https://github.com/mastodon/documentation/blob/master/content/en/methods/timelines/streaming.md
    N)	signature)Mastodon)MastodonMalformedEventErrorMastodonNetworkErrorMastodonReadTimeout)ChunkedEncodingErrorReadTimeoutConnectionErrorc                   t    e 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dZd Zd Zd Zd ZdS )StreamListenera  Callbacks for the streaming API. Create a subclass, override the on_xxx
    methods for the kinds of events you're interested in, then pass an instance
    of your subclass to Mastodon.user_stream(), Mastodon.public_stream(), or
    Mastodon.hashtag_stream().c                     dS )z^A new status has appeared. `status` is the parsed `status dict`
        describing the status.N selfstatuss     `/home/feoh/.local/pipx/venvs/mastodon-archive/lib/python3.11/site-packages/mastodon/streaming.py	on_updatezStreamListener.on_update   	     	    c                     dS )zAA status has been deleted. `status_id` is the status' integer ID.Nr   )r   	status_ids     r   	on_deletezStreamListener.on_delete       r   c                     dS )ziA new notification. `notification` is the parsed `notification dict`
        describing the notification.Nr   r   notifications     r   on_notificationzStreamListener.on_notification!   r   r   c                     dS )zgFilters have changed. Does not contain a payload, you will have to
           refetch filters yourself.Nr   r   s    r   on_filters_changedz!StreamListener.on_filters_changed&   r   r   c                     dS )zA direct message (in the direct stream) has been received. `conversation`
        is the parsed `conversation dict` dictionary describing the conversationNr   r   conversations     r   on_conversationzStreamListener.on_conversation+   r   r   c                     dS )zA new announcement has been published. `announcement` is the parsed
        `announcement dict` describing the newly posted announcement.Nr   r   annoucements     r   on_announcementzStreamListener.on_announcement0   r   r   c                     dS )zASomeone has reacted to an announcement. TODO: what is payload lolNr   r   TODOs     r   on_announcement_reactionz'StreamListener.on_announcement_reaction5   r   r   c                     dS )zaAn announcement has been deleted. `annoucement_id` is the id of the
        deleted announcement.Nr   r   annoucement_ids     r   on_announcement_deletez%StreamListener.on_announcement_delete9   r   r   c                     dS )zgA status has been edited. 'status' is the parsed JSON dictionary
        describing the updated status.Nr   r   s     r   on_status_updatezStreamListener.on_status_update>   r   r   c                     dS )z9An encrypted message has been received. Currently unused.Nr   r   unclears     r   on_encrypted_messagez#StreamListener.on_encrypted_messageC   r   r   c                     dS )a  There was a connection error, read timeout or other error fatal to
        the streaming connection. The exception object about to be raised
        is passed to this function for reference.

        Note that the exception will be raised properly once you return from this
        function, so if you are using this handler to reconnect, either never
        return or start a thread and then catch and ignore the exception.
        Nr   )r   errs     r   on_abortzStreamListener.on_abortG   s	     	r   Nc                     dS )zAn unknown mastodon API event has been received. The name contains the event-name and unknown_event
        contains the content of the unknown event.
        Nr   r   nameunknown_events      r   on_unknown_eventzStreamListener.on_unknown_eventR   	     	r   c                     dS )zThe server has sent us a keep-alive message. This callback may be
        useful to carry out periodic housekeeping tasks, or just to confirm
        that the connection is still open.Nr   r   s    r   handle_heartbeatzStreamListener.handle_heartbeatX   r?   r   c                    i }t                      }	 |                    d          D ]}|r|D ]}t          |g          }|dk    r	 |                    d          }nP# t          $ rC}t	          d          }|                     |           t          j        ||           Y d}~nd}~ww xY w|dk    r|                     |           i }n| 	                    ||          }t                      }|
                    |           ،dS # t          $ rD}t          d          }|                     |           t          j        ||           Y d}~dS d}~wt          $ rE}t          d	          f}|                     |           t          j        ||           Y d}~dS d}~wt          $ rE}t          d
          f}|                     |           t          j        ||           Y d}~dS d}~ww xY w)a  
        Handles a stream of events from the Mastodon server. When each event
        is received, the corresponding .on_[name]() method is called.

        When the Mastodon API changes, the on_unknown_event(name, content)
        function is called.
        The default behavior is to throw an error. Define a callback handler
        to intercept unknown events if needed (and avoid errors)

        response; a requests response object with the open stream for reading.
           )
chunk_size   
zutf-8zMalformed UTF-8N zServer ceased communication.z$Timed out while reading from server.z"Requests reports connection error.)	bytearrayiter_contentdecodeUnicodeDecodeErrorr   r9   six
raise_from	_dispatch_parse_lineextendr   r   r	   r   r
   )	r   responseeventline_bufferchunk
chunk_partliner8   	exceptions	            r   handle_streamzStreamListener.handle_stream^   s    kk.	!..!.<< ; ; ;&+ ; ;
%.
|%<%<
%..	"'2'9'9''B'B#5 " " ",G$5-7 -7	 $i 8 8 8 #$-$'!" !" !" !" !" !" !" !"	"  $rzz $u 5 5 5(*(,(8(8u(E(E*3++KK'..z::::-; ;. $ 	 	 	,-KLLIMM)$$$N          	 	 	+68 8 9IMM)$$$N          	 	 	,46 6 7IMM)$$$N        		sZ   4D	 AD	 
B*'9B% D	 %B**AD	 	
G19EG1:FG1,:G,,G1c                     |                     d          r|                                  nc	 |                    dd          \  }}n+#  t          d          }|                     |           |xY w||v r||xx         d|z   z  cc<   n|||<   |S )N:z: rC   zMalformed event.
)
startswithrA   splitr   r9   )r   rU   rQ   keyvaluerV   s         r   rN   zStreamListener._parse_line   s    ??3 	#!!#### !ZZa00
UU 78JKK	i((( e||c


dUl*



"c
s   A (A.c                    	 |d         }|d         }	 t          j        |d                   }n	#  d }Y nxY wt          j        |t          j                  }n# t          $ rP}t          d|j        d         |          }|                     |           t          j	        ||           Y d }~nTd }~wt          $ rD}t          d|          }|                     |           t          j	        ||           Y d }~nd }~ww xY wd|                    d	d
          z   }t          | || j                  }		 t          t          |	          j                  }
n%#  |	j        j        d |	j        j                 }
Y nxY wd|
v r(|	| j        k    r |	||           d S  |	|||           d S |	| j        k    r$|	| j        k    r |	             d S  |	|           d S  |	||           d S )NrQ   datastream)object_hookzMissing fieldr   zBad JSONon_._
for_stream)jsonloadsr   _Mastodon__json_hooksKeyErrorr   argsr9   rK   rL   
ValueErrorreplacegetattrr>   listr   
parameters__code__co_varnamesco_argcountr    )r   rQ   r<   r`   rf   payloadr8   rV   handler_namehandlerhandler_argss              r   rM   zStreamListener._dispatch   sl   	>D=D"!Zh88

"!


j83QRRRGG 	 	 	3!e5 5IMM)$$$N         	 	 	 4JEEIMM)$$$N       	 t||C555 $d.CDD	W	' 2 2 =>>LL	W"+78U9I9U8UVLLL <''$///,,,,,gz22222$///d555GIIIIIGG$$$$$g&&&&&sB   A . A 4%A 
C>"AB--C>::C99C>1!E  E5N)__name__
__module____qualname____doc__r   r   r   r    r$   r(   r,   r0   r2   r6   r9   r>   rA   rW   rN   rM   r   r   r   r   r      s       " "
  
    
  
  
  
    
  
  	 	 	     < < <|  $4' 4' 4' 4' 4'r   r   c                        e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd ZddZ xZS )CallbackStreamListenera3  
    Simple callback stream handler class.
    Can optionally additionally send local update events to a separate handler.
    Define an unknown_event_handler for new Mastodon API events. This handler is
    *not* guaranteed to receive these events forever, and should only be used
    for diagnostics.
    Nc                     t          t          |                                            || _        || _        || _        || _        || _        || _        || _	        || _
        |	| _        |
| _        || _        || _        d S rx   )superr~   __init__update_handlerlocal_update_handlerdelete_handlernotification_handlerfilters_changed_handlerconversation_handlerunknown_event_handlerstatus_update_handlerannouncement_handlerannouncement_reaction_handlerannouncement_delete_handlerencryted_message_handler)r   r   r   r   r   r   r   r   r   r   r   r   r   	__class__s                r   r   zCallbackStreamListener.__init__   s     	$d++44666,$8!,$8!'>$$8!%:"%:"$8!-J*+F((@%%%r   c                    | j         |                      |           	 | j        'd|d         d         vr|                     |           d S d S d S # t          $ r.}t          j        t          d|          |           Y d }~d S d }~ww xY w)N@accountacctzreceived bad update)r   r   	ExceptionrK   rL   r   )r   r   r8   s      r   r   z CallbackStreamListener.on_update
  s    *'''	(4SF9DUV\D]=]=]))&11111 54=]=] 	 	 	N+,A6JJ        	s   ,A 
B#BBc                 B    | j         |                      |           d S d S rx   )r   )r   
deleted_ids     r   r   z CallbackStreamListener.on_delete  s-    *
+++++ +*r   c                 B    | j         |                      |           d S d S rx   )r   r   s     r   r   z&CallbackStreamListener.on_notification  -    $0%%l33333 10r   c                 @    | j         |                                   d S d S rx   )r   r   s    r   r    z)CallbackStreamListener.on_filters_changed  s+    '3((***** 43r   c                 B    | j         |                      |           d S d S rx   )r   r"   s     r   r$   z&CallbackStreamListener.on_conversation#  r   r   c                 B    | j         |                      |           d S d S rx   )r   r&   s     r   r(   z&CallbackStreamListener.on_announcement'  s-    $0%%k22222 10r   c                 B    | j         |                      |           d S d S rx   )r   r*   s     r   r,   z/CallbackStreamListener.on_announcement_reaction+  s-    -9..t44444 :9r   c                 B    | j         |                      |           d S d S rx   )r   r.   s     r   r0   z-CallbackStreamListener.on_announcement_delete/  s-    +7,,^<<<<< 87r   c                 B    | j         |                      |           d S d S rx   )r   r   s     r   r2   z'CallbackStreamListener.on_status_update3  s-    %1&&v..... 21r   c                 B    | j         |                      |           d S d S rx   )r   r4   s     r   r6   z+CallbackStreamListener.on_encrypted_message7  s-    (4))'22222 54r   c                 D    | j         |                      ||           d S d S rx   )r   r;   s      r   r>   z'CallbackStreamListener.on_unknown_event;  s/    %1&&t];;;;; 21r   )NNNNNNNNNNNNrx   )ry   rz   r{   r|   r   r   r   r   r    r$   r(   r,   r0   r2   r6   r>   __classcell__)r   s   @r   r~   r~      s         !%&* $&*&*'+'+)-&*/3-1*.A A A A A A8  , , ,4 4 4+ + +4 4 43 3 35 5 5= = =/ / /3 3 3< < < < < < < <r   r~   )r|   rg   rK   inspectr   mastodonr   mastodon.Mastodonr   r   r   requests.exceptionsr   r	   r
   objectr   r~   r   r   r   <module>r      s
   
  



	!!!!!!!	D       d d d d d d d d d d R R R R R R R R R RP' P' P' P' P'V P' P' P'fX< X< X< X< X<^ X< X< X< X< X<s    