Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/mcplayer/mojang_api.py
2023-09-30 16:35:06 +08:00

39 lines
1.2 KiB
Python

import ujson as json
from PIL import Image
from core.utils.http import get_url, download_to_cache
async def uuid_to_name(uuid):
res = json.loads(await get_url(f'https://api.mojang.com/user/profiles/{uuid}/names', 200))
return res[0]['name']
async def name_to_uuid(name):
res = json.loads(await get_url(f'https://api.mojang.com/users/profiles/minecraft/{name}', 200))
return res['id']
async def uuid_to_skin_and_cape(uuid):
try:
render = await download_to_cache(
'https://crafatar.com/renders/body/' + uuid + '?overlay')
skin = await download_to_cache(
'https://crafatar.com/skins/' + uuid)
is_cape = True
try:
await get_url('https://crafatar.com/capes/' + uuid, status_code=200)
except ValueError:
is_cape = False
path = None
if is_cape:
cape = Image.open(await download_to_cache(
'https://crafatar.com/capes/' + uuid))
cape.crop((0, 0, 10, 16))
path = 'cache/' + uuid + '_fixed.png'
cape.save(path)
return {'render': render, 'skin': skin, 'cape': path}
except:
return None
__all__ = ['uuid_to_name', 'name_to_uuid', 'uuid_to_skin_and_cape']