a
    bgn                     @  s   d dl mZ d dlmZmZmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZ erhd dlZd dlmZmZ ddd	d
ZG dd deZdS )    )annotations)TYPE_CHECKINGAnyDictIterableListOptionalSequenceUnionDocument)
BaseLoaderN)OAuth2BearerHandlerOAuthHandlertweepyreturnc                  C  s,   zdd l } W n ty&   tdY n0 | S )Nr   zEtweepy package not found, please install it with `pip install tweepy`)r   ImportError)r    r   z/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/twitter.py_dependable_tweepy_import   s    
r   c                
   @  s~   e Zd ZdZdddddddZd	d
ddZddddddZeddddd dddZedddddddd dddZ	dS )TwitterTweetLoadera  Load `Twitter` tweets.

    Read tweets of the user's Twitter handle.

    First you need to go to
    `https://developer.twitter.com/en/docs/twitter-api
    /getting-started/getting-access-to-the-twitter-api`
    to get your token. And create a v2 version of the app.
    d   z(Union[OAuthHandler, OAuth2BearerHandler]zSequence[str]zOptional[int]auth_handlertwitter_usersnumber_tweetsc                 C  s   || _ || _|| _d S )N)authr   r   )selfr   r   r   r   r   r   __init__#   s    zTwitterTweetLoader.__init__zList[Document]r   c                 C  sb   t  }|j| j|j d}g }| jD ]6}|j|| jd}|j|d}| 	||}|
| q&|S )zLoad tweets.)parser)screen_namecount)r!   )r   ZAPIr   parsersZ
JSONParserr   Zuser_timeliner   Zget_user_format_tweetsextend)r   r   apiresultsusernametweetsuserdocsr   r   r   load-   s    
zTwitterTweetLoader.loadzList[Dict[str, Any]]dictzIterable[Document])r)   	user_infor   c                 c  s.   |D ]$}|d |d}t |d |dV  qdS )zFormat tweets into a string.
created_at)r/   r.   text)Zpage_contentmetadataNr   )r   r)   r.   Ztweetr1   r   r   r   r$   :   s    z!TwitterTweetLoader._format_tweetsstr)oauth2_bearer_tokenr   r   r   c                 C  s   t  }||}| |||dS )z5Create a TwitterTweetLoader from OAuth2 bearer token.r   )r   r   )clsr3   r   r   r   r   r   r   r   from_bearer_tokenH   s    
z$TwitterTweetLoader.from_bearer_token)access_tokenaccess_token_secretconsumer_keyconsumer_secretr   r   r   c           	      C  s&   t  }|j||||d}| |||dS )z;Create a TwitterTweetLoader from access tokens and secrets.)r6   r7   r8   r9   r   )r   r   )	r4   r6   r7   r8   r9   r   r   r   r   r   r   r   from_secretsX   s    zTwitterTweetLoader.from_secretsN)r   )r   )r   )
__name__
__module____qualname____doc__r   r,   r$   classmethodr5   r:   r   r   r   r   r      s    
  r   )
__future__r   typingr   r   r   r   r   r   r	   r
   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   r   r   r   r   r   r   r   r   r   <module>   s   (
