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

38 lines
1.1 KiB
Python
Raw Normal View History

2022-01-18 11:09:54 +00:00
import ujson as json
2022-01-20 12:13:03 +00:00
from PIL import Image
2023-02-05 14:33:33 +00:00
from core.utils.http import get_url, download_to_cache
2022-01-18 11:09:54 +00:00
async def uuid_to_name(uuid):
2022-08-01 15:33:35 +00:00
res = json.loads(await get_url(f'https://api.mojang.com/user/profiles/{uuid}/names', 200))
2022-01-18 11:09:54 +00:00
return res[0]['name']
async def name_to_uuid(name):
2022-08-01 15:33:35 +00:00
res = json.loads(await get_url(f'https://api.mojang.com/users/profiles/minecraft/{name}', 200))
2022-01-18 11:09:54 +00:00
return res['id']
async def uuid_to_skin_and_cape(uuid):
2022-07-21 17:40:01 +00:00
render = await download_to_cache(
2022-01-18 11:09:54 +00:00
'https://crafatar.com/renders/body/' + uuid + '?overlay')
2022-07-21 17:40:01 +00:00
skin = await download_to_cache(
2022-07-21 17:41:04 +00:00
'https://crafatar.com/skins/' + uuid)
is_cape = True
2022-01-18 11:09:54 +00:00
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)
2022-07-21 17:40:01 +00:00
return {'render': render, 'skin': skin, 'cape': path}
2022-01-18 11:09:54 +00:00
2022-01-20 12:13:03 +00:00
2022-01-18 11:09:54 +00:00
__all__ = ['uuid_to_name', 'name_to_uuid', 'uuid_to_skin_and_cape']