Skip to content

Commit

Permalink
Added change_name, upload_skin and reset_skin to UserSession
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucino772 authored and lpalmisa committed Mar 17, 2021
1 parent cc320dc commit ae6a382
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
30 changes: 30 additions & 0 deletions mojang/auth/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from . import yggdrasil
from . import security
from ..user.profile import UserProfile
from ..urls import MINECRAFT_SERVICES, MOJANG_API
from ..user.skin import Skin


class UserSession:
Expand Down Expand Up @@ -60,6 +62,34 @@ def verify(self, answers: list):
def profile(self):
return self.__profile

# Name
def change_name(self, name: str):
url = MINECRAFT_SERVICES.join('minecraft/profile/name/{}'.format(name))
response = self.__session.put(url)

if response.status_code == 204:
self.__profile = UserProfile(self.__session, authenticated=self.secure, load=True)

# Skin
def upload_skin(self, path: str, variant='classic'):
url = MINECRAFT_SERVICES.join('minecraft/profile/skins')
skin = Skin(path, variant=variant)
skin_data = skin.data

files = [
('variant', variant),
('file', (f'image.{skin.extension[1:]}', skin_data, f'image/{skin.extension[1:]}'))
]
response = self.__session.post(url, files=files, headers={'Content-Type': None})
self.__profile = UserProfile(self.__session, authenticated=self.secure, load=True)
return response.status_code == 200

def reset_skin(self):
url = MOJANG_API.join('user/profile/{}/skin'.format(self.profile.uuid))
response = self.__session.delete(url)
self.__profile = UserProfile(self.__session, authenticated=self.secure, load=True)
return response.status_code == 204


def user(username: str, password: str):
return UserSession(username, password)
4 changes: 4 additions & 0 deletions mojang/user/cape.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def __init__(self, filename: str):
self.__filename = filename
self.__extension = None

@property
def extension(self):
return self.__extension

@property
def exists(self):
do_exists = False
Expand Down
8 changes: 7 additions & 1 deletion mojang/user/skin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def __init__(self, filename: str, variant='classic'):
self.__variant = variant
self.__extension = None

@property
def extension(self):
return self.__extension

@property
def variant(self):
return self.__variant
Expand All @@ -33,9 +37,11 @@ def data(self):
if validators.url(self.__filename) == True:
response = requests.get(self.__filename)
if response.ok:
filenames = re.findall('filename=(.+)', response.headers.get('content-disposition'))
filenames = re.findall('filename=(.+)', response.headers.get('content-disposition',''))
if len(filenames) > 0:
self.__extension = os.path.splitext(filenames[0].replace('"','').strip())[1]
else:
self.__extension = '.' + response.headers.get('content-type','').split('/')[1]
image_bytes = response.content
elif os.path.exists(self.__filename):
self.__extension = os.path.splitext(self.__filename)[1]
Expand Down

0 comments on commit ae6a382

Please sign in to comment.