-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtgrcode_api.py
268 lines (229 loc) · 8.2 KB
/
tgrcode_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from dataclasses import dataclass
import requests
from config import TGRCODE_API, VERSION
USER_AGENT = f'SMM2Helper/{VERSION} HiddenSuperStar/0.0.4'
__all__ = ['TGRCodeAPIBaseException', 'TGRCodeAPIException', 'TGRCodeAPICourseIDException',
'Maker', 'Course',
'prettify_course_id', 'normalize_course_id',
'search_popular', 'search_endless_mode',
'user_info', 'level_info', 'level_data_dataid',
'super_world', 'get_posted']
class TGRCodeAPIBaseException(BaseException):
pass
class TGRCodeAPIException(TGRCodeAPIBaseException):
pass
class TGRCodeAPICourseIDException(TGRCodeAPIBaseException):
pass
@dataclass
class Maker:
name: str
region: str
maker_id: str
country: str
last_active: str
mii_image_url: str
pose_name: str
hat_name: str
shirt_name: str
pants_name: str
courses_played: int
courses_attempted: int
courses_cleared: int
courses_deaths: int
likes: int
maker_points: int
easy_highscore: int
normal_highscore: int
expert_highscore: int
super_expert_highscore: int
versus_rating: int
versus_rank: str
versus_plays: int
versus_won: int
versus_lost: int
versus_disconnected: int
coop_clears: int
coop_plays: int
versus_kills: int
versus_killed_by_others: int
uploaded_levels: int
first_clears: int
world_records: int
super_world_clears: int
super_world_id: str
@dataclass
class Course:
name: str
description: str
uploaded_date: str
data_id: str
course_id: str
game_style: str
theme: str
difficulty: str
tag_1: str
tag_2: str
world_record: str
upload_time: str
clears: int
attempts: int
clear_rate: str
likes: int
boos: int
maker: Maker
record_holder: Maker
def prettify_course_id(course_id: str) -> str:
course_id = course_id.strip()
return f'{course_id[0:3]}-{course_id[3:6]}-{course_id[6:9]}'.upper()
def normalize_course_id(course_id: str) -> str:
ret: str = course_id.translate(dict.fromkeys(map(ord, '-_ '), None)).upper()
if len(ret) != 9:
raise TGRCodeAPICourseIDException('Invalid course ID.')
return ret
def deserialize_course(course: dict) -> Course:
if 'world_record_pretty' not in course:
course['world_record_pretty'] = '0'
return Course(
name=course['name'],
description=course['description'],
uploaded_date=course['uploaded_pretty'],
course_id=course['course_id'],
data_id=course['data_id'],
game_style=course['game_style_name'],
theme=course['theme_name'],
difficulty=course['difficulty_name'],
tag_1=course['tags_name'][0],
tag_2=course['tags_name'][1],
world_record=course['world_record_pretty'],
upload_time=course['upload_time_pretty'],
clears=course['clears'],
attempts=course['attempts'],
clear_rate=course['clear_rate_pretty'],
likes=course['likes'],
boos=course['boos'],
maker=deserialize_maker(course['uploader']),
record_holder=deserialize_maker(course['record_holder'])
)
def deserialize_maker(maker: dict) -> Maker:
return Maker(
region=maker['region_name'],
maker_id=maker['code'],
name=maker['name'],
country=maker['country'],
last_active=maker['last_active_pretty'],
mii_image_url=maker['mii_image'],
pose_name=maker['pose_name'],
hat_name=maker['hat_name'],
shirt_name=maker['shirt_name'],
pants_name=maker['pants_name'],
courses_attempted=maker['courses_attempted'],
courses_played=maker['courses_played'],
courses_cleared=maker['courses_cleared'],
courses_deaths=maker['courses_deaths'],
likes=maker['likes'],
maker_points=maker['maker_points'],
easy_highscore=maker['easy_highscore'],
normal_highscore=maker['normal_highscore'],
expert_highscore=maker['expert_highscore'],
super_expert_highscore=maker['super_expert_highscore'],
versus_rating=maker['versus_rating'],
versus_rank=maker['versus_rank_name'],
versus_won=maker['versus_won'],
versus_lost=maker['versus_lost'],
versus_disconnected=maker['versus_disconnected'],
coop_clears=maker['coop_clears'],
coop_plays=maker['coop_plays'],
versus_plays=maker['versus_plays'],
versus_kills=maker['versus_kills'],
versus_killed_by_others=maker['versus_killed_by_others'],
uploaded_levels=maker['uploaded_levels'],
first_clears=maker['first_clears'],
world_records=maker['world_records'],
super_world_clears=maker['unique_super_world_clears'],
super_world_id=maker['super_world_id']
)
def search_multiple_levels(api: str, count: int = 10, difficulty_id: str = 'e') -> list[Course]:
try:
response = requests.get(
url=f'{TGRCODE_API}/{api}?difficulty={difficulty_id}&count={count}',
headers={'User-Agent': USER_AGENT}
)
except ConnectionError as ex:
raise TGRCodeAPIException(ex)
try:
courses = response.json()['courses']
except requests.exceptions.JSONDecodeError:
raise TGRCodeAPIException(response.text)
ret: list[Course] = []
for course in courses:
ret.append(deserialize_course(course))
return ret
def get_multiple_levels(api: str, data_or_maker_id: str) -> list[Course]:
response = requests.get(
url=f'{TGRCODE_API}/{api}/{data_or_maker_id}',
headers={'User-Agent': USER_AGENT}
)
try:
courses = response.json()['courses']
except requests.exceptions.JSONDecodeError:
raise TGRCodeAPIException(response.text)
ret: list[Course] = []
for course in courses:
ret.append(deserialize_course(course))
return ret
def search_endless_mode(count: int = 10, difficulty_id: str = 'e') -> list[Course]:
try:
return search_multiple_levels('search_endless_mode', count, difficulty_id)
except TGRCodeAPIException as ex: # pass exception
raise TGRCodeAPIException(ex)
def search_popular(count: int = 10, difficulty_id: str = 'e') -> list[Course]:
try:
return search_multiple_levels('search_popular', count, difficulty_id)
except TGRCodeAPIException as ex: # pass exception
raise TGRCodeAPIException(ex)
def level_data_dataid(data_id: int) -> bytes:
try:
response = requests.get(
url=f'{TGRCODE_API}/level_data_dataid/{data_id}',
headers={'User-Agent': USER_AGENT}
)
if response.status_code == 200:
return response.content
else:
raise TGRCodeAPIException(response.text)
except Exception as ex: # pass exception
raise TGRCodeAPIException(ex)
def level_info(course_id: str) -> Course:
try:
response = requests.get(
url=f'{TGRCODE_API}/level_info/{normalize_course_id(course_id)}',
headers={'User-Agent': USER_AGENT}
)
response_json = response.json()
if 'error' in response_json:
raise TGRCodeAPIException(response_json['error'])
return deserialize_course(response_json)
except (TGRCodeAPIException, ConnectionError) as ex: # pass exception
raise TGRCodeAPIException(ex)
def user_info(maker_id: str) -> Maker:
try:
response = requests.get(
url=f'{TGRCODE_API}/user_info/{normalize_course_id(maker_id)}',
headers={'User-Agent': USER_AGENT}
)
response_json = response.json()
if 'error' in response_json:
raise TGRCodeAPIException(response_json['error'])
return deserialize_maker(response_json)
except (TGRCodeAPIException, ConnectionError) as ex: # pass exception
raise TGRCodeAPIException(ex)
def super_world(super_world_id: str) -> list[Course]:
try:
return get_multiple_levels('super_world', super_world_id)
except TGRCodeAPIException as ex: # pass exception
raise TGRCodeAPIException(ex)
def get_posted(maker_id: str) -> list[Course]:
try:
return get_multiple_levels('get_posted', maker_id)
except TGRCodeAPIException as ex: # pass exception
raise TGRCodeAPIException(ex)