From ff4f5da25c07390d14b0d12c80d8c3c0121935b7 Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 28 Oct 2018 15:07:58 +0100 Subject: [PATCH] Added some tools - syntax validator for usernames - syntax vaidator for UUIDs GetPlayerData now accepts UUIDs too --- README.md | 37 +++++++++++++++++++++++--- mcuuid/api.py | 70 ++++++++++++++++++++++++++++++++++++------------- mcuuid/tools.py | 31 ++++++++++++++++++++++ test.py | 45 ++++++++++++++++++++----------- 4 files changed, 145 insertions(+), 38 deletions(-) create mode 100644 mcuuid/tools.py diff --git a/README.md b/README.md index a43bf53..89d94bf 100644 --- a/README.md +++ b/README.md @@ -5,22 +5,41 @@ Getting Minecraft Player Information from Mojang API. 1. `pip install mcuuid` 2. Use the module like this: +### API ``` from mcuuid.api import GetPlayerData -player = GetPlayerData(username) +player = GetPlayerData(identifier) if player.valid is True: uuid = player.uuid name = player.username ``` -Read `test.py` for some explaination. +Identifier can be a username or a UUID. -When `username = "gronkh"`: +When `identifier = "gronkh"`: `uuid` will be `"a2080281c2784181b961d99ed2f3347c"` and `name` will be `"Gronkh"` + +### Tools +Syntax check of username +``` +from mcuuid.tools import is_valid_minecraft_username + +if is_valid_minecraft_username('gronkh'): + print('Valid') +``` + +Syntaxcheck of UUID +``` +from mcuuid.tools import is_valid_mojang_uuid + +if is_valid_mojang_uuid('a2080281c2784181b961d99ed2f3347c'): + print('Valid') +``` + ## Test file Usage ``` @@ -28,10 +47,20 @@ $ python test.py gronkh ``` or ``` +$ python test.py a2080281c2784181b961d99ed2f3347c +``` +or +``` $ python test.py -Please enter a username: +Please enter a username or UUID: gronkh ``` +or +``` +$ python test.py +Please enter a username or UUID: +a2080281c2784181b961d99ed2f3347c +``` Response: ``` diff --git a/mcuuid/api.py b/mcuuid/api.py index b3a072c..72c03d0 100644 --- a/mcuuid/api.py +++ b/mcuuid/api.py @@ -7,11 +7,12 @@ Uses the official Mojang API to fetch player data. ### Import necessary modules import http.client import json - +from tools import is_valid_minecraft_username +from tools import is_valid_mojang_uuid ### Main class class GetPlayerData: - def __init__(self, username, timestamp=None): + def __init__(self, identifier, timestamp=None): self.valid = True """ Get the UUID of the player. @@ -29,20 +30,53 @@ class GetPlayerData: 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 + req = "" + if is_valid_minecraft_username(identifier): + req = "/users/profiles/minecraft/" + identifier + get_args + elif is_valid_mojang_uuid(identifier): + req = "/user/profiles/" + identifier + "/names" + get_args 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'] + self.valid = False + + if self.valid: + # Request the UUID + http_conn = http.client.HTTPSConnection("api.mojang.com"); + http_conn.request("GET", req, + 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) + if is_valid_minecraft_username(identifier): + # The UUID + self.uuid = json_data['id'] + # The username written correctly + self.username = json_data['name'] + elif is_valid_mojang_uuid(identifier): + # The UUID + self.uuid = identifier + + if timestamp is None: + timestamp = 0 + + current_name = "" + current_time = 0 + + # Getting the current username + for name in json_data: + if 'changedToAt' in name: + changed_to_at = name['changedToAt'] + else: + changed_to_at = 0 + + if changed_to_at <= timestamp and current_time <= changed_to_at: + current_time = changed_to_at + current_name = name['name'] + + # The username written correctly + self.username = name['name'] diff --git a/mcuuid/tools.py b/mcuuid/tools.py new file mode 100644 index 0000000..ec12f6b --- /dev/null +++ b/mcuuid/tools.py @@ -0,0 +1,31 @@ +def is_valid_minecraft_username(username): + """https://help.mojang.com/customer/portal/articles/928638-minecraft-usernames""" + allowed_chars = 'abcdefghijklmnopqrstuvwxyz1234567890_' + allowed_len = [3, 16] + + username = username.lower() + + if len(username) < allowed_len[0] or len(username) > allowed_len[1]: + return False + + for char in username: + if char not in allowed_chars: + return False + + return True + +def is_valid_mojang_uuid(uuid): + """https://minecraft-de.gamepedia.com/UUID""" + allowed_chars = '0123456789abcdef' + allowed_len = 32 + + uuid = uuid.lower() + + if len(uuid) != 32: + return False + + for char in uuid: + if char not in allowed_chars: + return False + + return True diff --git a/test.py b/test.py index 1518ebd..7b5dbcd 100644 --- a/test.py +++ b/test.py @@ -1,5 +1,7 @@ ### Import our module from mcuuid.api import GetPlayerData +from mcuuid.tools import is_valid_minecraft_username +from mcuuid.tools import is_valid_mojang_uuid ### Import some other necessary modules import sys @@ -7,24 +9,35 @@ 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] + identifier = sys.argv[1] # Else, ask for a username by userinput else: - print("Please enter a username: ") - username = raw_input() + print("Please enter a username or UUID: ") + identifier = raw_input() -### 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 +if is_valid_minecraft_username(identifier) or is_valid_mojang_uuid(identifier): + if is_valid_minecraft_username(identifier): + print('Valid username') + if is_valid_mojang_uuid(identifier): + print('Valid UUID') + + ### Obtaining the playerinformation using our module + player = GetPlayerData(identifier) + # 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 + + # Print everything + print('UUID: ' + uuid) + print('correct name: ' + name) + + + # Error message + else: + print("That player was not found.") - # Print everything - print('UUID: ' + uuid) - print('correct name: ' + name) -# Error message else: - print("That player was not found.") + print('identifier is not valid')