Trying to print all owners of an NFT collection on OpenSea using API and Python 3

Viewed 3003

I am trying to get a list of owners of the collection Aethir Wars on Opensea.io. I want the owner, which NFT they hold, and their ETH address. I am able to get a long list of owners and their address, but the list isn't complete for some reason. Also, I had to add an if statement because if an NFT was transferred and not purchased, the "Last_Sale" Parameter is null. Any help is appreciated.

import requests

url = "https://api.opensea.io/api/v1/assets"
api_key = "api key"
col = 'reapers'

def print_owners(race, offset):
    querystring = {'order_by': 'name', 'order_direction': 'asc', 'offset': offset, 'limit': '50',
                   'collection': col, 'X-API-KEY': api_key}
    response = requests.get(url, params=querystring)
    data = response.json()
    i = 0
    for each_asset in data['assets']:
        try:
            if race in data['assets'][0]['name']:
                if data["assets"][i]["last_sale"] is None:
                    print(data["assets"][i]["name"] + ': '
                          + data["assets"][i]["sell_orders"][0]["maker"]["address"])
                else:
                    print(data["assets"][i]["name"] + ': '
                          + data["assets"][i]["last_sale"]["transaction"]["from_account"]["user"]["username"])
        except TypeError:
            pass
        i += 1


x = range(0, 600, 50)

for stuff in x:
    try:
        print_owners(race='Reaper', offset=str(stuff))
    except KeyError:
        pass

    try:
        print_owners(race='Android', offset=str(stuff))
    except KeyError:
        pass

    try:
        print_owners(race='Goblin', offset=str(stuff))
    except KeyError:
        pass
1 Answers

I have found a lot of examples where any of these ["user"]["username"]['name'] are null. If you don't handle the if == null it throws an exception.

I have a sample that appears to work in Python, using Streamlit.

Related