Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
6c134927d5 | |||
6cac703c0e | |||
8fb708f0fa | |||
ff4f5da25c | |||
6788cb3944 | |||
2c1b1491e0 | |||
2720991a12 |
89
README.md
89
README.md
@@ -2,25 +2,92 @@
|
||||
Getting Minecraft Player Information from Mojang API.
|
||||
|
||||
## Usage
|
||||
1. Add the mcuuid.py file to your Python path.
|
||||
1. `pip install mcuuid`
|
||||
2. Use the module like this:
|
||||
|
||||
### API
|
||||
```
|
||||
from mcuuid import GetPlayerData
|
||||
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"`
|
||||
|
||||
|
||||
```
|
||||
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
|
||||
```
|
||||
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')
|
||||
```
|
||||
|
||||
## Documentation
|
||||
### Module `mcuuid.api`
|
||||
#### Class `GetPlayerData(indentifier, timestamp=None)`
|
||||
##### Parameters
|
||||
- `identifier`: string - a Minecraft username or Mojang UUID
|
||||
- `timestamp`: int - a unix timestamp
|
||||
|
||||
##### Returns
|
||||
`player` object
|
||||
|
||||
#### Object `player`
|
||||
##### Value `player.uuid`
|
||||
Mojang UUID
|
||||
|
||||
##### Value `player.username`
|
||||
Minecraft username
|
||||
|
||||
### Module `mcuuid.tools`
|
||||
#### Function `is_valid_minecraft_username(username)`
|
||||
##### Parameters
|
||||
- `username`: string - a Minecraft username
|
||||
|
||||
##### Returns
|
||||
`True` or `False`
|
||||
|
||||
#### Function `is_valid_mojang_uuid(uuid)`
|
||||
##### Parameters
|
||||
- `uuid`: string - a Mojang UUID
|
||||
|
||||
##### Returns
|
||||
`True` or `False`
|
||||
|
||||
#### Function `cleanup_uuid(uuid)`
|
||||
##### Parameters
|
||||
- `uuid`: string - a Mojang UUID
|
||||
|
||||
##### Returns
|
||||
`uuid` as string, lowered. Without dashes
|
||||
|
||||
## Test file
|
||||
Usage
|
||||
```
|
||||
@@ -28,10 +95,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:
|
||||
```
|
||||
|
87
mcuuid/api.py
Normal file
87
mcuuid/api.py
Normal file
@@ -0,0 +1,87 @@
|
||||
""" Username to UUID
|
||||
Converts a Minecraft username to it's UUID equivalent.
|
||||
|
||||
Uses the official Mojang API to fetch player data.
|
||||
"""
|
||||
|
||||
### Import necessary modules
|
||||
import http.client
|
||||
import json
|
||||
from mcuuid.tools import is_valid_minecraft_username
|
||||
from mcuuid.tools import is_valid_mojang_uuid
|
||||
|
||||
### Main class
|
||||
class GetPlayerData:
|
||||
def __init__(self, identifier, timestamp=None):
|
||||
self.valid = True
|
||||
"""
|
||||
Get the UUID of the player.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
username: string
|
||||
The known minecraft username
|
||||
timestamp : long integer (optional)
|
||||
The time at which the player used this name, expressed as a Unix timestamp.
|
||||
"""
|
||||
|
||||
# Handle the timestamp
|
||||
get_args = ""
|
||||
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
|
||||
elif is_valid_mojang_uuid(identifier):
|
||||
req = "/user/profiles/" + identifier + "/names" + get_args
|
||||
else:
|
||||
self.valid = False
|
||||
|
||||
# Proceed only, when the identifier was valid
|
||||
if self.valid:
|
||||
# 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'});
|
||||
response = http_conn.getresponse().read().decode("utf-8")
|
||||
|
||||
# In case the answer is empty, the user dont exist
|
||||
if not response:
|
||||
self.valid = False
|
||||
# If there is an answer, fill out the variables
|
||||
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
|
||||
|
||||
current_name = ""
|
||||
current_time = 0
|
||||
|
||||
# Getting the username based on timestamp
|
||||
for name in json_data:
|
||||
# Prepare the JSON
|
||||
# The first name has no change time
|
||||
if 'changedToAt' not in name:
|
||||
name['changedToAt'] = 0
|
||||
|
||||
# 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 = current_name
|
@@ -1,48 +0,0 @@
|
||||
""" Username to UUID
|
||||
Converts a Minecraft username to it's UUID equivalent.
|
||||
|
||||
Uses the official Mojang API to fetch player data.
|
||||
"""
|
||||
|
||||
### Import necessary modules
|
||||
import http.client
|
||||
import json
|
||||
|
||||
|
||||
### Main class
|
||||
class GetPlayerData:
|
||||
def __init__(self, username, timestamp=None):
|
||||
self.valid = True
|
||||
"""
|
||||
Get the UUID of the player.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
username: string
|
||||
The known minecraft username
|
||||
timestamp : long integer (optional)
|
||||
The time at which the player used this name, expressed as a Unix timestamp.
|
||||
"""
|
||||
|
||||
# Handle the timestamp
|
||||
get_args = ""
|
||||
if timestamp is not None:
|
||||
get_args = "?at=" + str(timestamp)
|
||||
|
||||
# Request the UUID
|
||||
http_conn = http.client.HTTPSConnection("api.mojang.com");
|
||||
http_conn.request("GET", "/users/profiles/minecraft/" + username + get_args,
|
||||
headers={'User-Agent':'https://github.com/clerie/mcuuid', 'Content-Type':'application/json'});
|
||||
response = http_conn.getresponse().read().decode("utf-8")
|
||||
|
||||
# In case the answer is empty, the user dont exist
|
||||
if not response:
|
||||
self.valid = False
|
||||
# If there is an answer, fill out the variables
|
||||
else:
|
||||
# Parse the JSON
|
||||
json_data = json.loads(response)
|
||||
# The UUID
|
||||
self.uuid = json_data['id']
|
||||
# The username written correctly
|
||||
self.username = json_data['name']
|
36
mcuuid/tools.py
Normal file
36
mcuuid/tools.py
Normal file
@@ -0,0 +1,36 @@
|
||||
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
|
||||
|
||||
def cleanup_uuid(uuid):
|
||||
uuid = uuid.lower()
|
||||
uuid = uuid.replace('-', '')
|
||||
return uuid
|
3
setup.py
3
setup.py
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
|
||||
|
||||
setuptools.setup(
|
||||
name="mcuuid",
|
||||
version="0.0.1",
|
||||
version="0.3",
|
||||
author="Clemens Riese",
|
||||
author_email="hallo@clerie.de",
|
||||
description="Getting Minecraft Player Information from Mojang API.",
|
||||
@@ -15,7 +15,6 @@ setuptools.setup(
|
||||
packages=setuptools.find_packages(),
|
||||
classifiers=[
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 2",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
],
|
||||
|
64
test.py
64
test.py
@@ -1,30 +1,52 @@
|
||||
### Import our module
|
||||
from mcuuid.mcuuid 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 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:
|
||||
username = sys.argv[1]
|
||||
# Else, ask for a username by userinput
|
||||
else:
|
||||
print("Please enter a username: ")
|
||||
username = raw_input()
|
||||
identifier = sys.argv[1]
|
||||
|
||||
### Obtaining the playerinformation using our module
|
||||
player = GetPlayerData(username)
|
||||
# Check if the request was valid and the user exists
|
||||
if player.valid is True:
|
||||
# Getting UUID
|
||||
uuid = player.uuid
|
||||
# Getting real Username
|
||||
name = player.username
|
||||
if len(sys.argv) > 2:
|
||||
if sys.argv[2].isdigit():
|
||||
timestamp = int(sys.argv[2])
|
||||
|
||||
# Print everything
|
||||
print('UUID: ' + uuid)
|
||||
print('correct name: ' + name)
|
||||
# Error message
|
||||
|
||||
# Else, ask for a identifier by userinput
|
||||
else:
|
||||
print("That player was not found.")
|
||||
print("Please enter a username or UUID: ")
|
||||
identifier = 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, timestamp)
|
||||
# Check if the request was valid and the user exists
|
||||
if player.valid is True:
|
||||
# Getting UUID
|
||||
uuid = player.uuid
|
||||
# Getting real Username
|
||||
name = player.username
|
||||
|
||||
# Print everything
|
||||
print('UUID: ' + uuid)
|
||||
print('correct name: ' + name)
|
||||
|
||||
|
||||
# Error message
|
||||
else:
|
||||
print("That player was not found.")
|
||||
|
||||
else:
|
||||
print('identifier is not valid')
|
||||
|
Reference in New Issue
Block a user