benno1237 / MinePI

Minecraft utility library
MIT License
20 stars 8 forks source link

await to_name(uuid) returns None even when uuid is valid #16

Closed bensonchow123 closed 1 year ago

bensonchow123 commented 1 year ago

Code to reproduce error:

from MinePI import to_name
import asyncio
async def test():
    name = await to_name("51477bde9a874fc5b477ab29ff0f9acb")
    print(name)
asyncio.run(test())
None

I think it is like this because mojang changed their api

import asyncio
from aiohttp import ClientSession
from json import loads
async def to_name(uuid: str, timestamp: float = None):
    async with ClientSession() as session:
        async with session.get("https://api.mojang.com/user/profiles/{}/names".format(uuid)) as resp:
            name_dict = loads(await resp.text())
            print(name_dict)
            if resp.status == 200:
                if timestamp == None:
                    max_entry = len(name_dict) - 1
                    name = name_dict[max_entry]["name"]
                    return name
                else:
                    for i in range(len(name_dict) - 1, 0, -1):
                        if timestamp > name_dict[i]["changedToAt"]:
                            return name_dict[i]["name"]
                    else:
                        return name_dict[0]["name"]
            elif resp.status == 400:
                raise ValueError(name_dict["errorMessage"])
name = asyncio.run(to_name("15014a7479d44ab090cccf83e9eac3d3"))
print(name)

if i run your to_name function and print the dict that the mojang api responed

{'error': 'Not Found', 'errorMessage': 'The server has not found anything matching the request URI'}
None
bensonchow123 commented 1 year ago

image I just checked and they removed the "https://api.mojang.com/user/profiles/{}/names" endpoint You need to make it so it uses the "https://sessionserver.mojang.com/session/minecraft/profile/uuid" endpoint

benno1237 commented 1 year ago

Sorry for answering late. You are right, they removed the endpoint. Thanks for letting me know.

The workaround isn't hard, but it will (again) increase the amount of requests. I will see what I can do to keep it down nevertheless.

A fix should (hopefully) be up in the comming days.

bensonchow123 commented 1 year ago

Still broken :(, my bot still doesn't work

benno1237 commented 1 year ago

Issue has been fixed in the latest release. Refer to the documentation here https://minepi.readthedocs.io/en/latest/ for more infos about the release