a
    bg"                     @   sl   d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl	Z	d dl
mZ d dlmZ G dd deZdS )    N)IteratorListLiteralOptional)Document)
BaseLoaderc                
   @   sp   e Zd ZdZdddddddeed eeeeee  eee	 ddd	Z
ee d
ddZee d
ddZdS )MintbaseDocumentLoadera  Load elements from a blockchain smart contract.

    The supported blockchains are: Near mainnet, Near testnet.

    If no BlockchainType is specified, the default is Near mainnet.

    The Loader uses the Mintbase API to interact with the blockchain.
    MB_API_KEY environment variable must be set to use this loader.

    The API returns 100 NFTs per request and can be paginated using the
    startToken parameter.

    If get_all_tokens is set to True, the loader will get all tokens
    on the contract.  Note that for contracts with a large number of tokens,
    this may take a long time (e.g. 10k tokens is 100 requests).
    Default value is false for this reason.

    The max_execution_time (sec) can be set to limit the execution time
    of the loader.

    Future versions of this loader can:
        - Support additional Mintbase APIs (e.g. getTokens, etc.)

    Example:
        .. code-block:: python

            contractAddress = "nft.yearofchef.near"  # Year of chef contract address
            blockchainLoader = MintbaseDocumentLoader(
                contract_address=contractAddress, blockchain_type="mainnet",api_key="omni-site"
            )
     NF)api_keytableselectfieldsget_all_tokensmax_execution_time)ZmainnetZtestnet)contract_addressblockchain_typer
   r   r   r   r   r   c          	      C   s|   || _ || _tjdp|| _dp$|| _dp.|| _|p<g d| _|| _	|| _
| jsZtdtd| j sxtd| j  dS )	a  

        Args:
            contract_address: The address of the smart contract.
            blockchainType: The blockchain type.
            api_key: The Mintbase API key.
            table: name of the table to query
            select: Conditions for querying
            fields: Information to display after query
            get_all_tokens: Whether to get all tokens on the contract.
            max_execution_time: The maximum execution time (sec).
        Z
MB_API_KEYmb_views_nft_tokensz3where: {nft_contract_id: {_eq: "contract_address"}})'Zbase_uriZburned_receipt_idZburned_timestampZcopiesdescriptionZ
expires_atextraZ	issued_atZlast_transfer_receipt_idZlast_transfer_timestampZmediaZ
media_hashZmetadata_content_flagZmetadata_idZ	mint_memoZminted_receipt_idZminted_timestampZminterZnft_contract_content_flagZnft_contract_created_atZnft_contract_iconZnft_contract_idZnft_contract_is_mintbaseZnft_contract_nameZnft_contract_owner_idZnft_contract_referenceZnft_contract_specZnft_contract_symbolowner	referenceZreference_blobZreference_hashZ	royaltiesZroyalties_percentZsplitsZ	starts_attitletoken_idZ
updated_atzMintbase API key not provided.z7^(([a-z\d]+[\-_])*[a-z\d]+\.)*([a-z\d]+[\-_])*[a-z\d]+$zInvalid contract address N)r   blockchainTypeosenvirongetr
   r   r   r   r   r   
ValueErrorrematch)	selfr   r   r
   r   r   r   r   r    r!   {/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/mintbase.py__init__.   s    

*zMintbaseDocumentLoader.__init__)returnc                 C   sB  g }t   }d}|d| j}|d| j}|d| j}|dd| j}| jdd}|i d	d
}d| j }t	j
||t|d}|jdkrtd|j | d d }|sq(|D ]6}	t|	}
|	d }| j| j|d}|t|
|d q| jsq(| jd urt   | | jkrtdq|s>td| j |S )Nx
            query MyQuery {
            table(select) {
                fields
            }
            }
            r   r   r   r   
application/jsonz
mb-api-keyzContent-TypeMyQueryquery	variablesZoperationNamehttps://graph.mintbase.xyz/headersdata    Request failed with status code r0   r   r   sourceZ
blockchaintokenIdZpage_contentmetadata/Execution time exceeded the allowed time limit.z#No NFTs found for contract address )timereplacer   r   r   joinr   r
   r   requestspostjsondumpsstatus_coder   strappendr   r   r   RuntimeError)r    result
start_timeoperations_docr/   r0   urlresponseitemsitemcontentr   r7   r!   r!   r"   load   sV    	



zMintbaseDocumentLoader.loadc                 c   s"  t   }d}|d| j}|d| j}|d| j}|dd| j}| jdd}|i d	d
}d| j }t	j
||t|d}|jdkrtd|j | d d }|sq|D ]2}t|}	|d }
| j| j|
d}t|	|dV  q| jsq| jd urt   | | jkrtdqd S )Nr%   r   r   r   r   r&   r'   r(   r)   r*   r-   r.   r1   r2   r0   r   r   r3   r6   r8   )r9   r:   r   r   r   r;   r   r
   r   r<   r=   r>   r?   r@   r   rA   r   r   r   rC   )r    rE   rF   r/   r0   rG   rH   rI   rJ   rK   r5   r7   r!   r!   r"   	lazy_load   sJ    	

z MintbaseDocumentLoader.lazy_load)__name__
__module____qualname____doc__rA   r   r   r   boolintr#   r   rL   r   rM   r!   r!   r!   r"   r      s&   %
SIr   )r>   r   r   r9   typingr   r   r   r   r<   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   r   r!   r!   r!   r"   <module>   s   