-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
353 lines (286 loc) · 10.3 KB
/
main.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import json
import time
from flask import Flask, request, make_response, jsonify
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from db import database
from new_pages import new_pages, get_map_pool
import api
from user import User
import create_action
app = Flask(__name__)
limiter = Limiter(
app,
key_func=get_remote_address,
application_limits=['120/minute'],
default_limits=['80/minute']
)
def api_endpoint():
database.register_request(category='api', user_agent=request.headers.get('User-Agent'), ip=request.remote_addr, request_path=str(request.url_rule))
@app.route('/api/actions')
def actions():
api_endpoint()
return api.action_list()
@app.route('/api/add_user', methods=['POST'])
def add_user():
api_endpoint()
return api.add_user(request.json, request.remote_addr)
@app.route('/api/ranked_list/<pool_id>')
def ranked_list_api(pool_id):
api_endpoint()
return api.ranked_list(pool_id)
@app.route('/api/ranked_list/<pool_id>/<page>')
def ranked_list_page_api(pool_id, page):
api_endpoint()
count = 30
page = int(page)
return api.ranked_list(pool_id, offset=page * count)
@app.route('/api/ranked_list_detailed/<pool_id>/<int:page>')
def ranked_list_detailed_api(pool_id, page):
api_endpoint()
count = 30
if request.args.get('per_page'):
count = min(100, int(request.args.get('per_page')))
return api.ranked_list_detailed(pool_id, page, count=count)
@app.route('/api/map_pools')
def map_pools_api():
api_endpoint()
return api.ranked_lists()
@app.route('/api/announcement')
def announcement_api():
api_endpoint()
return api.get_announcement()
@app.route('/api/map_pools_detailed')
def map_pools_detailed_api():
api_endpoint()
search = request.args.get('search') if request.args.get('search') else ''
return api.get_map_pools_detailed(search=search)
@app.route('/api/leaderboard/<leaderboard_id>/info')
def leaderboard_info_api(leaderboard_id):
api_endpoint()
return api.get_leaderboard_info(leaderboard_id)
@app.route('/api/leaderboard/<leaderboard_id>/scores')
def leaderboard_score_first_api(leaderboard_id):
api_endpoint()
return api.get_leaderboard_scores(leaderboard_id, offset=0, count=30)
@app.route('/api/leaderboard/<leaderboard_id>/scores/<page>')
def leaderboard_scores_api(leaderboard_id, page):
api_endpoint()
page = int(page)
count = 30
return api.get_leaderboard_scores(leaderboard_id, offset=page * count, count=count)
@app.route('/api/leaderboard/<leaderboard_id>/scores_extended/<page>')
def leaderboard_scores_extended_api(leaderboard_id, page):
api_endpoint()
page = int(page)
count = 10
if request.args.get('per_page'):
count = min(100, int(request.args.get('per_page')))
return api.get_leaderboard_scores_extended(leaderboard_id, offset=page * count, count=count)
@app.route('/api/leaderboard/<leaderboard_id>/nearby_scores/<user>')
def laderboard_scores_nearby_api(leaderboard_id, user):
api_endpoint()
user = int(user)
return api.get_leaderboard_scores_nearby(leaderboard_id, user)
@app.route('/api/leaderboard/<leaderboard_id>/nearby_scores_extended/<user>')
def laderboard_scores_nearby_extended_api(leaderboard_id, user):
api_endpoint()
user = int(user)
return api.get_leaderboard_scores_nearby(leaderboard_id, user, extend=True)
@app.route('/api/leaderboard/<leaderboard_id>/score_to_rank/<int:score>')
def leaderboard_score_to_rank(leaderboard_id, score):
api_endpoint()
return api.leaderboard_score_to_rank(leaderboard_id, score)
@app.route('/api/leaderboard/<leaderboard_id>/friends', methods=['POST'])
def leaderboard_scores_friends_api(leaderboard_id):
api_endpoint()
return api.leaderboard_scores_friends(leaderboard_id, request.json['friends'])
@app.route('/api/leaderboard/<leaderboard_id>/friends_extended', methods=['POST'])
def leaderboard_scores_friends_extended_api(leaderboard_id):
api_endpoint()
return api.leaderboard_scores_friends(leaderboard_id, request.json['friends'], extend=True)
@app.route('/api/tools/mass_ss_to_hitbloq', methods=['POST'])
def mass_ss_to_hitbloq_id():
api_endpoint()
return api.mass_ss_to_hitbloq_id(request.json['ids'])
@app.route('/api/tools/ss_to_hitbloq/<ss_id>')
def ss_to_hitbloq_id(ss_id):
api_endpoint()
return api.ss_to_hitbloq_id(ss_id)
@app.route('/api/tools/ss_registered/<ss_id>')
def ss_registered(ss_id):
api_endpoint()
return api.ss_registered(ss_id)
@app.route('/api/users/<user_id>')
def user_basic_api(user_id):
api_endpoint()
user_id = int(user_id)
return api.user_basic_api(user_id)
@app.route('/api/player_rank/<pool_id>/<user>')
def player_rank_api(pool_id, user):
api_endpoint()
user = int(user)
# catching mod bug from versions <= 1.2.1
if pool_id == 'None':
return 'stop lol', 500
return api.player_rank_api(pool_id, user)
@app.route('/api/player_ranks', methods=['POST'])
def player_ranks_api():
api_endpoint()
user = request.json['user']
pool_ids = request.json['pools']
return api.player_ranks_api(pool_ids, user)
@app.route('/api/ladder/<pool_id>/cr_to_rank/<cr>')
def ranked_ladder_theoretical_rank(pool_id, cr):
api_endpoint()
return api.theoretical_rank(pool_id, float(cr))
@app.route('/api/ladder/<pool_id>/players/<int:page>')
def ranked_ladder_api(pool_id, page):
api_endpoint()
per_page = 10
if request.args.get('per_page'):
per_page = min(100, int(request.args.get('per_page')))
search = request.args.get('search')
return api.ranked_ladder(pool_id, page, players_per_page=per_page, search=search)
@app.route('/api/ladder/<pool_id>/nearby_players/<int:user_id>')
def ranked_ladder_nearby_api(pool_id, user_id):
api_endpoint()
return api.ranked_ladder_nearby(pool_id, user_id)
@app.route('/api/ladder/<pool_id>/friends', methods=['POST'])
def ranked_ladder_friends_api(pool_id):
api_endpoint()
return api.ranked_ladder_friends(pool_id, request.json['friends'])
@app.route('/api/user/<int:user_id>/scores')
def user_api(user_id):
api_endpoint()
sort_mode = 'cr'
page = 0
if request.args.get('sort'):
sort_mode = request.args.get('sort')
if request.args.get('page'):
page = int(request.args.get('page'))
return api.get_user_scores(user_id, get_map_pool(), sort_mode=sort_mode, page=page)
@app.route('/api/user/<int:user_id>/all_scores')
def user_all_api(user_id):
api_endpoint()
return api.get_all_user_scores(user_id, get_map_pool())
@app.route('/api/update_user/<int:user_id>')
def update_user(user_id):
api_endpoint()
print('received user update request for', user_id)
last_refresh = database.db['users'].find_one({'_id': user_id})['last_manual_refresh']
if time.time() - last_refresh > 60:
action_id = create_action.update_user(user_id, queue_id=1)
database.db['users'].update_one({'_id': user_id}, {'$set': {'last_manual_refresh': time.time()}})
return json.dumps({'time': time.time(), 'id': action_id, 'error': None})
else:
return json.dumps({'time': last_refresh, 'id': None, 'error': 'refreshed too quickly'})
@app.route('/api/refresh_profile/<int:user_id>')
def refresh_profile(user_id):
api_endpoint()
print('received profile refresh request for', user_id)
action_id = create_action.refresh_profile(user_id, queue_id=1)
return json.dumps({'success': True})
@app.route('/api/action_status/<action_id>')
def action_id_status(action_id):
api_endpoint()
return api.action_id_status(action_id)
@app.route('/api/queue_statuses')
def action_queue_statuses():
api_endpoint()
return api.action_queue_statuses()
@app.route('/api/get_template/<template_id>')
def get_template(template_id):
api_endpoint()
return api.get_template(template_id)
@app.route('/api/event')
def get_current_event():
api_endpoint()
return api.get_current_event()
@app.route('/api/popular_pools')
def get_popular_pools():
api_endpoint()
popular_pool_threshold = 15
return api.get_popular_pools(popular_pool_threshold)
@app.route('/api/player_badges/<int:user_id>')
def get_player_badges(user_id):
api_endpoint()
return api.get_player_badges(user_id)
@app.route('/api/pool_feed/<pool_id>')
def get_pool_feed(pool_id):
api_endpoint()
page = 0
if request.args.get('page'):
page = int(request.args.get('page'))
return api.pool_feed(pool_id, page)
@app.route('/api/mm/ss_user/<scoresaber_id>')
def get_mm_ss_user(scoresaber_id):
api_endpoint()
return jsonify(api.mm.profile(scoresaber_id))
@app.route('/api/mm/user/<int:user_id>')
def get_mm_user(user_id):
api_endpoint()
return jsonify(api.mm.profile(user_id))
@app.route('/api/mm/active_pools')
def get_mm_pools():
api_endpoint()
return jsonify(api.mm.pools())
@app.route('/api/mm/submit_match', methods=['POST'])
def submit_mm_match():
api_endpoint()
return jsonify(api.mm.submit_match(request.json))
@app.route('/')
def new_home():
html = new_pages['home']()
return html
@app.route('/about')
def new_about():
html = new_pages['about']()
return html
@app.route('/map_pools')
def new_map_pools():
html = new_pages['map_pools']()
return html
@app.route('/map_pool/<pool_id>')
def new_map_pool(pool_id):
html = new_pages['map_pool'](pool_id)
return html
@app.route('/ladder/<ladder>')
def new_ladder(ladder):
html = new_pages['ladder'](ladder)
return html
@app.route('/leaderboard/<leaderboard_id>')
def new_leaderboard(leaderboard_id):
html = new_pages['leaderboard'](leaderboard_id)
return html
@app.route('/user/<int:user_id>')
def new_user(user_id):
html = new_pages['user'](user_id)
return html
@app.route('/ranked_list/<pool_id>')
def new_ranked_list(pool_id):
html = new_pages['ranked_list'](pool_id)
return html
@app.route('/add_user')
def new_add_user():
html = new_pages['add_user']()
return html
@app.route('/actions')
def new_actions():
html = new_pages['actions']()
return html
@app.route('/contact')
def contact():
html = new_pages['contact']()
return html
@app.route('/discord')
def discord():
html = new_pages['discord']()
return html
@app.after_request
def after_request(response):
if request.path.split('.')[-1] == 'bplist':
response.headers['Cache-Control'] = 'no-store'
return response
if __name__ == "__main__":
app.run()