Use the Mojang API directly; reduces overhead.

This commit is contained in:
mrlolethan 2015-03-11 16:55:25 -02:30
parent 192046b776
commit d78fad6937

View File

@ -1,25 +1,38 @@
""" Username to UUID """ Username to UUID
Converts a Minecraft username to it's UUID equivalent. Converts a Minecraft username to it's UUID equivalent.
Parses http://www.lb-stuff.com/Minecraft-Name-History output to retrieve the UUID Uses the official Mojang API to fetch player data.
of an old name that's no longer in use.
""" """
import http.client import http.client
import json
from bs4 import BeautifulSoup
class UsernameToUUID: class UsernameToUUID:
def __init__(self, username): def __init__(self, username):
self.username = username self.username = username
def get_uuid(self): def get_uuid(self, timestamp=None):
""" """
Get the UUID of the player. Get the UUID of the player.
"""
httpConn = http.client.HTTPConnection("www.lb-stuff.com");
httpConn.request("GET", "/Minecraft-Name-History?user=" + self.username);
response = httpConn.getresponse().read()
soup = BeautifulSoup(response)
return soup.body.findAll('p')[1].findAll('code')[1].text Parameters
----------
timestamp : long integer
The time at which the player used this name, expressed as a Unix timestamp.
"""
get_args = "" if timestamp is None else "?at=" + str(timestamp)
http_conn = http.client.HTTPSConnection("api.mojang.com");
http_conn.request("GET", "/users/profiles/minecraft/" + self.username + get_args,
headers={'User-Agent':'Minecraft Username -> UUID', 'Content-Type':'application/json'});
response = http_conn.getresponse().read().decode("utf-8")
if (not response and timestamp is None): # No response & no timestamp
return self.get_uuid(0) # Let's retry with the Unix timestamp 0.
if (not response): # No response (player probably doesn't exist)
return ""
json_data = json.loads(response)
uuid = json_data['id']
return uuid