Added timestamp support to UUIDs

This commit is contained in:
clerie 2018-10-28 16:40:35 +01:00
parent ff4f5da25c
commit 8fb708f0fa
3 changed files with 39 additions and 16 deletions

View File

@ -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
```

View File

@ -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

17
test.py
View File

@ -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