This repository was archived by the owner on May 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecape_api.py
206 lines (145 loc) · 6.36 KB
/
recape_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
import json
import requests
import sys
import shutil
DEBUG = os.path.exists(".debug")
VERSION = "1.0"
RECAPE_URL = "https://localhost" if False else "https://recape-server.boyne.dev" #TODO: Change back to debug
# TODO: Implement
def restart():
#page.window_destroy()
os.execl(sys.executable, sys.executable, *sys.argv)
class API:
def read_credentials():
folder = os.path.expanduser("~") + "/.recape"
try:
os.mkdir(folder)
except FileExistsError:
pass
try:
with open(folder + "/auth.json", "r") as auth:
data = auth.read()
data = json.loads(data)
if not "uuid" in data.keys() or not "token" in data.keys():
return None
return data
except Exception as e:
return None
def save_credentials(token, uuid):
folder = os.path.expanduser("~")
folder += "/.recape"
try:
os.mkdir(folder)
except FileExistsError:
pass
with open(folder + "/auth.json", "w+") as auth:
auth.write(json.dumps({
"token": token,
"uuid": uuid
}))
# TODO: Refactor login attemptors to have the same success sequence to remove redundancy
def attempt_login_ms(username, ms_email, ms_password):
result = requests.get(RECAPE_URL + "/authenticate/ms_login", headers={"email": ms_email, "password": ms_password, "username": username, "source": "ReCape Client " + VERSION})
result = result.json()
if result["status"] == "failure":
return result["error"]
elif result["status"] == "success":
API.save_credentials(result["token"], result["uuid"])
return ["Successfully authenticated your account! Welcome to ReCape.", True]
def attempt_login_code(username, code):
result = requests.get(RECAPE_URL + "/authenticate/server_code", headers={"code": code, "username": username, "source": "ReCape Client " + VERSION}, verify=not DEBUG)
try:
result = result.json()
except requests.exceptions.JSONDecodeError:
return result.text
if result["status"] == "failure":
return result["error"]
elif result["status"] == "success":
API.save_credentials(result["token"], result["uuid"])
return ["Successfully authenticated your account! Welcome to ReCape.", True]
def delete_credentials():
folder = os.path.expanduser("~")
folder += "/.recape"
try:
shutil.rmtree(folder)
return ["Login credentials deleted!", True]
except FileNotFoundError:
return "There were no credentials to delete."
def verify_credentials(token, uuid, username):
try:
result = requests.get(RECAPE_URL + "/authenticate/check_token", headers={"token": token, "uuid": uuid, "username": username}, verify=not DEBUG)
except requests.exceptions.ConnectionError:
return None
result = result.json()
return result["result"] == "valid"
def set_cape(friendly, cape):
credentials = API.read_credentials()
files = {}
cape_type = "custom"
if not cape.endswith(".png"):
cape_type = cape
else:
files = {"file": open(cape, "rb")}
result = requests.post(RECAPE_URL + "/account/set_cape", headers={"uuid": credentials["uuid"], "token": credentials["token"], "cape_type": cape_type}, files=files, verify=not DEBUG)
result = result.json()
if result["status"] == "success":
return "Successfully set cape to: " + friendly
elif result["status"] == "failure":
return result["error"]
else:
return "*shrugs*"
def get_config():
credentials = API.read_credentials()
if credentials == None:
return False
try:
result = requests.get(RECAPE_URL + "/account/get_config", headers={"uuid": credentials["uuid"], "token": credentials["token"]}, verify=not DEBUG)
except requests.exceptions.ConnectionError:
return False
print(result.text)
result = result.json()
if not "status" in result.keys():
return result
if result["status"] == "failure":
return False
else:
return result
def get_cosmetic_list():
credentials = API.read_credentials()
if credentials == None:
return False
try:
result = requests.get(RECAPE_URL + "/account/get_cosmetic_list", headers={"uuid": credentials["uuid"], "token": credentials["token"]}, verify=not DEBUG)
except requests.exceptions.ConnectionError:
return {}
result = result.json()
print(result)
if result["status"] == 'failure':
return {}
return result["models"]
def set_models(models):
credentials = API.read_credentials()
result = requests.post(RECAPE_URL + "/account/set_config", headers={"uuid": credentials["uuid"], "token": credentials["token"], "config": json.dumps(models)}, verify=not DEBUG)
result = result.json()
if result["status"] == "success":
return "Successfully changed your models!"
elif result["status"] == "failure":
return result["error"]
else:
return "*shrugs*"
def upload_model(model_path, texture_path):
credentials = API.read_credentials()
files = {}
files = {"model": open(model_path, "rb"), "texture": open(texture_path, "rb")}
try:
result = requests.post(RECAPE_URL + "/account/upload_cosmetic", headers={"uuid": credentials["uuid"], "token": credentials["token"]}, files=files, verify=not DEBUG)
result = result.json()
if result["status"] == "success":
return "Successfully uploaded the model files!"
elif result["status"] == "failure":
return result["error"]
else:
return "*shrugs*" # ignorance is bliss
except Exception as e:
return "An error occurred, and your model could not be uploaded. Check the Discord for downtime information. The error is: " + str(e)