From 8fb708f0fa9c9ff0c1b43511267c4d48582aaf46 Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 28 Oct 2018 16:40:35 +0100 Subject: [PATCH] Added timestamp support to UUIDs --- README.md | 9 +++++++++ mcuuid/api.py | 29 +++++++++++++++++------------ test.py | 17 +++++++++++++---- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 89d94bf..0246ec5 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,15 @@ When `identifier = "gronkh"`: and `name` will be `"Gronkh"` +``` +player = GetPlayerData(identifier, timestamp) +``` + +Some names are time-sensitive. (When they are changed) +So you can choose a special time. + +It even works with UUIDs. They respond the username at the given time. + ### Tools Syntax check of username ``` diff --git a/mcuuid/api.py b/mcuuid/api.py index 72c03d0..80f4b09 100644 --- a/mcuuid/api.py +++ b/mcuuid/api.py @@ -30,6 +30,7 @@ class GetPlayerData: if timestamp is not None: get_args = "?at=" + str(timestamp) + # Build the request path based on the identifier req = "" if is_valid_minecraft_username(identifier): req = "/users/profiles/minecraft/" + identifier + get_args @@ -38,8 +39,9 @@ class GetPlayerData: else: self.valid = False + # Proceed only, when the identifier was valid if self.valid: - # Request the UUID + # Request the player data 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'}); @@ -52,31 +54,34 @@ class GetPlayerData: else: # Parse the JSON json_data = json.loads(response) + + ### Handle the response of the different requests on different ways + # Request using username if is_valid_minecraft_username(identifier): # The UUID self.uuid = json_data['id'] # The username written correctly self.username = json_data['name'] + #Request using UUID 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 + # Getting the username based on timestamp for name in json_data: - if 'changedToAt' in name: - changed_to_at = name['changedToAt'] - else: - changed_to_at = 0 + # Prepare the JSON + # The first name has no change time + if 'changedToAt' not in name: + name['changedToAt'] = 0 - if changed_to_at <= timestamp and current_time <= changed_to_at: - current_time = changed_to_at + # Get the right name on timestamp + if current_time <= name['changedToAt'] and (timestamp is None or name['changedToAt'] <= timestamp): + current_time = name['changedToAt'] current_name = name['name'] + # The username written correctly - self.username = name['name'] + self.username = current_name diff --git a/test.py b/test.py index 7b5dbcd..58e771f 100644 --- a/test.py +++ b/test.py @@ -6,23 +6,32 @@ from mcuuid.tools import is_valid_mojang_uuid ### 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 +### Which input method should we use? +# Are there some arguments brought by the console use the first after the filename as identifier +timestamp = None if len(sys.argv) > 1: identifier = sys.argv[1] -# Else, ask for a username by userinput + + if len(sys.argv) > 2: + if sys.argv[2].isdigit(): + timestamp = int(sys.argv[2]) + + +# Else, ask for a identifier by userinput else: print("Please enter a username or UUID: ") identifier = raw_input() +### Is the identifier a valid value? if is_valid_minecraft_username(identifier) or is_valid_mojang_uuid(identifier): + # Print the type of the 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) + player = GetPlayerData(identifier, timestamp) # Check if the request was valid and the user exists if player.valid is True: # Getting UUID