From f799d1d71461ac34baa9f6ea3be9d3e09c18fa27 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Sun, 15 Nov 2020 05:49:06 +0100 Subject: [PATCH] Completed request constructor Getting cryptic error message from endpoint "Minimum width and height exceeded. The minimum size is 22x17." Testing suggests it has something with the binary encoding of the included file to do. Needs investigation --- .gitignore | 3 +- 2.png | Bin 0 -> 239 bytes labylib/Cape.py | 97 +++++++++++++++++++++++++++++++ lib.py | 5 ++ animated-textures.py => start.py | 2 +- labylib/__init__.py => test.txt | 0 6 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 2.png create mode 100644 labylib/Cape.py create mode 100644 lib.py rename animated-textures.py => start.py (98%) rename labylib/__init__.py => test.txt (100%) diff --git a/.gitignore b/.gitignore index d3a3232..fc77936 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -animated-textures/* \ No newline at end of file +animated-textures/* +*__pycache__ \ No newline at end of file diff --git a/2.png b/2.png new file mode 100644 index 0000000000000000000000000000000000000000..5d92343e7e4c5ed96cfdac10a60c7295e0572783 GIT binary patch literal 239 zcmeAS@N?(olHy`uVBq!ia0y~yU`z&LVJ2pv$h^M(%|MDLz$e7jy};+o7X~J#U;qFA zPkm6f0w~T{666=m;PC858jv&5)5S5Q;?~>i8+jWHcw8O%J7)@>n4lsQ(xD~DyE8oM z*LCZ2*Dl_i7ykNj+$>2)waF)+2>#41jbl1D%FX42Xq{Rr>mdKI;Vst01eq(Pyhe` literal 0 HcmV?d00001 diff --git a/labylib/Cape.py b/labylib/Cape.py new file mode 100644 index 0000000..94931ac --- /dev/null +++ b/labylib/Cape.py @@ -0,0 +1,97 @@ +import requests +import hashlib +import time + +class BadRequestError(Exception): pass + +class Cape: + + endpoint = "https://www.labymod.net/page/php/cape.php" + + def __init__(self,cookie,img): + self.validate(cookie,img) + + self.body = "" # 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.addFormData("cosmetic","cape") + self.addFormData("file",self.bOpen(img)) + self.closeFormData() + + # ----------------------------------- + + 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 = str(f.read()) + f.close() + + length = len(content) - 1 + content = content[2:length] + + return content + + # Append form-data to request body and boundary header + def addFormData(self,name,payload): + body = contentType = "" + eol = "\r\n" + + disposition = f'name="{name}"' + if(name == "file"): + contentType = "Content-Type: image/png" + eol + + # Use current epoch as filename. It has to be different from last request + filename = str(round(time.time())) + ".png" + disposition += f'; filename="{filename}"' + + body += f"--{self.boundary}" + eol # Init data header + body += f"Content-Disposition: form-data; {disposition}" + eol + body += contentType + eol + eol + body += payload + eol + + self.body += body + + # Last form-data has been set, add final post width for boundary header + def closeFormData(self): + self.body += f"--{self.boundary}--\r\n\r\n" + + # ----------------------------------- + + def update(self): + request = requests.post(Cape.endpoint, + headers = self.headers, + cookies = self.cookies, + data = self.body + ) + + if(request.text != "OK"): + raise BadRequestError(request.text) \ No newline at end of file diff --git a/lib.py b/lib.py new file mode 100644 index 0000000..9bfc8c8 --- /dev/null +++ b/lib.py @@ -0,0 +1,5 @@ +from labylib import Cape + +cape = Cape.Cape("76eppb9t0vg3saftu42vf1e223","2.png") +cape.update() +print(cape.body) \ No newline at end of file diff --git a/animated-textures.py b/start.py similarity index 98% rename from animated-textures.py rename to start.py index 860d356..0d16e6b 100644 --- a/animated-textures.py +++ b/start.py @@ -3,7 +3,7 @@ import json import importlib from pathlib import Path -# from labylib import Cape +from labylib import Cape # Don't forget to reflect in .gitignore if you change this name = "animated-textures" diff --git a/labylib/__init__.py b/test.txt similarity index 100% rename from labylib/__init__.py rename to test.txt