o
    Zh                     @  s~   d dl mZ d dl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r0d dlZddd	ZG d
d deZdS )    )annotationsN)TYPE_CHECKINGAnyDictIterableIteratorListOptionalSequenceDocument)
BaseLoaderreturnmastodonc                  C  s&   zdd l } W | S  ty   tdw )Nr   zOMastodon.py package not found, please install it with `pip install Mastodon.py`)r   ImportError)r    r   d/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/mastodon.py_dependable_mastodon_import   s   
r   c                   @  s8   e Zd ZdZ				ddddZdddZd ddZdS )!MastodonTootsLoaderzLoad the `Mastodon` 'toots'.d   FNhttps://mastodon.socialmastodon_accountsSequence[str]number_tootsOptional[int]exclude_repliesboolaccess_tokenOptional[str]api_base_urlstrc                 C  s<   t  }|p
tjd}|j||d| _|| _|| _|| _dS )a  Instantiate Mastodon toots loader.

        Args:
            mastodon_accounts: The list of Mastodon accounts to query.
            number_toots: How many toots to pull for each account. Defaults to 100.
            exclude_replies: Whether to exclude reply toots from the load.
                Defaults to False.
            access_token: An access token if toots are loaded as a Mastodon app. Can
                also be specified via the environment variables "MASTODON_ACCESS_TOKEN".
            api_base_url: A Mastodon API base URL to talk to, if not using the default.
                Defaults to "https://mastodon.social".
        ZMASTODON_ACCESS_TOKEN)r   r   N)	r   osenvirongetZMastodonapir   r   r   )selfr   r   r   r   r   r   r   r   r   __init__%   s   
zMastodonTootsLoader.__init__r   Iterator[Document]c              	   c  sN    | j D ] }| j|}| jj|jdd| jd| jd}| ||E dH  qdS )zLoad toots into documents.FT)Z
only_mediapinnedr   Zexclude_reblogslimitN)r   r$   Zaccount_lookupZaccount_statusesidr   r   _format_toots)r%   accountusertootsr   r   r   	lazy_loadB   s   
zMastodonTootsLoader.lazy_loadr.   List[Dict[str, Any]]	user_infodictIterable[Document]c                 c  s:    |D ]}|d ||d dud}t |d |dV  qdS )zlFormat toots into documents.

        Adding user info, and selected toot fields into the metadata.
        
created_atZin_reply_to_idN)r4   r1   Zis_replycontent)Zpage_contentmetadatar   )r%   r.   r1   Ztootr6   r   r   r   r+   P   s   

z!MastodonTootsLoader._format_toots)r   FNr   )
r   r   r   r   r   r   r   r   r   r    )r   r'   )r.   r0   r1   r2   r   r3   )__name__
__module____qualname____doc__r&   r/   r+   r   r   r   r   r   "   s    
r   )r   r   )
__future__r   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   <module>   s    (
