HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/tweepy/__pycache__/streaming.cpython-310.pyc
o

+weRc�@s�ddlmZddlZddlZddlmZddlmZddlZddl	Z	ddl
mZddlm
Z
ddlmZddlZddlZddlZddlmZmZdd	lmZdd
lmZe�e�Zedd�ZGd
d�d�ZGdd�dee�ZGdd�de�Z dS)�)�
namedtupleN)�inf)�python_version)�Thread)�sleep)�
NamedTuple)�
BaseClient�Response)�TweepyException)�Tweet�StreamResponse)�data�includes�errors�matching_rulesc@sxeZdZddeddd�dd�Z		dd	d
�Zdd�Zd
d�Zdd�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�
BaseStreamiFNT)�
chunk_size�daemon�max_retries�proxy�verifycCsb||_||_||_|rd|ini|_||_d|_t��|_d|_	dt
��dtj�dtj��|_
dS)N�httpsFzPython/z
 Requests/z Tweepy/)rrr�proxiesr�running�requests�Session�session�threadr�__version__�tweepy�
user_agent)�selfrrrrr�r"�I/home/arjun/projects/env/lib/python3.10/site-packages/tweepy/streaming.py�__init__#s

���zBaseStream.__init__�c
Cs�d|_d}d}	d}
d}d}}
d}d}|j|jjd<�zJ�z|j�r.||jk�r.z�|jj||||||d||j|jd	�
�~}|jd
kr|d}|
}d}	|�	�|jsW	Wd�Wn�|j
|jd�D]}|rh|�|�n|�
�|jsqnq^|jjr{|�|�n6|�|j�|js�	Wd�Wn�t�d|j�|d
7}|jdvr�||kr�|}t|�|d9}||kr�|}Wd�n1s�wYWnbtjtjtjjtjtjjtjjf�y$}zFt |tj�r�|j!r�dt"|j!d�vs�|�#�|js�WYd}~n4t�dd�$t%�&t'|�|���(��t|	�|	|
7}	|	|k�r|}	WYd}~nd}~ww|j�r.||jks(Wnt)�yG}z|�*|�WYd}~nd}~wwW|j�+�d|_|�,�dSW|j�+�d|_|�,�dS|j�+�d|_|�,�w)NTrg�?��i@�<z
User-Agent)�params�headersr
�timeout�stream�authrr��)rzHTTP error response text: %s�)i�i��z	timed outzConnection error: %s�F)-rr rr*r�requestrr�status_code�
on_connect�
iter_linesr�on_data�
on_keep_alive�raw�closed�	on_closed�on_request_error�log�error�textrr�ConnectionError�Timeout�
exceptions�ChunkedEncodingError�ssl�SSLError�urllib3�ReadTimeoutError�
ProtocolError�
isinstance�args�str�on_connection_error�join�	traceback�format_exception_only�type�rstrip�	Exception�on_exception�close�
on_disconnect)r!�method�urlr-r)r*�bodyr+�error_count�network_error_wait�network_error_wait_step�network_error_wait_max�http_error_wait�http_error_wait_start�http_error_wait_max�http_429_error_wait_start�resp�line�excr"r"r#�_connect4s�
�
�
��
��"�
���
1
���
����Q���
�

�
zBaseStream._connectcOs(t|jd|||jd�|_|j��|jS)Nz
Tweepy Stream)�target�namerI�kwargsr)rrcrr�start)r!rIrfr"r"r#�_threaded_connect�s
�
zBaseStream._threaded_connectcCs
d|_dS)zDisconnect the streamFN)r�r!r"r"r#�
disconnect�s
zBaseStream.disconnectcC�t�d�dS)z�This is called when the stream has been closed by Twitter.

        Parameters
        ----------
        response : requests.Response
            The Response from Twitter
        z#Stream connection closed by TwitterN�r<r=�r!�responser"r"r#r:��zBaseStream.on_closedcCrk)zKThis is called after successfully connecting to the streaming API.
        zStream connectedN�r<�inforir"r"r#r4�szBaseStream.on_connectcCrk)z>This is called when the stream connection errors or times out.z*Stream connection has errored or timed outNrlrir"r"r#rK��zBaseStream.on_connection_errorcCrk)z0This is called when the stream has disconnected.zStream disconnectedNrprir"r"r#rT�rrzBaseStream.on_disconnectcCrk)z�This is called when an unhandled exception occurs.

        Parameters
        ----------
        exception : Exception
            The unhandled exception
        zStream encountered an exceptionN)r<�	exception)r!rsr"r"r#rR�rozBaseStream.on_exceptioncCrk)z4This is called when a keep-alive signal is received.zReceived keep-alive signalN�r<�debugrir"r"r#r7�rrzBaseStream.on_keep_alivecC�t�d|�dS)z�This is called when a non-200 HTTP status code is encountered.

        Parameters
        ----------
        status_code : int
            The HTTP status code encountered
        z!Stream encountered HTTP error: %dNrl)r!r3r"r"r#r;��zBaseStream.on_request_error)NNNNr%)�__name__�
__module__�__qualname__rr$rcrhrjr:r4rKrTrRr7r;r"r"r"r#r!s �
�l

rcs�eZdZdZedd�dd�Z�fdd�Zd"�fd	d
�	Zdd�Zd
d�Z	dd�dd�Z
dd�Zdd�dd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zd d!�Z�ZS)#�StreamingClientu�Filter and sample realtime Tweets with Twitter API v2

    .. versionadded:: 4.6

    Parameters
    ----------
    bearer_token : str
        Twitter API Bearer Token
    return_type : type[dict | requests.Response | Response]
        Type to return from requests to the API
    wait_on_rate_limit : bool
        Whether or not to wait before retrying when a rate limit is
        encountered. This applies to requests besides those that connect to a
        stream (see ``max_retries``).
    chunk_size : int
        The default socket.read size. Default to 512, less than half the size
        of a Tweet so that it reads Tweets with the minimal latency of 2 reads
        per Tweet. Values higher than ~1kb will increase latency by waiting for
        more data to arrive but may also increase throughput by doing fewer
        socket read calls.
    daemon : bool
        Whether or not to use a daemon thread when using a thread to run the
        stream
    max_retries : int
        Max number of times to retry connecting the stream
    proxy : str | None
        URL of the proxy to use when connecting to the stream
    verify : bool | str
        Either a boolean, in which case it controls whether to verify the
        server’s TLS certificate, or a string, in which case it must be a path
        to a CA bundle to use.

    Attributes
    ----------
    running : bool
        Whether there's currently a stream running
    session : :class:`requests.Session`
        Requests Session used to connect to the stream
    thread : :class:`threading.Thread` | None
        Thread used to run the stream
    user_agent : str
        User agent used when connecting to the stream
    F��return_type�wait_on_rate_limitcKs(tj||||d�tj|fi|��dS)z�__init__(             bearer_token, *, return_type=Response, wait_on_rate_limit=False,             chunk_size=512, daemon=False, max_retries=inf, proxy=None,             verify=True         )
        r|N)rr$r)r!�bearer_tokenr}r~rfr"r"r#r$s
�zStreamingClient.__init__cs:d|j��|jjd<d|�d�}t�j||fi|��dS)NzBearer �
Authorizationz!https://api.twitter.com/2/tweets/z/stream)rrr*�superrc)r!rU�endpointrfrV��	__class__r"r#rcszStreamingClient._connectNcs�|turUt|t�r4g}|D]$}d|vr$|�t|d|d|dd��q
|�t|d|dd��q
|S|durSd|vrIt|d|d|dd�St|d|dd�SdSt�j||d�S)N�tag�value�id)r�r�r�)r�r�)�	data_type)�
StreamRulerH�list�appendr��
_process_data)r!r
r��rules�ruler�r"r#r�s(

�
���zStreamingClient._process_datacKspdgi}t|t�r|f}|D]}|jdur"|d�|j|jd��q|d�d|ji�q|jdd|d|td�S)	a�add_rules(add, *, dry_run)

        Add rules to filtered stream.

        Parameters
        ----------
        add : list[StreamRule] | StreamRule
            Specifies the operation you want to perform on the rules.
        dry_run : bool
            Set to true to test the syntax of your rule without submitting it.
            This is useful if you want to check the syntax of a rule before
            removing one or more of your existing rules.

        Returns
        -------
        dict | requests.Response | Response

        References
        ----------
        https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/post-tweets-search-stream-rules
        �addN)r�r�r��POST�/2/tweets/search/stream/rules��dry_run�r)�endpoint_parameters�jsonr�)rHr�r�r�r��
_make_request)r!r�r)r�r�r"r"r#�	add_rules.s

�zStreamingClient.add_rulescKs|ddgii}t|tttf�r|f}|D]}t|t�r'|dd�t|j��q|dd�t|��q|jdd|d|td�S)a$delete_rules(ids, *, dry_run)

        Delete rules from filtered stream.

        Parameters
        ----------
        ids : int | str | list[int | str | StreamRule] | StreamRule
            Array of rule IDs, each one representing a rule already active in
            your stream. IDs must be submitted as strings.
        dry_run : bool
            Set to true to test the syntax of your rule without submitting it.
            This is useful if you want to check the syntax of a rule before
            removing one or more of your existing rules.

        Returns
        -------
        dict | requests.Response | Response

        References
        ----------
        https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/post-tweets-search-stream-rules
        �delete�idsr�r�r�r�)rH�intrJr�r�r�r�)r!r�r)r�r�r"r"r#�delete_rulesRs
�zStreamingClient.delete_rules)�threadedcK�L|jrtd��d}d}|j|dd�}|r|j|||d�S|j|||d�dS)a�
filter(             *, backfill_minutes=None, expansions=None, media_fields=None,             place_fields=None, poll_fields=None, tweet_fields=None,             user_fields=None, threaded=False         )

        Streams Tweets in real-time based on a specific set of filter rules.

        If you are using the academic research product track, you can connect
        up to two `redundant connections <filter redundant connections_>`_ to
        maximize your streaming up-time.

        The Tweets returned by this endpoint count towards the Project-level
        `Tweet cap`_.

        Parameters
        ----------
        backfill_minutes : int | None
            By passing this parameter, you can request up to five (5) minutes
            worth of streaming data that you might have missed during a
            disconnection to be delivered to you upon reconnection. The
            backfilled Tweets will automatically flow through the reconnected
            stream, with older Tweets generally being delivered before any
            newly matching Tweets. You must include a whole number between 1
            and 5 as the value to this parameter.

            This feature will deliver duplicate Tweets, meaning that if you
            were disconnected for 90 seconds, and you requested two minutes of
            backfill, you will receive 30 seconds worth of duplicate Tweets.
            Due to this, you should make sure your system is tolerant of
            duplicate data.

            This feature is currently only available to the Academic Research
            product track.
        expansions : list[str] | str
            :ref:`expansions_parameter`
        media_fields : list[str] | str
            :ref:`media_fields_parameter`
        place_fields : list[str] | str
            :ref:`place_fields_parameter`
        poll_fields : list[str] | str
            :ref:`poll_fields_parameter`
        tweet_fields : list[str] | str
            :ref:`tweet_fields_parameter`
        user_fields : list[str] | str
            :ref:`user_fields_parameter`
        threaded : bool
            Whether or not to use a thread to run the stream

        Raises
        ------
        TweepyException
            When the stream is already connected

        Returns
        -------
        threading.Thread | None
            The thread if ``threaded`` is set to ``True``, else ``None``

        References
        ----------
        https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream

        .. _filter redundant connections: https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/recovery-and-redundancy-features
        .. _Tweet cap: https://developer.twitter.com/en/docs/twitter-api/tweet-caps
        �Stream is already connected�GET�search��backfill_minutes�
expansionszmedia.fieldszplace.fieldszpoll.fieldsztweet.fieldszuser.fields�r��r)N�rr
�_process_paramsrhrc�r!r�r)rUr�r"r"r#�filterwsC�zStreamingClient.filtercKs|jdd|dtd�S)a%get_rules(*, ids)

        Return a list of rules currently active on the streaming endpoint,
        either as a list or individually.

        Parameters
        ----------
        ids : list[str] | str
            Comma-separated list of rule IDs. If omitted, all rules are
            returned.

        Returns
        -------
        dict | requests.Response | Response

        References
        ----------
        https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream-rules
        r�r�)r�)r)r�r�)r�r�)r!r)r"r"r#�	get_rules�s�zStreamingClient.get_rulescKr�)a-
sample(             *, backfill_minutes=None, expansions=None, media_fields=None,             place_fields=None, poll_fields=None, tweet_fields=None,             user_fields=None, threaded=False         )

        Streams about 1% of all Tweets in real-time.

        If you are using the academic research product track, you can connect
        up to two `redundant connections <sample redundant connections_>`_ to
        maximize your streaming up-time.

        Parameters
        ----------
        backfill_minutes : int | None
            By passing this parameter, you can request up to five (5) minutes
            worth of streaming data that you might have missed during a
            disconnection to be delivered to you upon reconnection. The
            backfilled Tweets will automatically flow through the reconnected
            stream, with older Tweets generally being delivered before any
            newly matching Tweets. You must include a whole number between 1
            and 5 as the value to this parameter.

            This feature will deliver duplicate Tweets, meaning that if you
            were disconnected for 90 seconds, and you requested two minutes of
            backfill, you will receive 30 seconds worth of duplicate Tweets.
            Due to this, you should make sure your system is tolerant of
            duplicate data.

            This feature is currently only available to the Academic Research
            product track.
        expansions : list[str] | str
            :ref:`expansions_parameter`
        media_fields : list[str] | str
            :ref:`media_fields_parameter`
        place_fields : list[str] | str
            :ref:`place_fields_parameter`
        poll_fields : list[str] | str
            :ref:`poll_fields_parameter`
        tweet_fields : list[str] | str
            :ref:`tweet_fields_parameter`
        user_fields : list[str] | str
            :ref:`user_fields_parameter`
        threaded : bool
            Whether or not to use a thread to run the stream

        Raises
        ------
        TweepyException
            When the stream is already connected

        Returns
        -------
        threading.Thread | None
            The thread if ``threaded`` is set to ``True``, else ``None``

        References
        ----------
        https://developer.twitter.com/en/docs/twitter-api/tweets/volume-streams/api-reference/get-tweets-sample-stream

        .. _sample redundant connections: https://developer.twitter.com/en/docs/twitter-api/tweets/volume-streams/integrate/recovery-and-redundancy-features
        r�r��sampler�r�r�Nr�r�r"r"r#r��s?�zStreamingClient.samplecCs�t�|�}d}i}g}g}d|vrt|d�}|�|�d|vr,|�|d�}|�|�d|vr9|d}|�|�d|vrKdd�|dD�}|�|�|�t	||||��dS)a�This is called when raw data is received from the stream.
        This method handles sending the data to other methods.

        Parameters
        ----------
        raw_data : JSON
            The raw data from the stream

        References
        ----------
        https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/consuming-streaming-data
        Nr
rrrcSs g|]}t|d|dd��qS)r�r�)r�r�)r�)�.0r�r"r"r#�
<listcomp>Ts��z+StreamingClient.on_data.<locals>.<listcomp>)
r��loadsr�on_tweet�_process_includes�on_includes�	on_errors�on_matching_rules�on_responser)r!�raw_datar
�tweetrrrr"r"r#r66s,




�
�zStreamingClient.on_datacC�dS)z�This is called when a Tweet is received.

        Parameters
        ----------
        tweet : Tweet
            The Tweet received
        Nr")r!r�r"r"r#r�^�zStreamingClient.on_tweetcCr�)z�This is called when includes are received.

        Parameters
        ----------
        includes : dict
            The includes received
        Nr")r!rr"r"r#r�hr�zStreamingClient.on_includescCrv)z�This is called when errors are received.

        Parameters
        ----------
        errors : dict
            The errors received
        zReceived errors: %sNrl)r!rr"r"r#r�rrwzStreamingClient.on_errorscCr�)z�This is called when matching rules are received.

        Parameters
        ----------
        matching_rules : list[StreamRule]
            The matching rules received
        Nr")r!rr"r"r#r�|r�z!StreamingClient.on_matching_rulescCrv)z�This is called when a response is received.

        Parameters
        ----------
        response : StreamResponse
            The response received
        zReceived response: %sNrtrmr"r"r#r��rwzStreamingClient.on_response)N)rxryrz�__doc__r	r$rcr�r�r�r�r�r�r6r�r�r�r�r��
__classcell__r"r"r�r#r{�s$,�$%UQ(



r{c@s6eZdZUdZdZeed<dZeed<dZeed<dS)r�a�Rule for filtered stream

    .. versionadded:: 4.6

    Parameters
    ----------
    value : str | None
        The rule text. If you are using a `Standard Project`_ at the Basic
        `access level`_, you can use the basic set of `operators`_, can submit
        up to 25 concurrent rules, and can submit rules up to 512 characters
        long. If you are using an `Academic Research Project`_ at the Basic
        access level, you can use all available operators, can submit up to
        1,000 concurrent rules, and can submit rules up to 1,024 characters
        long.
    tag : str | None
        The tag label. This is a free-form text you can use to identify the
        rules that matched a specific Tweet in the streaming response. Tags can
        be the same across rules.
    id : str | None
        Unique identifier of this rule. This is returned as a string.


    .. _Standard Project: https://developer.twitter.com/en/docs/projects
    .. _access level: https://developer.twitter.com/en/products/twitter-api/early-access/guide#na_1
    .. _operators: https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/build-a-query
    .. _Academic Research Project: https://developer.twitter.com/en/docs/projects
    Nr�r�r�)	rxryrzr�r�rJ�__annotations__r�r�r"r"r"r#r��s

r�)!�collectionsrr��logging�mathr�platformrrCrM�	threadingr�timer�typingrrrEr�
tweepy.clientrr	�
tweepy.errorsr
�tweepy.tweetr�	getLoggerrxr<rrr{r�r"r"r"r#�<module>s6
�::