-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnftutil.py
More file actions
73 lines (65 loc) · 2 KB
/
nftutil.py
File metadata and controls
73 lines (65 loc) · 2 KB
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
import os, json
'''
NFTDataMapper
Responsible for transforming Scaffold map data into a metadata standard format
Formats Supported: OpenSea
'''
class NFTDataMapper:
def __init__(self, name, asset_url, data, url=None, prompt=None, attr_map=None, value_map=None, exclude=None):
self.name = name
self.data = data # required: data in the Scaffold format with attr as a key
self.map = attr_map # if used only include attributes in the map {'old_attr_name': 'new_attr_name', ...}
self.valmap = value_map # if used, allows remap of attribute values
self.has_valmap = value_map is not None
self.prompt = prompt # if used override prompt from data
self.asset_url = asset_url
self.url = url
self.exclude = exclude # if used, exclude all attributes from list
# retrieve a value from the data or from data.attr and perform value transcription if necessary
def get(self, key):
if key is None:
return ''
o = ''
if key in self.data:
o = self.data[key]
else:
if key in self.data['attr']:
o = self.data['attr'][key]
if self.has_valmap:
if o in self.valmap:
o = self.valmap[o]
return o
# retrieve a key from the attribute key map
def _attr(self, key):
if self.map is None:
return key
if key in self.map:
return self.map[key]
else:
return key
# retrieve a value that is mapped from
def attr(self, key):
return self.get(self._attr(key))
def filter(self, key):
if self.exclude is None:
return True
return key not in self.exclude
def opensea(self, out_path=None):
attr = []
for k, v in self.data['attr'].items():
if self.filter(k):
attr.append({'trait_type': self._attr(k), 'value': self.get(k)})
o = {
'description': self.get('description'),
'external_url': self.url,
'image': self.asset_url,
'name': self.name,
'attributes': attr,
'comment': 'https://docs.opensea.io/docs/metadata-standards',
}
if out_path is not None:
with open(out_path, 'w+') as f:
json.dump(o, f)
return o
def enjin(self, out_path):
return None