
    Ng"                     z    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e          ZdS )    N)IteratorListLiteralOptional)Document)
BaseLoaderc                       e Zd ZdZddddddddeded         d	ed
ededeee                  dedee	         fdZ
dee         fdZdee         f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contract_addressblockchain_type)mainnettestnetr   r   r   r   r   r   c                J   || _         || _        t          j                            d          p|| _        d| _        d| _        |pg d| _        || _	        || _
        | j        st          d          t          j        d| j                   st          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).
        
MB_API_KEYmb_views_nft_tokensz3where: {nft_contract_id: {_eq: "contract_address"}})'base_uriburned_receipt_idburned_timestampcopiesdescription
expires_atextra	issued_atlast_transfer_receipt_idlast_transfer_timestampmedia
media_hashmetadata_content_flagmetadata_id	mint_memominted_receipt_idminted_timestampminternft_contract_content_flagnft_contract_created_atnft_contract_iconnft_contract_idnft_contract_is_mintbasenft_contract_namenft_contract_owner_idnft_contract_referencenft_contract_specnft_contract_symbolowner	referencereference_blobreference_hash	royaltiesroyalties_percentsplits	starts_attitletoken_id
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   s	            i/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/mintbase.py__init__zMintbaseDocumentLoader.__init__.   s    0 !1-z~~l33>w*
K (
 (!
 (!
 (!
T -"4| 	?=>>>xF!
 
 	R P9NPPQQQ		R 	R    returnc                    g }t          j                     }	 d}|                    d| j                  }|                    d| j                  }|                    d| j                  }|                    dd                    | j                            }| j        dd	}|i d
d}d| j         }t          j
        ||t          j        |                    }|j        dk    rt          d|j                   |                                d         d         }|sn|D ]M}	t          |	          }
|	d         }| j        | j        |d}|                    t#          |
|                     N| j        sn7| j        .t          j                     |z
  | j        k    rt)          d          |st          d| j                   |S )NTx
            query MyQuery {
            table(select) {
                fields
            }
            }
            r   r   r   r   
application/jsonz
mb-api-keyzContent-TypeMyQueryquery	variablesoperationNamehttps://graph.mintbase.xyz/headersdata    Request failed with status code rY   r   r>   source
blockchaintokenId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_coderD   strappendr   r   r   RuntimeError)rG   result
start_timeoperations_docrX   rY   urlresponseitemsitemcontentr>   rb   s                rH   loadzMintbaseDocumentLoader.load   s!   Y[[
;	VN ,33HdkJJN+33"D$9 N ,33GTZHHN+33Hdii>T>TUUN &*\CUVVG (!* D F0CEEC}S'
4@P@PQQQH#s** Mx7KMM   MMOOF+,ABE  Q Qd))
+"3"&"5' 
 hGhOOOPPPP &  '3Y[[:-1HHH"#TUUUw;	Vz  	Md6KMM   rJ   c              #   @  K   t          j                     }	 d}|                    d| j                  }|                    d| j                  }|                    d| j                  }|                    dd                    | j                            }| j        dd	}|i d
d}d| j         }t          j
        ||t          j        |                    }|j        dk    rt          d|j                   |                                d         d         }|sd S |D ]<}t          |          }	|d         }
| j        | j        |
d}t!          |	|          V  =| j        sd S | j        .t          j                     |z
  | j        k    rt'          d          )NTrM   r   r   r   r   rN   rO   rP   rQ   rR   rV   rW   rZ   r[   rY   r   r>   r\   r`   rc   )rd   re   r   r   r   rf   r   r   r@   rg   rh   ri   rj   rk   rD   rl   r   r   r   rn   )rG   rp   rq   rX   rY   rr   rs   rt   ru   rv   r_   rb   s               rH   	lazy_loadz MintbaseDocumentLoader.lazy_load   s     Y[[
;	VN ,33HdkJJN+33"D$9 N ,33GTZHHN+33Hdii>T>TUUN &*\CUVVG (!* D F0CEEC}S'
4@P@PQQQH#s** Mx7KMM   MMOOF+,ABE  H Hd))z*"3"&"5& 
 GhGGGGGGG &  '3Y[[:-1HHH"#TUUUw;	VrJ   )__name__
__module____qualname____doc__rl   r   r   r   boolintrI   r   rw   r   ry    rJ   rH   r
   r
      s        J &*$,0QR QR QRQR !!56	QR
 QR QR QR c#QR QR %SMQR QR QR QRfGd8n G G G GR>V8H- >V >V >V >V >V >VrJ   r
   )ri   rA   rE   rd   typingr   r   r   r   rg   langchain_core.documentsr   )langchain_community.document_loaders.baser   r
   r   rJ   rH   <module>r      s     				 				  4 4 4 4 4 4 4 4 4 4 4 4  - - - - - - @ @ @ @ @ @{V {V {V {V {VZ {V {V {V {V {VrJ   