forked from ProjectOpenSea/metadata-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
159 lines (128 loc) · 5.79 KB
/
app.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
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
from flask import Flask
from flask import jsonify
from google.cloud import storage
from google.oauth2 import service_account
from PIL import Image
import os
import mimetypes
GOOGLE_STORAGE_PROJECT = os.environ['GOOGLE_STORAGE_PROJECT']
GOOGLE_STORAGE_BUCKET = os.environ['GOOGLE_STORAGE_BUCKET']
app = Flask(__name__)
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]
@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://example.com/?token_id=%s' % token_id,
'attributes': attributes
})
@app.route('/api/box/<token_id>')
def 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://example.com/?token_id=%s' % token_id,
'attributes': attributes
})
@app.route('/api/factory/<token_id>')
def 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://example.com/?token_id=%s' % token_id,
'attributes': attributes
})
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.png" % 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
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)
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)