Added some tools
- syntax validator for usernames - syntax vaidator for UUIDs GetPlayerData now accepts UUIDs too
This commit is contained in:
parent
6788cb3944
commit
ff4f5da25c
37
README.md
37
README.md
@ -5,22 +5,41 @@ Getting Minecraft Player Information from Mojang API.
|
|||||||
1. `pip install mcuuid`
|
1. `pip install mcuuid`
|
||||||
2. Use the module like this:
|
2. Use the module like this:
|
||||||
|
|
||||||
|
### API
|
||||||
```
|
```
|
||||||
from mcuuid.api import GetPlayerData
|
from mcuuid.api import GetPlayerData
|
||||||
|
|
||||||
player = GetPlayerData(username)
|
player = GetPlayerData(identifier)
|
||||||
|
|
||||||
if player.valid is True:
|
if player.valid is True:
|
||||||
uuid = player.uuid
|
uuid = player.uuid
|
||||||
name = player.username
|
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"`
|
`uuid` will be `"a2080281c2784181b961d99ed2f3347c"`
|
||||||
and `name` will be `"Gronkh"`
|
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
|
## Test file
|
||||||
Usage
|
Usage
|
||||||
```
|
```
|
||||||
@ -28,10 +47,20 @@ $ python test.py gronkh
|
|||||||
```
|
```
|
||||||
or
|
or
|
||||||
```
|
```
|
||||||
|
$ python test.py a2080281c2784181b961d99ed2f3347c
|
||||||
|
```
|
||||||
|
or
|
||||||
|
```
|
||||||
$ python test.py
|
$ python test.py
|
||||||
Please enter a username:
|
Please enter a username or UUID:
|
||||||
gronkh
|
gronkh
|
||||||
```
|
```
|
||||||
|
or
|
||||||
|
```
|
||||||
|
$ python test.py
|
||||||
|
Please enter a username or UUID:
|
||||||
|
a2080281c2784181b961d99ed2f3347c
|
||||||
|
```
|
||||||
|
|
||||||
Response:
|
Response:
|
||||||
```
|
```
|
||||||
|
@ -7,11 +7,12 @@ Uses the official Mojang API to fetch player data.
|
|||||||
### Import necessary modules
|
### Import necessary modules
|
||||||
import http.client
|
import http.client
|
||||||
import json
|
import json
|
||||||
|
from tools import is_valid_minecraft_username
|
||||||
|
from tools import is_valid_mojang_uuid
|
||||||
|
|
||||||
### Main class
|
### Main class
|
||||||
class GetPlayerData:
|
class GetPlayerData:
|
||||||
def __init__(self, username, timestamp=None):
|
def __init__(self, identifier, timestamp=None):
|
||||||
self.valid = True
|
self.valid = True
|
||||||
"""
|
"""
|
||||||
Get the UUID of the player.
|
Get the UUID of the player.
|
||||||
@ -29,9 +30,18 @@ class GetPlayerData:
|
|||||||
if timestamp is not None:
|
if timestamp is not None:
|
||||||
get_args = "?at=" + str(timestamp)
|
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
|
# Request the UUID
|
||||||
http_conn = http.client.HTTPSConnection("api.mojang.com");
|
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'});
|
headers={'User-Agent':'https://github.com/clerie/mcuuid', 'Content-Type':'application/json'});
|
||||||
response = http_conn.getresponse().read().decode("utf-8")
|
response = http_conn.getresponse().read().decode("utf-8")
|
||||||
|
|
||||||
@ -42,7 +52,31 @@ class GetPlayerData:
|
|||||||
else:
|
else:
|
||||||
# Parse the JSON
|
# Parse the JSON
|
||||||
json_data = json.loads(response)
|
json_data = json.loads(response)
|
||||||
|
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']
|
||||||
|
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
31
mcuuid/tools.py
Normal 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
21
test.py
@ -1,5 +1,7 @@
|
|||||||
### Import our module
|
### Import our module
|
||||||
from mcuuid.api import GetPlayerData
|
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 some other necessary modules
|
||||||
import sys
|
import sys
|
||||||
@ -7,14 +9,20 @@ import sys
|
|||||||
### Which username should we use?
|
### Which username 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 username
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
username = sys.argv[1]
|
identifier = sys.argv[1]
|
||||||
# Else, ask for a username by userinput
|
# Else, ask for a username by userinput
|
||||||
else:
|
else:
|
||||||
print("Please enter a username: ")
|
print("Please enter a username or UUID: ")
|
||||||
username = raw_input()
|
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
|
### Obtaining the playerinformation using our module
|
||||||
player = GetPlayerData(username)
|
player = GetPlayerData(identifier)
|
||||||
# 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
|
||||||
@ -25,6 +33,11 @@ if player.valid is True:
|
|||||||
# Print everything
|
# Print everything
|
||||||
print('UUID: ' + uuid)
|
print('UUID: ' + uuid)
|
||||||
print('correct name: ' + name)
|
print('correct name: ' + name)
|
||||||
|
|
||||||
|
|
||||||
# Error message
|
# Error message
|
||||||
else:
|
else:
|
||||||
print("That player was not found.")
|
print("That player was not found.")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print('identifier is not valid')
|
||||||
|
Loading…
Reference in New Issue
Block a user