From 0963e78b4bf7e037a82a233beaf63f744c1d6394 Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 28 Oct 2018 12:02:55 +0100 Subject: [PATCH] Rewrite class construction, usage, test and readme. Added correct name support. --- README.md | 49 +++++++++++++++++++++++++++++++++------------ mcuuid.py | 48 ++++++++++++++++++++++++++++++++++++++++++++ test.py | 32 +++++++++++++++++++++-------- username_to_uuid.py | 41 ------------------------------------- 4 files changed, 108 insertions(+), 62 deletions(-) create mode 100644 mcuuid.py delete mode 100644 username_to_uuid.py diff --git a/README.md b/README.md index 0fbbf60..0a58da2 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,43 @@ -# Minecraft Username to UUID -Convert old Minecraft usernames to their UUID. -Written for Python3. +# MCUUID +Getting Minecraft Player Information from Mojang API. ## Usage -1. Add the username_to_uuid.py file to your Python path. -2. Use the library like this: -``` -from username_to_uuid import UsernameToUUID +1. Add the mcuuid.py file to your Python path. +2. Use the module like this: -converter = UsernameToUUID("MrLolEthan") -uuid = converter.get_uuid() - -... # Do stuff ``` -The contents of `uuid` will now be: -`854dd5e0fe044568a787053417ea6335` +from mcuuid import GetPlayerData + +player = GetPlayerData(username) + +if player.valid is True: + uuid = player.uuid + name = player.username +``` + +Read `test.py` for some explaination. + +When `username = "gronkh"`: +`uuid` will be `"a2080281c2784181b961d99ed2f3347c"` +and `name` will be `"Gronkh"` + +## Test file +Usage +``` +$ python test.py gronkh +``` +or +``` +$ python test.py +Please enter a username: +gronkh +``` + +Response: +``` +UUID: a2080281c2784181b961d99ed2f3347c +correct name: Gronkh +``` ## License This software is licensed under the MIT license. Feel free to use it however you like. diff --git a/mcuuid.py b/mcuuid.py new file mode 100644 index 0000000..b3a072c --- /dev/null +++ b/mcuuid.py @@ -0,0 +1,48 @@ +""" Username to UUID +Converts a Minecraft username to it's UUID equivalent. + +Uses the official Mojang API to fetch player data. +""" + +### Import necessary modules +import http.client +import json + + +### Main class +class GetPlayerData: + def __init__(self, username, timestamp=None): + self.valid = True + """ + Get the UUID of the player. + + Parameters + ---------- + username: string + The known minecraft username + timestamp : long integer (optional) + The time at which the player used this name, expressed as a Unix timestamp. + """ + + # Handle the timestamp + get_args = "" + if timestamp is not None: + get_args = "?at=" + str(timestamp) + + # Request the UUID + http_conn = http.client.HTTPSConnection("api.mojang.com"); + http_conn.request("GET", "/users/profiles/minecraft/" + username + get_args, + headers={'User-Agent':'https://github.com/clerie/mcuuid', 'Content-Type':'application/json'}); + response = http_conn.getresponse().read().decode("utf-8") + + # In case the answer is empty, the user dont exist + if not response: + self.valid = False + # If there is an answer, fill out the variables + else: + # Parse the JSON + json_data = json.loads(response) + # The UUID + self.uuid = json_data['id'] + # The username written correctly + self.username = json_data['name'] diff --git a/test.py b/test.py index 4582d28..a307b50 100644 --- a/test.py +++ b/test.py @@ -1,14 +1,30 @@ -from username_to_uuid import UsernameToUUID +### Import our module +from mcuuid import GetPlayerData + +### Import some other necessary modules import sys +### Which username should we use? +# Are there some arguments brought by the console use the first after the filename as username +if len(sys.argv) > 1: + username = sys.argv[1] +# Else, ask for a username by userinput +else: + print("Please enter a username: ") + username = raw_input() -print("Please enter a username: ") -username = sys.stdin.readline().rstrip() +### Obtaining the playerinformation using our module +player = GetPlayerData(username) +# Check if the request was valid and the user exists +if player.valid is True: + # Getting UUID + uuid = player.uuid + # Getting real Username + name = player.username -converter = UsernameToUUID(username) -uuid = converter.get_uuid() - -if (uuid is not ""): - print("That player's UUID is: " + uuid) + # Print everything + print('UUID: ' + uuid) + print('correct name: ' + name) +# Error message else: print("That player was not found.") diff --git a/username_to_uuid.py b/username_to_uuid.py deleted file mode 100644 index afc596e..0000000 --- a/username_to_uuid.py +++ /dev/null @@ -1,41 +0,0 @@ -""" Username to UUID - Converts a Minecraft username to it's UUID equivalent. - -Uses the official Mojang API to fetch player data. -""" -import http.client -import json - - -class UsernameToUUID: - def __init__(self, username): - self.username = username - - def get_uuid(self, timestamp=None): - """ - Get the UUID of the player. - - 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) - try: - uuid = json_data['id'] - except KeyError as e: - print("KeyError raised:", e); - - return uuid