a
    bg                     @  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r`d dlZdddd	ZG d
d deZdS )    )annotationsN)TYPE_CHECKINGAnyDictIterableIteratorListOptionalSequenceDocument)
BaseLoadermastodonreturnc                  C  s,   zdd l } W n ty&   tdY n0 | S )Nr   zOMastodon.py package not found, please install it with `pip install Mastodon.py`)r   ImportError)r    r   {/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/mastodon.py_dependable_mastodon_import   s    
r   c                   @  sH   e Zd ZdZddddd	d
dddZddddZddddddZdS )MastodonTootsLoaderzLoad the `Mastodon` 'toots'.d   FNhttps://mastodon.socialzSequence[str]zOptional[int]boolzOptional[str]str)mastodon_accountsnumber_tootsexclude_repliesaccess_tokenapi_base_urlc                 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__zIterator[Document]r   c              	   c  sL   | 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_loadzList[Dict[str, Any]]dictzIterable[Document])r+   	user_infor   c                 c  s8   |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)r/   r.   Zis_replycontent)Zpage_contentmetadatar   )r#   r+   r.   Ztootr1   r   r   r   r(   P   s    
z!MastodonTootsLoader._format_toots)r   FNr   )__name__
__module____qualname____doc__r$   r,   r(   r   r   r   r   r   "   s       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   (