This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
/
app.py
134 lines (106 loc) · 4.43 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
from flask import Flask
from flask import request, send_from_directory
from datetime import datetime
from flask import jsonify
from google.cloud import storage
from google.cloud.exceptions import NotFound
from google.cloud.exceptions import GoogleCloudError
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', 'Jessica', 'Fluffles', 'Dave', 'Randy']
LAST_NAMES = ['McPufflestein', 'McDonald', 'Winkleton']
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/puff/<token_id>')
def cryptopuff(token_id):
token_id = int(token_id)
num_first_names = len(FIRST_NAMES)
num_last_names = len(LAST_NAMES)
cryptopuff_name = "%s %s" % (FIRST_NAMES[token_id % num_first_names], LAST_NAMES[token_id % num_last_names])
base = token_id % 6 + 1
eyes = token_id % 2 + 1
image_url = _compose_image(['images/bases/base-%s.png' % base, 'images/eyes/eyes-%s.png' % eyes], token_id)
attributes = []
_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, 'puff_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': cryptopuff_name,
'description': "Generic puff description. This really should be customized.",
'imageUrl': image_url,
'externalUrl': 'https://cryptopuff.io/%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/box.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!",
'imageUrl': image_url,
'externalUrl': 'https://cryptopuff.io/%s' % token_id,
'attributes': attributes
})
@app.route('/api/factory/<token_id>')
def factory(token_id):
token_id = int(token_id)
image_url = _compose_image(['images/mystery/mystery.png'], token_id, "factory")
attributes = []
_add_attribute(attributes, 'number_inside', [1], token_id)
return jsonify({
'name': "Creature Sale",
'description': "Buy a magical Creature of random variety!",
'imageUrl': image_url,
'externalUrl': 'https://cryptopuff.io/%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="puffs"):
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")
if not blob.exists():
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)