如何使用JavaScript从OpenSea检索NFT的TRAITS?

问题描述 投票:0回答:2

我有一个网站,我想知道如何从 OpenSea 获取 NFT 的 Traits。有人可以帮助我吗,我真的需要建议。感谢您的帮助。 以下是我在 OpenSea 文档上获取的特征

 "traits": [
        {
          "trait_type": "original shirt",
          "value": "collared shirt white",
          "display_type": null,
          "max_value": null,
          "trait_count": 0,
          "order": null
        },
        {
          "trait_type": "original background",
          "value": "red",
          "display_type": null,
          "max_value": null,
          "trait_count": 0,
          "order": null
        },
        {
          "trait_type": "original type",
          "value": "plain mfer",
          "display_type": null,
          "max_value": null,
          "trait_count": 0,
          "order": null
        },
        {
          "trait_type": "original eyes",
          "value": "eye mask",
          "display_type": null,
          "max_value": null,
          "trait_count": 0,
          "order": null
        },
        {
          "trait_type": "mouth",
          "value": "smile",
          "display_type": null,
          "max_value": null,
          "trait_count": 0,
          "order": null
        },
        {
          "trait_type": "original headphones",
          "value": "white headphones",
          "display_type": null,
          "max_value": null,
          "trait_count": 0,
          "order": null
        },
        {
          "trait_type": "original smoke",
          "value": "cig black",
          "display_type": null,
          "max_value": null,
          "trait_count": 0,
          "order": null
        },
        {
          "trait_type": "original 4:20 watch",
          "value": "oyster silver",
          "display_type": null,
          "max_value": null,
          "trait_count": 0,
          "order": null
        }
      ],

在此处查看 API:https://docs.opensea.io/reference/retriving-assets-rinkeby

node.js api blockchain nft
2个回答
0
投票

OpenSea API 允许您通过向 /nft/{id}/traits 端点发出 GET 请求来检索 NFT 的特征。端点中的 {id} 是您要检索其特征的 NFT 的 ID。


0
投票

如果您仍然想知道如何解决提取 NFT 特征的问题,下面的代码应该非常有帮助。

(请注意,当我为改进 NFT 数据提取和处理的库编写下面的代码时,我使用了 Python 和 requests 库,但用 JavaScript 重写它应该非常简单,因为基本概念是相同的。)

根据集合的不同,包含元数据的 URL 将如下所示: “https://ipfs.io/ipfs/QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/8128”

正如其他人指出的那样,您只需在包含元数据的链接上使用 get 请求即可。最简单的形式,它看起来像这样。

response = requests.get(url)

if response.status_code == 200:
    metadata = response.json()
    traits = metadata.get('attributes', [])  # Returns a list of attributes
    return traits
else:
    print(f"Error extracting traits from {url}: Status Code {response.status_code}")

您还可以将其放入函数中并添加更好的异常处理。

def extract_traits(url):
    
    max_retries = 3  # Maximum number of retries
    retries = 0

    while retries < max_retries:

        response = requests.get(url)

        if response.status_code == 200:

            metadata = response.json()
            traits = metadata.get('attributes', [])  # Returns a list of attributes
            return traits

        elif response.status_code == 429:  # Too Many Requests

            if 'Retry-After' in response.headers:
                retry_after = int(response.headers['Retry-After'])
                print(f"Rate limit exceeded. Waiting for {retry_after} seconds before retrying...")
                time.sleep(retry_after)  # Wait for the specified duration before retrying

            else:
                print("Rate limit exceeded. Waiting for a default duration before retrying...")
                time.sleep(5)  # Default wait time

            retries += 1
        else:
            print(f"Error extracting traits from {url}: Status Code {response.status_code}")
            return []

    print("Maximum number of retries reached. Aborting.")
    return []

希望能够消除您对特征提取的任何疑虑!

© www.soinside.com 2019 - 2024. All rights reserved.