-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
331 lines (269 loc) · 11.8 KB
/
app.py
File metadata and controls
331 lines (269 loc) · 11.8 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from flask import Flask, abort
from flask import jsonify
from PIL import Image
import os
import mimetypes
app = Flask(__name__)
########################################################################
# Data
########################################################################
# opensea-creatures
FIRST_NAMES = ['Herbie', 'Sprinkles', 'Boris', 'Dave', 'Randy', 'Captain']
LAST_NAMES = ['Starbelly', 'Fisherton', 'McCoy']
BASES = ['jellyfish', 'starfish', 'crab', 'narwhal', 'tealfish', 'goldfish']
EYES = ['big', 'joy', 'wink', 'sleepy', 'content']
MOUTH = ['happy', 'surprised', 'pleased', 'cute']
INT_ATTRIBUTES = [5, 2, 3, 4, 8]
FLOAT_ATTRIBUTES = [1.4, 2.3, 11.7, 90.2, 1.2]
STR_ATTRIBUTES = [
'Happy',
'Sad',
'Sleepy',
'Boring'
]
BOOST_ATTRIBUTES = [10, 40, 30]
PERCENT_BOOST_ATTRIBUTES = [5, 10, 15]
NUMBER_ATTRIBUTES = [1, 2, 1, 1]
# opensea-creatures-accessories
ACCESSORIES_IMAGES = [
'Bamboo-flute.png',
'Life-ring.png',
'Message-in-a-bottle.png',
'Pearl.png',
'Scuba-mask.png',
'Trident.png'
]
ACCESSORIES_NAMES = [a.replace('-', ' ').replace('.png', '')
for a in ACCESSORIES_IMAGES]
ACCESSORIES_ATTS_INT = [200, 11, 3, 41, 9, 172]
ACCESSORIES_ATTS_PERCENT = [5, 10, 1, 20, 15, 25]
ACCESSORIES_ATTS_LOCATION = ['Head', 'Body', 'Held', 'Held', 'Head', 'Held']
ACCESSORIES_ATTS_RARITY = [
'Common',
'Rare',
'Legendary',
'Epic',
'Divine',
'Hidden'
]
ACCESSORIES_ATTS_DEPTH = [
'beach',
'shore',
'shallows',
'deeps',
'shore',
'deeps'
]
ACCESSORIES_ATTS_GENERATION = [1, 1, 2, 1, 1, 3]
# contractURI() support
CONTRACT_URI_METADATA = {
'opensea-creatures': {
'name': 'OpenSea Creatures',
'description': 'Friendly creatures of the sea.',
'image': 'https://example.com/image.png',
'external_link': 'https://github.com/ProjectOpenSea/opensea-creatures/'
},
'opensea-erc1155': {
'name': 'OpenSea Creature Accessories',
'description': "Fun and useful accessories for your OpenSea creatures.",
'image': 'https://example.com/image.png',
'external_link': 'https://github.com/ProjectOpenSea/opensea-erc1155/'
}
}
CONTRACT_URI_METADATA_AVAILABLE = CONTRACT_URI_METADATA.keys()
########################################################################
# Routes
########################################################################
# opensea-creatures
@app.route('/api/creature/<token_id>')
def creature(token_id):
token_id = int(token_id)
num_first_names = len(FIRST_NAMES)
num_last_names = len(LAST_NAMES)
creature_name = '%s %s' % (FIRST_NAMES[token_id % num_first_names], LAST_NAMES[token_id % num_last_names])
base = BASES[token_id % len(BASES)]
eyes = EYES[token_id % len(EYES)]
mouth = MOUTH[token_id % len(MOUTH)]
image_url = _compose_image(['images/bases/base-%s.png' % base,
'images/eyes/eyes-%s.png' % eyes,
'images/mouths/mouth-%s.png' % mouth],
token_id)
attributes = []
_add_attribute(attributes, 'Base', BASES, token_id)
_add_attribute(attributes, 'Eyes', EYES, token_id)
_add_attribute(attributes, 'Mouth', MOUTH, token_id)
_add_attribute(attributes, 'Level', INT_ATTRIBUTES, token_id)
_add_attribute(attributes, 'Stamina', FLOAT_ATTRIBUTES, token_id)
_add_attribute(attributes, 'Personality', STR_ATTRIBUTES, token_id)
_add_attribute(attributes, 'Aqua Power', BOOST_ATTRIBUTES, token_id, display_type='boost_number')
_add_attribute(attributes, 'Stamina Increase', PERCENT_BOOST_ATTRIBUTES, token_id, display_type='boost_percentage')
_add_attribute(attributes, 'Generation', NUMBER_ATTRIBUTES, token_id, display_type='number')
return jsonify({
'name': creature_name,
'description': 'Friendly OpenSea Creature that enjoys long swims in the ocean.',
'image': image_url,
'external_url': 'https://openseacreatures.io/%s' % token_id,
'attributes': attributes
})
@app.route('/api/box/creature/<token_id>')
def creature_box(token_id):
token_id = int(token_id)
image_url = _compose_image(['images/box/lootbox.png'], token_id, 'box')
attributes = []
_add_attribute(attributes, 'number_inside', [3], token_id)
return jsonify({
'name': 'Creature Loot Box',
'description': 'This lootbox contains some OpenSea Creatures! It can also be traded!',
'image': image_url,
'external_url': 'https://openseacreatures.io/%s' % token_id,
'attributes': attributes
})
@app.route('/api/factory/creature/<token_id>')
def creature_factory(token_id):
token_id = int(token_id)
if token_id == 0:
name = 'One OpenSea creature'
description = 'When you purchase this option, you will receive a single OpenSea creature of a random variety. ' \
'Enjoy and take good care of your aquatic being!'
image_url = _compose_image(['images/factory/egg.png'], token_id, 'factory')
num_inside = 1
elif token_id == 1:
name = 'Four OpenSea creatures'
description = 'When you purchase this option, you will receive four OpenSea creatures of random variety. ' \
'Enjoy and take good care of your aquatic beings!'
image_url = _compose_image(['images/factory/four-eggs.png'], token_id, 'factory')
num_inside = 4
elif token_id == 2:
name = 'One OpenSea creature lootbox'
description = 'When you purchase this option, you will receive one lootbox, which can be opened to reveal three ' \
'OpenSea creatures of random variety. Enjoy and take good care of these cute aquatic beings!'
image_url = _compose_image(['images/box/lootbox.png'], token_id, 'factory')
num_inside = 3
attributes = []
_add_attribute(attributes, 'number_inside', [num_inside], token_id)
return jsonify({
'name': name,
'description': description,
'image': image_url,
'external_url': 'https://openseacreatures.io/%s' % token_id,
'attributes': attributes
})
# opensea-creatures-accessories
@app.route('/api/accessory/<token_id>')
def accessory(token_id):
token_id = int(token_id)
num_accessories = len(ACCESSORIES_NAMES)
if token_id >= num_accessories:
abort(404, description='No such token')
accessory_name = ACCESSORIES_NAMES[token_id]
image_path = 'images/accessory/%s' % ACCESSORIES_IMAGES[token_id]
image_url = _bucket_image(image_path, token_id, 'accessory')
attributes = []
_add_attribute(attributes, 'Aqua Boost', ACCESSORIES_ATTS_INT, token_id, display_type='boost_number')
_add_attribute(attributes, 'Stamina Increase', ACCESSORIES_ATTS_PERCENT, token_id, display_type='boost_percentage')
_add_attribute(attributes, 'Location', ACCESSORIES_ATTS_LOCATION, token_id)
_add_attribute(attributes, 'Depth', ACCESSORIES_ATTS_DEPTH, token_id)
_add_attribute(attributes, 'Rarity', ACCESSORIES_ATTS_RARITY, token_id)
_add_attribute(attributes, 'Generation', ACCESSORIES_ATTS_GENERATION, token_id, display_type='number')
return jsonify({
'name': accessory_name,
'description': 'A fun and useful accessory for your friendly OpenSea creatures.',
'image': image_url,
'external_url': 'https://openseacreatures.io/accessory/%s' % token_id,
'attributes': attributes
})
@app.route('/api/box/accessory/<token_id>')
def accessory_box(token_id):
token_id = int(token_id)
image_url = _compose_image(['images/box/lootbox.png'], token_id, 'box')
attributes = []
_add_attribute(attributes, 'number_inside', [3], token_id)
return jsonify({
'name': 'Accessory Loot Box',
'description': 'This lootbox contains some OpenSea Creature accessories! It can also be traded!',
'image': image_url,
'external_url': 'https://openseacreatures.io/box/accessory/%s' % token_id,
'attributes': attributes
})
@app.route('/api/factory/accessory/<token_id>')
def accessory_factory(token_id):
token_id = int(token_id)
if token_id == 0:
name = 'One OpenSea creature accessory'
description = 'When you purchase this option, you will receive a single OpenSea creature accessory of a random variety. ' \
'Enjoy and take good care of your aquatic being!'
image_url = _compose_image(['images/factory/egg.png'], token_id, 'factory')
num_inside = 1
elif token_id == 1:
name = 'Four OpenSea creature accessories'
description = 'When you purchase this option, you will receive four OpenSea creature accessories of random variety. ' \
'Enjoy and take good care of your aquatic beings!'
image_url = _compose_image(['images/factory/four-eggs.png'], token_id, 'factory')
num_inside = 4
elif token_id == 2:
name = 'One OpenSea creature accessory lootbox'
description = 'When you purchase this option, you will receive one lootbox, which can be opened to reveal three ' \
'OpenSea creature accessories of random variety. Enjoy and take good care of these cute aquatic beings!'
image_url = _compose_image(['images/box/lootbox.png'], token_id, 'factory')
num_inside = 3
attributes = []
_add_attribute(attributes, 'number_inside', [num_inside], token_id)
return jsonify({
'name': name,
'description': description,
'image': image_url,
'external_url': 'https://openseacreatures.io/%s' % token_id,
'attributes': attributes
})
# contractURI()
@app.route('/contract/<contract_name>')
def contract_uri(contract_name):
if not contract_name in CONTRACT_URI_METADATA_AVAILABLE:
abort(404, description='Resource not found')
return jsonify(CONTRACT_URI_METADATA[contract_name])
# Error handling
@app.errorhandler(404)
def resource_not_found(e):
return jsonify(error=str(e)), 404
########################################################################
# Utility code
########################################################################
def _add_attribute(existing, attribute_name, options, token_id, display_type=None):
trait = {
'trait_type': attribute_name,
'value': options[token_id % len(options)]
}
if display_type:
trait['display_type'] = display_type
existing.append(trait)
def _compose_image(image_files, token_id, path='creature'):
composite = None
for image_file in image_files:
foreground = Image.open(image_file).convert('RGBA')
if composite:
composite = Image.alpha_composite(composite, foreground)
else:
composite = foreground
output_path = 'images/output/%s/%s.png' % (path, token_id)
composite.save(output_path)
#blob = _get_bucket().blob(f'{path}/{token_id}.png')
#blob.upload_from_filename(filename=output_path)
#return blob.public_url
return output_path
def _bucket_image(image_path, token_id, path='accessory'):
#blob = _get_bucket().blob(f'{path}/{token_id}.png')
#blob.upload_from_filename(filename=image_path)
#return blob.public_url
#return 'images/output/%s/%s.png' % (path, token_id)
return image_path
def _get_bucket():
credentials = service_account.Credentials.from_service_account_file('credentials/google-storage-credentials.json')
if credentials.requires_scopes:
credentials = credentials.with_scopes(['https://www.googleapis.com/auth/devstorage.read_write'])
client = storage.Client(project=GOOGLE_STORAGE_PROJECT, credentials=credentials)
return client.get_bucket(GOOGLE_STORAGE_BUCKET)
########################################################################
# Main flow of execution
########################################################################
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)