Reputation: 21
I am pretty good at python and couldn't figure out how to use the Mojang API with python.
I want to d something like GET https://api.mojang.com/users/profiles/minecraft/<username>?at=<timestamp>
(from the API) but I can't figure out how to do it! Does anyone know how to do this? I'm in python 3.8.
https://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time
Upvotes: 2
Views: 7502
Reputation: 159
The documentation is relatively straightforward.
You want to send a GET
request with their username:
import requests
username = "Bob"
resp = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{username}")
uuid = resp.json()["id"]
print(f"Bob's current UUID is {uuid}")
Optionally, you can include a UNIX timestamp to get the username's UUID at a specific point in time:
username = "Alice"
# UNIX timestamp that equates to 01/01/2018
timestamp = 1514764800
resp = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{username}?at={timestamp}")
uuid = resp.json()["id"]
print(f"Alice's UUID on 01/01/2018 was {uuid}")
Alternatively, you can use my newly released Mojang package, if you don't want to deal with the HTTP requests, JSON, and other web junk in your code.
Install it with pip by running the following console command:
python -m pip install mojang
Usage:
from mojang import API
api = API()
uuid = api.get_uuid("Alice")
print(f"Alice's UUID is {uuid}")
# or with a timestamp
uuid = api.get_uuid("Alice", timestamp=1514764800)
print(f"Alice's UUID on 01/01/2018 was {uuid}")
Upvotes: 0
Reputation: 78
You can find all the documentation here
If you read the first few lines you will notice that there is a rate limit of 600 requests per 10 minutes. After that rate limit is reached you will raise a KeyError
. To fix this we will use a try exception.
Don't forget to install the requests
library.
pip3 install requests
Getting the user's uuid
import requests
user = "Notch"
try:
resp = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{user}")
uuid = resp.json()["id"]
print(uuid)
except KeyError:
resp = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{user}")
error = resp.json()["error"]
print(error)
Timestamps
If you want to check when a user's username at a certain time period, use a UNIX time.
import requests
user = "Notch"
ts = 1420088400 # Must be a UNIX value.
try:
resp = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{user}?at={ts}")
uuid = resp.json()["id"]
print(uuid)
except KeyError:
resp = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{user}?at={ts}")
error = resp.json()["error"]
print(error)
Upvotes: 1
Reputation: 16032
It's pretty straightforward, just replace <username>
with the person's username, and the response will give your their uuid
.
Here is an example using requests
:
import requests
username = 'KrisJelbring'
url = f'https://api.mojang.com/users/profiles/minecraft/{username}?'
response = requests.get(url)
uuid = response.json()['id']
print(uuid) #7125ba8b1c864508b92bb5c042ccfe2b
Upvotes: 1