
    dh"                     r    S SK r S SKrS SKrS SKrS SKJrJrJrJr  S SK	r	S SK
Jr  S SKJr   " S S\5      rg)    N)IteratorListLiteralOptional)Document)
BaseLoaderc                       \ rS rSrSrSSSSSSS.S\S\S	   S
\S\S\S\\\      S\	S\\
   4S jjrS\\   4S jrS\\   4S jrSrg)MintbaseDocumentLoader   aB  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                   Xl         X l        [        R                  R	                  S5      =(       d    UU l        SU l        SU l        U=(       d    / SQU l        Xpl	        Xl
        U R
                  (       d  [        S5      e[        R                  " SU R                   5      (       d  [        SU R                    35      eg)	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	            e/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/document_loaders/mintbase.py__init__MintbaseDocumentLoader.__init__.   s    0 !1-zz~~l3>w*
K (
 (!
T -"4||=>>xxF!!
 
 89N9N8OPQQ	
    returnc                    / n[         R                   " 5       n SnUR                  SU R                  5      nUR                  SU R                  5      nUR                  SU R                  5      nUR                  SSR                  U R                  5      5      nU R                  SS.nU0 S	S
.nSU R                   3n[        R                  " Xd[        R                  " U5      S9nUR                  S:w  a  [        SUR                   35      eUR                  5       S   S   nU(       d  OU HE  n	[        U	5      n
U	S   nU R                  U R                  US.nUR!                  [#        XS95        MG     U R$                  (       d  OAU R&                  b1  [         R                   " 5       U-
  U R&                  :  a  [)        S5      eGM  U(       d  [        SU R                   35      eU$ )Nx
            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 r[   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   rA   requestspostjsondumpsstatus_coderE   strappendr   r   r   RuntimeError)rH   result
start_timeoperations_docrZ   r[   urlresponseitemsitemcontentr?   rd   s                rI   loadMintbaseDocumentLoader.load   s   YY[
N ,33HdkkJN+33"D$9$9N ,33GTZZHN+33Hdii>TUN &*\\CUVG (!*D 00C0C/DEC}}S

4@PQH##s* 6x7K7K6LM  MMOF+,ABEd)
+"33"&"5"5'
 hGOP  && ''3YY[:-1H1HH"#TUUw z 5d6K6K5LM  rL   c              #     #    [         R                   " 5       n SnUR                  SU R                  5      nUR                  SU R                  5      nUR                  SU R                  5      nUR                  SSR                  U R                  5      5      nU R                  SS.nU0 S	S
.nSU R                   3n[        R                  " XS[        R                  " U5      S9nUR                  S:w  a  [        SUR                   35      eUR                  5       S   S   nU(       d  g U H8  n[        U5      n	US   n
U R                  U R                  U
S.n[!        XS9v   M:     U R"                  (       d  g U R$                  b1  [         R                   " 5       U-
  U R$                  :  a  ['        S5      eGM  7f)NrO   r   r   r   r   rP   rQ   rR   rS   rT   rX   rY   r\   r]   r[   r   r?   r^   rb   re   )rf   rg   r   r   r   rh   r   r   rA   ri   rj   rk   rl   rm   rE   rn   r   r   r   rp   )rH   rr   rs   rZ   r[   rt   ru   rv   rw   rx   ra   rd   s               rI   	lazy_load MintbaseDocumentLoader.lazy_load   s    YY[
N ,33HdkkJN+33"D$9$9N ,33GTZZHN+33Hdii>TUN &*\\CUVG (!*D 00C0C/DEC}}S

4@PQH##s* 6x7K7K6LM  MMOF+,ABEd)z*"33"&"5"5&
 GGG  && ''3YY[:-1H1HH"#TUUw s   F<F>)r   rA   r   r   r   r   r   r   )__name__
__module____qualname____firstlineno____doc__rn   r   r   r   boolintrJ   r   ry   r   r|   __static_attributes__ rL   rI   r
   r
      s    J &*$,0QRQR !!56	QR
 QR QR QR c#QR QR %SMQRfGd8n GR>V8H- >VrL   r
   )rk   rB   rF   rf   typingr   r   r   r   ri   langchain_core.documentsr   )langchain_community.document_loaders.baser   r
   r   rL   rI   <module>r      s0     	 	  4 4  - @{VZ {VrL   