Added some tools

- syntax validator for usernames
- syntax vaidator for UUIDs

GetPlayerData now accepts UUIDs too
This commit is contained in:
clerie 2018-10-28 15:07:58 +01:00
parent 6788cb3944
commit ff4f5da25c
4 changed files with 145 additions and 38 deletions

View File

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

View File

@ -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,9 +30,18 @@ class GetPlayerData:
if timestamp is not None:
get_args = "?at=" + str(timestamp)
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:
self.valid = False
if self.valid:
# Request the UUID
http_conn = http.client.HTTPSConnection("api.mojang.com");
http_conn.request("GET", "/users/profiles/minecraft/" + username + get_args,
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")
@ -42,7 +52,31 @@ class GetPlayerData:
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']

31
mcuuid/tools.py Normal file
View File

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

21
test.py
View File

@ -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,14 +9,20 @@ 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()
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(username)
player = GetPlayerData(identifier)
# Check if the request was valid and the user exists
if player.valid is True:
# Getting UUID
@ -25,6 +33,11 @@ if player.valid is True:
# Print everything
print('UUID: ' + uuid)
print('correct name: ' + name)
# Error message
else:
print("That player was not found.")
else:
print('identifier is not valid')