forked from rounakbanik/generative-art-nft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.py
69 lines (52 loc) · 1.85 KB
/
metadata.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
61
62
63
64
65
66
67
68
69
import pandas as pd
import os
from progressbar import progressbar
import json
from copy import deepcopy
import time
JSON = {
"name": "Bean #",
"description": "",
"image": "ipfs://1234567890",
"edition": 1,
"date": int(time.time()),
"attributes": [],
}
# Get metadata and JSON files path based on the collection name
def generate_paths(name):
path = os.path.join("output", name)
metadata_path = os.path.join(path, "metadata.csv")
json_path = os.path.join(path, "json")
return path, metadata_path, json_path
# Function to get attribute metadata
def get_attribute_metadata(metadata_path):
df = pd.read_csv(metadata_path)
df = df.drop("Unnamed: 0", axis=1)
df.columns = [col for col in df.columns]
return df
def main():
print("Enter the edition you want to generate metadata for: ")
name = input()
path, metadata_path, json_path = generate_paths(name)
if not os.path.exists(path):
print("Oops! Looks like this collection doesn't exist!")
return
# Make json folder
if not os.path.exists(json_path):
os.makedirs(json_path)
df = get_attribute_metadata(metadata_path)
for index, row in progressbar(df.iterrows()):
item_json = deepcopy(JSON)
item_json["name"] += str(index)
item_json["image"] += f"/{index}.png"
# Add all existing traits to attributes dictionary
attributes = dict(row)
for attribute in attributes:
if attributes[attribute] != "none":
item_json['attributes'].append({"trait_type": attribute, "value": attributes[attribute]})
# Write file to json folder
item_json_path = os.path.join(json_path, str(index))
with open(item_json_path, "w", encoding="utf-8") as file:
json.dump(item_json, file, indent=4)
if __name__ == "__main__":
main()