-
Notifications
You must be signed in to change notification settings - Fork 371
/
metadata.py
114 lines (83 loc) · 3.39 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# coding: utf-8
import pandas as pd
import numpy as np
import time
import os
from progressbar import progressbar
import json
from copy import deepcopy
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
# Base metadata. MUST BE EDITED.
BASE_IMAGE_URL = "ipfs://<-- Your CID Code-->"
BASE_NAME = ""
BASE_JSON = {
"name": BASE_NAME,
"description": "",
"image": BASE_IMAGE_URL,
"attributes": [],
}
# Get metadata and JSON files path based on edition
def generate_paths(edition_name):
edition_path = os.path.join('output', 'edition ' + str(edition_name))
metadata_path = os.path.join(edition_path, 'metadata.csv')
json_path = os.path.join(edition_path, 'json')
return edition_path, metadata_path, json_path
# Function to convert snake case to sentence case
def clean_attributes(attr_name):
clean_name = attr_name.replace('_', ' ')
clean_name = list(clean_name)
for idx, ltr in enumerate(clean_name):
if (idx == 0) or (idx > 0 and clean_name[idx - 1] == ' '):
clean_name[idx] = clean_name[idx].upper()
clean_name = ''.join(clean_name)
return clean_name
# Function to get attribure metadata
def get_attribute_metadata(metadata_path):
# Read attribute data from metadata file
df = pd.read_csv(metadata_path)
df = df.drop('Unnamed: 0', axis = 1)
df.columns = [clean_attributes(col) for col in df.columns]
# Get zfill count based on number of images generated
# -1 according to nft.py. Otherwise not working for 100 NFTs, 1000 NTFs, 10000 NFTs and so on
zfill_count = len(str(df.shape[0]-1))
return df, zfill_count
# Main function that generates the JSON metadata
def main():
# Get edition name
print("Enter edition you want to generate metadata for: ")
while True:
edition_name = input()
edition_path, metadata_path, json_path = generate_paths(edition_name)
if os.path.exists(edition_path):
print("Edition exists! Generating JSON metadata...")
break
else:
print("Oops! Looks like this edition doesn't exist! Check your output folder to see what editions exist.")
print("Enter edition you want to generate metadata for: ")
continue
# Make json folder
if not os.path.exists(json_path):
os.makedirs(json_path)
# Get attribute data and zfill count
df, zfill_count = get_attribute_metadata(metadata_path)
for idx, row in progressbar(df.iterrows()):
# Get a copy of the base JSON (python dict)
item_json = deepcopy(BASE_JSON)
# Append number to base name
item_json['name'] = item_json['name'] + str(idx)
# Append image PNG file name to base image path
item_json['image'] = item_json['image'] + '/' + str(idx).zfill(zfill_count) + '.png'
# Convert pandas series to dictionary
attr_dict = dict(row)
# Add all existing traits to attributes dictionary
for attr in attr_dict:
if attr_dict[attr] != 'none':
item_json['attributes'].append({ 'trait_type': attr, 'value': attr_dict[attr] })
# Write file to json folder
item_json_path = os.path.join(json_path, str(idx))
with open(item_json_path, 'w') as f:
json.dump(item_json, f)
# Run the main function
main()