o
    i/                     @   sN   d Z ddlZddlZddlmZ dddZG dd dZdd	d
Zdd ZdS )z8
Authentication utilities to accompany `twitterclient`.
    N)TwythonFc                 C   s   t  j| ||dS )z1
    Convenience function for authentication
    
creds_filesubdirverbose)Authenticate
load_credsr    r	   L/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nltk/twitter/util.pycredsfromfile   s   r   c                   @   s,   e Zd ZdZdd Zd
ddZddd	ZdS )r   z2
    Methods for authenticating with Twitter.
    c                 C   sN   d| _ d | _i | _ztjd | _| j| _W d S  ty&   d | _d | _Y d S w )Nzcredentials.txtTWITTER)r   creds_fullpathoauthosenvirontwitter_dircreds_subdirKeyErrorselfr	   r	   r
   __init__!   s   zAuthenticate.__init__NFc           	      C   s   |dur|| _ |du r| jdu rd}t|n|| _tjtj| j| j | _tj| js7t	d| j t
| j,}|rGtd| j  |D ]}d|v r`|dd\}}| | j| < qIW d   n1 skw   Y  | j|d | jS )a  
        Read OAuth credentials from a text file.

        File format for OAuth 1::

           app_key=YOUR_APP_KEY
           app_secret=YOUR_APP_SECRET
           oauth_token=OAUTH_TOKEN
           oauth_token_secret=OAUTH_TOKEN_SECRET


        File format for OAuth 2::

           app_key=YOUR_APP_KEY
           app_secret=YOUR_APP_SECRET
           access_token=ACCESS_TOKEN

        :param str file_name: File containing credentials. ``None`` (default) reads
            data from `TWITTER/'credentials.txt'`
        NzQSupply a value to the 'subdir' parameter or set the TWITTER environment variable.zCannot find file zReading credentials file =   )r   )r   r   
ValueErrorr   pathnormpathjoinr   isfileOSErroropenprintsplitstripr   _validate_creds_file)	r   r   r   r   msginfilelinenamevaluer	   r	   r
   r   -   s4   
	zAuthenticate.load_credsc                    s   d}g d}d}g d}t  fdd|D rd}nt  fdd|D r'd}|s>|s>d j d	}|t j7 }t||rKtd
 j d dS dS )z%Check validity of a credentials file.F)app_key
app_secretoauth_tokenoauth_token_secret)r)   r*   access_tokenc                 3       | ]}| j v V  qd S Nr   .0kr   r	   r
   	<genexpr>i       z4Authenticate._validate_creds_file.<locals>.<genexpr>Tc                 3   r.   r/   r0   r1   r   r	   r
   r4   k   r5   z Missing or incorrect entries in 
zCredentials file "z" looks goodN)allr   pprintpformatr   r   r    )r   r   oauth1oauth1_keysoauth2oauth2_keysr$   r	   r   r
   r#   c   s   z!Authenticate._validate_creds_fileNNF)F)__name__
__module____qualname____doc__r   r   r#   r	   r	   r	   r
   r      s
    
6r   c           	      C   s   | du rt jt}t j|d} t| d}|d }|d }t||dd}| }d| d	}t| d
}t	||d W d   dS 1 sFw   Y  dS )zc
    For OAuth 2, retrieve an access token for an app and append it to a
    credentials file.
    Nzcredentials2.txt)r   r)   r*      )oauth_versionzaccess_token=r6   a)file)
r   r   dirname__file__r   r   r   obtain_access_tokenr   r    )	r   r   r<   r)   r*   twitterr-   tokr%   r	   r	   r
   add_access_tokenv   s   
"rL   c                 C   s&   t j| r| S t jt jd| S )z
    If the path is not absolute, guess that it is a subdirectory of the
    user's home directory.

    :param str pth: The pathname of the directory where files of tweets should be written
    ~)r   r   isabs
expanduserr   )pthr	   r	   r
   
guess_path   s   rQ   r>   r/   )	rB   r   r8   twythonr   r   r   rL   rQ   r	   r	   r	   r
   <module>   s   
	
Z