-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
167 lines (148 loc) · 4.14 KB
/
api.py
File metadata and controls
167 lines (148 loc) · 4.14 KB
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
# Engine Tribe API wrapper
from aiohttp import request
from config import *
from models import (
ServerStats
)
async def user_register(
username: str,
im_id: int,
password_hash: str,
):
async with request(
method='POST',
url=API_HOST + '/user/register',
data={
'username': username,
'password_hash': password_hash,
'im_id': im_id,
'api_key': API_KEY
},
headers={
'User-Agent': 'EngineBot'
}
) as response:
return await response.json()
async def update_password(
user_identifier: str | int,
password_hash: str,
im_id: int,
):
async with request(
method='POST',
url=API_HOST + f'/user/{user_identifier}/update_password',
data={
'password_hash': password_hash,
'api_key': API_KEY,
'im_id': im_id
},
headers={
'User-Agent': 'EngineBot'
}
) as response:
return await response.json()
async def user_info(
user_identifier: str | int
):
async with request(
method='POST',
url=API_HOST + f'/user/{user_identifier}/info',
headers={
'User-Agent': 'EngineBot'
}
) as response:
return await response.json()
async def login_session(
token: str
) -> str:
async with request(
method='POST',
url=API_HOST + '/user/login',
data={
'token': token,
'alias': 'EngineBot',
'password': '0'
},
headers={
'User-Agent': 'EngineBot'
}
) as response:
try:
auth_code = (await response.json())['auth_code']
except KeyError:
raise Exception(await response.text())
return auth_code
async def get_user_levels(
username: str,
auth_code: str,
rows_perpage: int = 10
) -> list[dict[str, str]]:
async with request(
method='POST',
url=API_HOST + '/stages/detailed_search',
data={
'auth_code': auth_code,
'rows_perpage': rows_perpage,
'author': username
},
headers={
'User-Agent': 'EngineBot'
}
) as response:
response_json = await response.json()
return response_json['result'] if 'result' in response_json else []
async def server_stats() -> ServerStats:
async with request(
method='GET',
url=API_HOST + '/server_stats',
headers={
'User-Agent': 'EngineBot'
}
) as response:
return ServerStats.parse_obj(await response.json())
async def update_permission(
user_identifier: str,
permission: str,
value: bool
):
async with request(
method='POST',
url=API_HOST + f'/user/{user_identifier}/permission/{permission}',
data={
'api_key': API_KEY,
'value': value
},
headers={
'User-Agent': 'EngineBot'
}
) as response:
return await response.json()
async def random_level(
auth_code: str,
difficulty: str | None = None
):
data = {'dificultad': difficulty, 'auth_code': auth_code} if difficulty is not None else {'auth_code': auth_code}
async with request(
method='POST',
url=API_HOST + '/stage/random',
data=data,
headers={
'User-Agent': 'EngineBot'
}
) as response:
return await response.json()
async def query_level(
auth_code: str,
level_id: str
):
async with request(
method='POST',
url=API_HOST + f'/stage/{level_id}',
data={
'auth_code': auth_code
},
headers={
'User-Agent': 'EngineBot'
}
) as response:
return await response.json()