-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mojang.api.base moved to mojang.account.base, get_profile was removed
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import datetime as dt | ||
from typing import NamedTuple, Tuple, Union | ||
|
||
# Status check | ||
class ServiceStatus(NamedTuple): | ||
name: str | ||
status: str | ||
|
||
class StatusCheck(Tuple[ServiceStatus]): | ||
|
||
def get(self, name: str) -> Union[None, ServiceStatus]: | ||
service = list(filter(lambda s: s.name == name, self)) | ||
if len(service) > 0: | ||
return service[0] | ||
|
||
# UUID and Name | ||
class UUIDInfo(NamedTuple): | ||
name: str | ||
uuid: str | ||
legacy: bool = False | ||
demo: bool = False | ||
|
||
class NameInfo(NamedTuple): | ||
name: str | ||
changed_to_at: dt.datetime | ||
|
||
class NameInfoList(Tuple[NameInfo]): | ||
|
||
@property | ||
def current(self) -> NameInfo: | ||
if len(self) == 1: | ||
return self[0] | ||
|
||
_list = filter(lambda n: n.change_to_at != None, self) | ||
return max(_list, key=lambda n: n.change_to_at) | ||
|
||
@property | ||
def first(self) -> Union[None, NameInfo]: | ||
first = list(filter(lambda n: n.changed_to_at == None, self)) | ||
if len(first) > 0: | ||
return first[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
class URLs: | ||
|
||
@classmethod | ||
def status_check(cls): | ||
return 'https://status.mojang.com/check' | ||
|
||
@classmethod | ||
def uuid(cls, username: str): | ||
return f'https://api.mojang.com/users/profiles/minecraft/{username}' | ||
|
||
@classmethod | ||
def uuids(cls): | ||
return 'https://api.mojang.com/profiles/minecraft' | ||
|
||
@classmethod | ||
def name_history(cls, uuid: str): | ||
return f'https://api.mojang.com/user/profiles/{uuid}/names' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import requests | ||
|
||
from ._urls import URLs | ||
from ._structures import * | ||
|
||
# TODO: Handle errors and exception | ||
|
||
def status(): | ||
response = requests.get(URLs.status_check()) | ||
|
||
_status = [] | ||
for service in response.json(): | ||
item = list(service.items())[0] | ||
_status.append(ServiceStatus(name=item[0], status=item[1])) | ||
|
||
return StatusCheck(_status) | ||
|
||
def get_uuid(username: str): | ||
response = requests.get(URLs.uuid(username)) | ||
|
||
data = response.json() | ||
data['uuid'] = data.pop('id') | ||
|
||
return UUIDInfo(**data) | ||
|
||
def get_uuids(usernames: list): | ||
usernames = list(map(lambda u: u.lower(), usernames)) | ||
_uuids = [None]*len(usernames) | ||
|
||
for i in range(0, len(usernames), 10): | ||
response = requests.post(URLs.uuids(), json=usernames[i:i+10]) | ||
|
||
for item in response.json(): | ||
index = usernames.index(item['name'].lower()) | ||
item['uuid'] = item.pop('id') | ||
_uuids[index] = UUIDInfo(**item) | ||
|
||
return _uuids | ||
|
||
def names(uuid: str): | ||
response = requests.get(URLs.name_history(uuid)) | ||
|
||
_names = [] | ||
for item in response.json(): | ||
changed_to_at = None | ||
if 'changedToAt' in item.keys(): | ||
changed_to_at = dt.datetime.fromtimestamp(item['changedToAt'] / 1000) | ||
_names.append(NameInfo(name=item['name'], changed_to_at=changed_to_at)) | ||
|
||
return NameInfoList(_names) |