Skip to content

Commit

Permalink
Merge pull request #1 from VictorWesterlund/feature/labylib-core
Browse files Browse the repository at this point in the history
Pre-release 0.1.0
  • Loading branch information
VictorWesterlund authored Nov 15, 2020
2 parents f8c8071 + 566ccc4 commit 488c64a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*__pycache__
98 changes: 98 additions & 0 deletions labylib/Cape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import requests
import hashlib
import time

class RequestError(Exception): pass

class Texture:

endpoint = "https://www.labymod.net/page/php/cape.php"

def __init__(self,cookie,img):
self.validate(cookie,img)

self.body = b"" # Initialize request body
self.cookies = dict(PHPSESSID = cookie)
self.boundary = self.boundary()

self.headers = {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9,sv;q=0.8",
"cache-control": "no-cache",
"dnt": "1",
"user-agent": "Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0",
"origin": "https://www.labymod.net",
"pragma": "no-cache",
"referer": "https://www.labymod.net/dashboard",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
"Content-Type": "multipart/form-data; boundary=" + self.boundary
}

self.appendBinaryFormData(b"cosmetic",b"cape")
self.appendBinaryFormData(b"file",self.bOpen(img))

# -----------------------------------

def validate(self,cookie,file):
return True

# Generate boundary header from MD5-hash of current time
def boundary(self):
seed = str(time.time())
md5 = hashlib.md5(seed.encode("utf-8"))

boundary = "----WebKitFormBoundary" + md5.hexdigest()
return boundary

# Open and return file binary as string
def bOpen(self,file):
f = open(file,"rb")
content = f.read()
f.close()

return content

# Append form-data to request body and boundary header
def appendBinaryFormData(self,name,payload):
body = contentType = b""
eol = b"\r\n"

disposition = b'name="' + name + b'"'
if(name == b"file"):
contentType = b"Content-Type: image/png" + eol

# Use current epoch as filename. It has to be different from last request
filename = str(round(time.time())) + ".png"
filename = filename.encode()
disposition += b'; filename="' + filename + b'"'

body += b"--" + self.boundary.encode() + eol # Init data header
body += b"Content-Disposition: form-data; " + disposition + eol
body += contentType + eol
body += payload + eol

self.body += body

# Last form-data has been set, add final post width for boundary header
def closeBinaryFormData(self):
self.body += b"--" + self.boundary.encode() + b"--\r\n\r\n"

# -----------------------------------

def update(self):
self.closeBinaryFormData() # Add final boundary header

request = requests.post(Texture.endpoint,
headers = self.headers,
cookies = self.cookies,
data = self.body
)

# Raise exception if request fails
# Use [3:5] to clean up junk chars from reponse body
if(str(request.text)[3:5] != "OK"):
raise RequestError(str(request.text))

0 comments on commit 488c64a

Please sign in to comment.