Ai
1 Star 0 Fork 0

ProjectOpenSea/metadata-api-python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.py 5.79 KB
一键复制 编辑 原始数据 按行查看 历史
Daniel Viau 提交于 2021-08-18 11:19 +08:00 . replace example url
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)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ProjectOpenSea/metadata-api-python.git
git@gitee.com:ProjectOpenSea/metadata-api-python.git
ProjectOpenSea
metadata-api-python
metadata-api-python
master

搜索帮助