-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_metadata_uris.py
60 lines (48 loc) · 1.73 KB
/
get_metadata_uris.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pickle
import csv
from web3 import Web3
import os
import json
from dotenv import load_dotenv
load_dotenv()
def get_uris(csv_file):
WEB3_PROVIDER_URL=os.getenv("WEB3_PROVIDER_URL")
w3 = Web3(Web3.HTTPProvider(WEB3_PROVIDER_URL))
erc721_abi = {}
with open("abi/erc721.json", "r") as erc721_abi_f:
erc721_abi = json.loads(erc721_abi_f.read())
uris = []
with open(csv_file, "r") as f:
nft_owners_reader = csv.reader(f)
next(nft_owners_reader) # remove the headers
for p in nft_owners_reader:
token_address = Web3.toChecksumAddress(p[0].lower())
token_id = int(p[1])
token_contract = w3.eth.contract(token_address, abi=erc721_abi)
try:
uri = token_contract.functions.tokenURI(token_id).call()
except:
continue
print(uri)
uris.append(uri)
return [clean_uri(uri) for uri in uris if clean_uri(uri)]
def clean_uri(uri):
if uri.startswith("https://ipfs.io/ipfs/"):
uri = uri.replace("https://ipfs.io/ipfs/", "ipfs://")
if uri.startswith("ipfs://"):
# Do nothing
pass
else:
# Not a parsable ipfs uri, return None
return None
return uri
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Get IPFS metadata files to pass to bacalhau')
parser.add_argument('results_file', type=str)
args = parser.parse_args()
uris = get_uris(args.results_file)
# Write uris to a pickle that can be loaded by `generate_volume_args.py`.
# Could also pass the list directly to generate_volume_args(...)
with open("metadata_uris.pkl", "wb") as f:
pickle.dump(uris, f)