Skip to content

Commit

Permalink
mojang.api.base moved to mojang.account.base, get_profile was removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucino772 committed Apr 27, 2021
1 parent 883434d commit 74cef82
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
41 changes: 41 additions & 0 deletions mojang/account/_structures.py
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]
18 changes: 18 additions & 0 deletions mojang/account/_urls.py
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'
50 changes: 50 additions & 0 deletions mojang/account/base.py
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)

0 comments on commit 74cef82

Please sign in to comment.