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)
|
2022-01-18 11:21:14 +00:00
|
|
|
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']
|