Added timestamp support to UUIDs
This commit is contained in:
parent
ff4f5da25c
commit
8fb708f0fa
@ -23,6 +23,15 @@ When `identifier = "gronkh"`:
|
|||||||
and `name` will be `"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
|
### Tools
|
||||||
Syntax check of username
|
Syntax check of username
|
||||||
```
|
```
|
||||||
|
@ -30,6 +30,7 @@ class GetPlayerData:
|
|||||||
if timestamp is not None:
|
if timestamp is not None:
|
||||||
get_args = "?at=" + str(timestamp)
|
get_args = "?at=" + str(timestamp)
|
||||||
|
|
||||||
|
# Build the request path based on the identifier
|
||||||
req = ""
|
req = ""
|
||||||
if is_valid_minecraft_username(identifier):
|
if is_valid_minecraft_username(identifier):
|
||||||
req = "/users/profiles/minecraft/" + identifier + get_args
|
req = "/users/profiles/minecraft/" + identifier + get_args
|
||||||
@ -38,8 +39,9 @@ class GetPlayerData:
|
|||||||
else:
|
else:
|
||||||
self.valid = False
|
self.valid = False
|
||||||
|
|
||||||
|
# Proceed only, when the identifier was valid
|
||||||
if self.valid:
|
if self.valid:
|
||||||
# Request the UUID
|
# Request the player data
|
||||||
http_conn = http.client.HTTPSConnection("api.mojang.com");
|
http_conn = http.client.HTTPSConnection("api.mojang.com");
|
||||||
http_conn.request("GET", req,
|
http_conn.request("GET", req,
|
||||||
headers={'User-Agent':'https://github.com/clerie/mcuuid', 'Content-Type':'application/json'});
|
headers={'User-Agent':'https://github.com/clerie/mcuuid', 'Content-Type':'application/json'});
|
||||||
@ -52,31 +54,34 @@ class GetPlayerData:
|
|||||||
else:
|
else:
|
||||||
# Parse the JSON
|
# Parse the JSON
|
||||||
json_data = json.loads(response)
|
json_data = json.loads(response)
|
||||||
|
|
||||||
|
### Handle the response of the different requests on different ways
|
||||||
|
# Request using username
|
||||||
if is_valid_minecraft_username(identifier):
|
if is_valid_minecraft_username(identifier):
|
||||||
# The UUID
|
# The UUID
|
||||||
self.uuid = json_data['id']
|
self.uuid = json_data['id']
|
||||||
# The username written correctly
|
# The username written correctly
|
||||||
self.username = json_data['name']
|
self.username = json_data['name']
|
||||||
|
#Request using UUID
|
||||||
elif is_valid_mojang_uuid(identifier):
|
elif is_valid_mojang_uuid(identifier):
|
||||||
# The UUID
|
# The UUID
|
||||||
self.uuid = identifier
|
self.uuid = identifier
|
||||||
|
|
||||||
if timestamp is None:
|
|
||||||
timestamp = 0
|
|
||||||
|
|
||||||
current_name = ""
|
current_name = ""
|
||||||
current_time = 0
|
current_time = 0
|
||||||
|
|
||||||
# Getting the current username
|
# Getting the username based on timestamp
|
||||||
for name in json_data:
|
for name in json_data:
|
||||||
if 'changedToAt' in name:
|
# Prepare the JSON
|
||||||
changed_to_at = name['changedToAt']
|
# The first name has no change time
|
||||||
else:
|
if 'changedToAt' not in name:
|
||||||
changed_to_at = 0
|
name['changedToAt'] = 0
|
||||||
|
|
||||||
if changed_to_at <= timestamp and current_time <= changed_to_at:
|
# Get the right name on timestamp
|
||||||
current_time = changed_to_at
|
if current_time <= name['changedToAt'] and (timestamp is None or name['changedToAt'] <= timestamp):
|
||||||
|
current_time = name['changedToAt']
|
||||||
current_name = name['name']
|
current_name = name['name']
|
||||||
|
|
||||||
|
|
||||||
# The username written correctly
|
# The username written correctly
|
||||||
self.username = name['name']
|
self.username = current_name
|
||||||
|
17
test.py
17
test.py
@ -6,23 +6,32 @@ from mcuuid.tools import is_valid_mojang_uuid
|
|||||||
### Import some other necessary modules
|
### Import some other necessary modules
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
### Which username should we use?
|
### Which input method should we use?
|
||||||
# Are there some arguments brought by the console use the first after the filename as username
|
# Are there some arguments brought by the console use the first after the filename as identifier
|
||||||
|
timestamp = None
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
identifier = 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:
|
else:
|
||||||
print("Please enter a username or UUID: ")
|
print("Please enter a username or UUID: ")
|
||||||
identifier = raw_input()
|
identifier = raw_input()
|
||||||
|
|
||||||
|
### Is the identifier a valid value?
|
||||||
if is_valid_minecraft_username(identifier) or is_valid_mojang_uuid(identifier):
|
if is_valid_minecraft_username(identifier) or is_valid_mojang_uuid(identifier):
|
||||||
|
# Print the type of the identifier
|
||||||
if is_valid_minecraft_username(identifier):
|
if is_valid_minecraft_username(identifier):
|
||||||
print('Valid username')
|
print('Valid username')
|
||||||
if is_valid_mojang_uuid(identifier):
|
if is_valid_mojang_uuid(identifier):
|
||||||
print('Valid UUID')
|
print('Valid UUID')
|
||||||
|
|
||||||
### Obtaining the playerinformation using our module
|
### Obtaining the playerinformation using our module
|
||||||
player = GetPlayerData(identifier)
|
player = GetPlayerData(identifier, timestamp)
|
||||||
# Check if the request was valid and the user exists
|
# Check if the request was valid and the user exists
|
||||||
if player.valid is True:
|
if player.valid is True:
|
||||||
# Getting UUID
|
# Getting UUID
|
||||||
|
Loading…
Reference in New Issue
Block a user