Skip to content

Commit

Permalink
Added Skin and Cape class
Browse files Browse the repository at this point in the history
  • Loading branch information
lpalmisa committed Mar 16, 2021
1 parent d562b9a commit 849532b
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 15 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ verify_ssl = true

[packages]
requests = "*"
validators = "*"

[requires]
python_version = "3.8"
25 changes: 24 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions pymojang/user/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from urllib.parse import urljoin
from base64 import urlsafe_b64decode
from .profile import UserProfile
from .skin import Skin
from .cape import Cape

MOJANG_STATUS_URL = 'https://status.mojang.com/check'
MOJANG_API_URL = 'https://api.mojang.com'
Expand Down Expand Up @@ -97,13 +99,8 @@ def get_profile(player_id: str):
for d in data['properties']:
textures = json.loads(urlsafe_b64decode(d['value']))['textures']
if 'SKIN' in textures.keys():
profile.skins = [{
'url': textures['SKIN']['url'],
'variant': textures['SKIN'].get('metadata',{}).get('model','classic')
}]
profile.skins.append(Skin(textures['SKIN']['url'], textures['SKIN'].get('metadata',{}).get('model','classic')))
if 'CAPE' in textures.keys():
profile.capes = [{
'url': textures['CAPE']['url']
}]
profile.capes.append(Cape(textures['CAPE']['url']))

return profile
48 changes: 48 additions & 0 deletions pymojang/user/cape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import requests
import validators
import os
import re

class Cape:

def __init__(self, filename: str):
self.__filename = filename
self.__extension = None

@property
def exists(self):
do_exists = False

if validators.url(self.__filename) == True:
response = requests.head(self.__filename, allow_redirects=True)
if response.status_code == 200:
do_exists = True
elif os.path.exists(self.__filename):
do_exists = True

return do_exists

@property
def data(self):
image_bytes = None
if validators.url(self.__filename) == True:
response = requests.get(self.__filename)
if response.ok:
filenames = re.findall('filename=(.+)', response.headers.get('content-disposition'))
if len(filenames) > 0:
self.__extension = os.path.splitext(filenames[0].replace('"','').strip())[1]
image_bytes = response.content
elif os.path.exists(self.__filename):
self.__extension = os.path.splitext(self.__filename)[1]
with open(self.__filename,'rb') as fp:
image_bytes = fp.read()

return image_bytes

def to_file(self, filename: str):
file_data = self.data
file_path = filename + self.__extension

if file_data is not None and self.__extension is not None:
with open(file_path, 'wb') as fp:
fp.write(file_data)
11 changes: 4 additions & 7 deletions pymojang/user/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from .profile import UserProfile
from ..auth import Yggdrasil, SecurityCheck
from ..utils import TokenPair
from .skin import Skin
from .cape import Cape

class UserSession:

Expand Down Expand Up @@ -87,14 +89,9 @@ def _get_profile(self):
self._profile.name = data['name']

for skin in data['skins']:
self._profile.skins.append({
'url': skin['url'],
'variant': skin['variant'].lower()
})
self._profile.skins.append(Skin(skin['url'], skin['variant'].lower()))

for cape in data['capes']:
self._profile.capes.append({
'url': cape['url']
})
self._profile.capes.append(Cape(cape['url']))
else:
pass
53 changes: 53 additions & 0 deletions pymojang/user/skin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import requests
import validators
import os
import re

class Skin:

def __init__(self, filename: str, variant='classic'):
self.__filename = filename
self.__variant = variant
self.__extension = None

@property
def variant(self):
return self.__variant

@property
def exists(self):
do_exists = False

if validators.url(self.__filename) == True:
response = requests.get(self.__filename)
if response.status_code == 200:
do_exists = True
elif os.path.exists(self.__filename):
do_exists = True

return do_exists

@property
def data(self):
image_bytes = None
if validators.url(self.__filename) == True:
response = requests.get(self.__filename)
if response.ok:
filenames = re.findall('filename=(.+)', response.headers.get('content-disposition'))
if len(filenames) > 0:
self.__extension = os.path.splitext(filenames[0].replace('"','').strip())[1]
image_bytes = response.content
elif os.path.exists(self.__filename):
self.__extension = os.path.splitext(self.__filename)[1]
with open(self.__filename,'rb') as fp:
image_bytes = fp.read()

return image_bytes

def to_file(self, filename: str):
file_data = self.data
file_path = filename + self.__extension

if file_data is not None and self.__extension is not None:
with open(file_path, 'wb') as fp:
fp.write(file_data)

0 comments on commit 849532b

Please sign in to comment.