-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
449 lines (379 loc) · 13.6 KB
/
Copy pathapp.py
File metadata and controls
449 lines (379 loc) · 13.6 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
from flask import Flask, jsonify, request, make_response, send_file, send_from_directory, redirect, url_for
import sqlite3
import b30 as rattools
import io
import os
import json
app = Flask(__name__, static_url_path='/static')
# 静态资源
@app.route('/favicon.ico')
def favicon():
return send_from_directory(app.root_path, 'static/favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/static/video/<path:filename>')
def serve_video(filename):
# 设置缓存控制头
return send_from_directory(app.static_folder, 'video/' + filename, cache_timeout=604800)
# API
@app.route('/api/playlog/<playlog_id>', methods=['POST'])
def playlog(playlog_id):
if not playlog_id.isdigit():
return jsonify(error='非法操作'), 403
data = rattools.single_music_playlog(playlog_id)
return jsonify(data), 200
@app.route('/api/quicklogin', methods=['POST'])
def quickloginapi():
"""
处理快速登录请求
"""
if 'db_id' in request.cookies:
return jsonify(error='已登录'), 403
conn = sqlite3.connect('../rinsama-aqua/data/db.sqlite')
cursor = conn.cursor()
cursor.execute("SELECT id,user_name FROM chusan_user_data")
rows = cursor.fetchall()
columns = [description[0] for description in cursor.description]
user_list = []
for row in reversed(rows):
row_dict = dict(zip(columns, row))
user_list.append(row_dict)
conn.close()
try:
id = request.json.get('id')
except:
id = False
def check_id_in_list(id, user_list):
for item in user_list:
if item[0] == id:
return True
return False
if(id):
if(check_id_in_list(id, rows)):
resp = make_response(jsonify(message='Login successful'))
resp.set_cookie('db_id', str(id))
return resp, 200
else:
return jsonify(error='非法操作'), 403
else:
return jsonify(user_list) # 返回用户列表
@app.route('/api/news', methods=['POST'])
def newsapi():
"""
返回服务器公告
"""
if os.path.exists("news.md"):
with open("news.md", 'r', encoding='utf-8') as file:
news_content = file.read()
return jsonify({"news": news_content}), 200
else:
return jsonify({"news": "内部绝赞测试中"}), 200
@app.route('/api/b30', methods=['POST'])
def b30api():
"""
返回b30数据
"""
if not ('db_id' in request.cookies):
return jsonify(error='未登录'), 403
else:
return jsonify(rattools.process_b30(request.cookies.get("db_id"))[:30]), 200
@app.route('/api/playlog', methods=['POST'])
def playlogapi():
"""
返回playlog数据
"""
if not ('db_id' in request.cookies):
return jsonify(error='未登录'), 403
else:
return jsonify(rattools.playlog(request.cookies.get("db_id"))), 200
@app.route('/api/r10', methods=['POST'])
def r10api():
"""
返回r10数据
"""
if not ('db_id' in request.cookies):
return jsonify(error='未登录'), 403
else:
return jsonify(rattools.process_r10(request.cookies.get("db_id"))[:10]), 200
@app.route('/api/rating_image', methods=['POST'])
def rating_imageapi():
number = request.json.get('number')
if number is None:
return jsonify(error='非法操作'), 403
rating_image = rattools.create_rating_image(number)
img_byte_array = io.BytesIO()
rating_image.save(img_byte_array, format='PNG')
img_byte_array.seek(0)
return send_file(img_byte_array, mimetype='image/png')
@app.route('/api/user_info_image', methods=['POST'])
def user_info_imageapi():
"""
返回个人信息图片
"""
if not ('db_id' in request.cookies):
return jsonify(error='未登录'), 403
id = request.cookies.get("db_id")
if id is None:
return jsonify(error='非法操作'), 403
rating_image = rattools.get_user_info_pic(id)
img_byte_array = io.BytesIO()
rating_image.save(img_byte_array, format='PNG')
img_byte_array.seek(0)
return send_file(img_byte_array, mimetype='image/png')
@app.route('/api/user_data', methods=['POST'])
def user_dataapi():
"""
查询数据库中的 user_data
"""
if not ('db_id' in request.cookies):
return jsonify(error='未登录'), 403
conn = sqlite3.connect('../rinsama-aqua/data/db.sqlite')
cursor = conn.cursor()
id = request.cookies.get("db_id")
if not (id.isdecimal()):
return jsonify(error='非法操作'), 403
cursor.execute("SELECT * FROM chusan_user_data WHERE card_id = '{}'".format(id))
rows = cursor.fetchall()
columns = [description[0] for description in cursor.description]
row_dict = dict(zip(columns, rows[0]))
conn.close()
return jsonify(row_dict)
@app.route('/api/music_detail', methods=['POST'])
def music_detailapi():
"""
查询数据库中的 music_detail
"""
if not ('db_id' in request.cookies):
return jsonify(error='未登录'), 403
conn = sqlite3.connect('../rinsama-aqua/data/db.sqlite')
cursor = conn.cursor()
id = request.cookies.get("db_id")
if not (id.isdecimal()):
return jsonify(error='非法操作'), 403
cursor.execute("SELECT * FROM chusan_user_music_detail WHERE user_id = '{}'".format(id))
rows = cursor.fetchall()
columns = [description[0] for description in cursor.description]
music_detail = []
count = 0
for row in reversed(rows):
row_dict = dict(zip(columns, row))
music_detail.append(row_dict)
count += 1
conn.close()
"""
备忘:
导入到rinNET时,删除"id"与"user_id",删除下划线
"""
return jsonify(music_detail)
@app.route('/api/login', methods=['POST'])
def loginapi():
"""
处理用户登录请求
"""
if 'db_id' in request.cookies:
return jsonify(error='已登录'), 403
aime_id = request.json.get('aime_id')
if not (len(aime_id) == 20 and aime_id.isdecimal()):
return jsonify(error='非法操作'), 403
conn = sqlite3.connect('../rinsama-aqua/data/db.sqlite')
cursor = conn.cursor()
cursor.execute("SELECT id FROM sega_card WHERE luid='{}'".format(aime_id))
user = cursor.fetchone()
conn.close()
if user:
resp = make_response(jsonify(message='Login successful'))
resp.set_cookie('aime_id', aime_id)
resp.set_cookie('db_id', str(user[0]))
return resp, 200
else:
return jsonify(error='未查询到用户'), 404
@app.route('/api/user_item')
def user_itemapi():
if not ('db_id' in request.cookies):
return jsonify(error='未登录'), 403
else :
user_id = request.cookies.get("db_id")
try:
assert(user_id.isdecimal())
except AssertionError as e:
return jsonify(error='非法操作'), 403
json_data = []
# 当前装备物品
user_data_response = user_dataapi()
try:
if user_data_response.status_code == 200:
user_data = json.loads((user_data_response.response)[0].decode('utf-8'))
except:
return jsonify(error='非法操作'), 403
json_data.append(user_data)
# 当前持有物品
conn = sqlite3.connect('../rinsama-aqua/data/db.sqlite')
cursor = conn.cursor()
cursor.execute("SELECT item_id, item_kind, stock FROM chusan_user_item WHERE user_id = '{}'".format(user_id))
rows = cursor.fetchall()
item_types = {
"Nameplate": [],
"Frame": [],
"Trophy": [],
"Skill": [],
"Ticket": [],
"Present": [],
"Music": [],
"Map Icon": [],
"System Voice": [],
"Symbol Chat": [],
"Avatar Accessory": []
}
for row in rows:
item_id = row[0]
item_kind = row[1]
stock = row[2]
if item_kind == 1:
item_types["Nameplate"].append({"item_id": item_id, "stock": stock})
elif item_kind == 2:
item_types["Frame"].append({"item_id": item_id, "stock": stock})
elif item_kind == 3:
item_types["Trophy"].append({"item_id": item_id, "stock": stock})
elif item_kind == 4:
item_types["Skill"].append({"item_id": item_id, "stock": stock})
elif item_kind == 5:
item_types["Ticket"].append({"item_id": item_id, "stock": stock})
elif item_kind == 6:
item_types["Present"].append({"item_id": item_id, "stock": stock})
elif item_kind == 7:
item_types["Music"].append({"item_id": item_id, "stock": stock})
elif item_kind == 8:
item_types["Map Icon"].append({"item_id": item_id, "stock": stock})
elif item_kind == 9:
item_types["System Voice"].append({"item_id": item_id, "stock": stock})
elif item_kind == 10:
item_types["Symbol Chat"].append({"item_id": item_id, "stock": stock})
elif item_kind == 11:
item_types["Avatar Accessory"].append({"item_id": item_id, "stock": stock})
#Trophy 部分
user_trophy_list=[]
matched_trophies = []
for item in item_types['Trophy']:
user_trophy_list.append(item['item_id'])
try:
assert(user_data["trophy_id"] in user_trophy_list)
except AssertionError as e:
return jsonify(error='账户数据异常(已设置的收藏品尚未获得,通常是由于绕过前端直接操作数据库造成的。)'), 403
try:
with open("masterdata/Trophy_output.json", 'r', encoding='utf-8') as file:
data = json.load(file)
for trophy_id in user_trophy_list:
for item in data:
if str(item["id"]) == str(trophy_id):
trophy_name = item["str"]
explainText = item["explainText"]
rarity = int(item["rareType"])
# 因为有些歌是直接修改器改出来的,把不应该拿到的称号也一起发到账户里了。这里先简单屏蔽一下,以后再说:
if len(item["id"]) == 4 and int(item["id"][0]) in [5,6,7]:
continue
matched_trophies.append({
"id": trophy_id,
"name": trophy_name,
"explainText": explainText,
"rarity": rarity
})
break
# print(json.dumps(matched_trophies, ensure_ascii=False, indent=4))
except:
pass
json_data.append(matched_trophies)
#nameplate 部分
user_nameplate_list=[]
matched_nameplates = []
for item in item_types['Nameplate']:
user_nameplate_list.append(item['item_id'])
try:
assert(user_data["nameplate_id"] in user_nameplate_list)
except AssertionError as e:
return jsonify(error='账户数据异常(已设置的收藏品尚未获得,通常是由于绕过前端直接操作数据库造成的。)'), 403
try:
with open("masterdata/Nameplate_output.json", 'r', encoding='utf-8') as file:
data = json.load(file)
for nameplate_id in user_nameplate_list:
for item in data:
if str(item["id"]) == str(nameplate_id):
nameplate_name = item["str"]
explainText = item["explainText"]
matched_nameplates.append({
"id": nameplate_id,
"name": nameplate_name,
"explainText": explainText,
"src": "static/NamePlate/{}.png".format(str(nameplate_id).zfill(5))
})
break
# print(json.dumps(matched_nameplates, ensure_ascii=False, indent=4))
except:
pass
json_data.append(matched_nameplates)
return jsonify(json_data)
# 页面
@app.route('/')
def indexPage():
"""
根据是否存在特定的cookie,重定向到不同的页面
"""
if 'db_id' in request.cookies:
return redirect(url_for('cardPage'))
else:
return redirect(url_for('loginPage'))
@app.route('/b30')
def b30pic():
"""
返回b30图片
"""
if not ('db_id' in request.cookies):
return jsonify(error='未登录'), 403
else:
return send_file(rattools.chunib30(request.cookies.get("db_id")), as_attachment=False), 200
# 登录页面路由
@app.route('/login')
def loginPage():
"""
提供登录页面
"""
if 'db_id' in request.cookies:
return redirect(url_for('cardPage'))
return send_from_directory('static', 'login.html')
# 收藏品路由
@app.route('/items')
def itemsPage():
"""
提供收藏品页面
"""
if not ('db_id' in request.cookies):
return jsonify(error='未登录'), 403
else :
user_id = request.cookies.get("db_id")
return send_from_directory('static', 'items.html')
# 主页路由
@app.route('/card')
def cardPage():
"""
提供主页页面
"""
if 'db_id' not in request.cookies:
return redirect(url_for('loginPage'))
return send_from_directory('static', 'card.html')
# 游玩记录路由
@app.route('/playlog')
def playlogPage():
"""
提供游玩记录页面
"""
if 'db_id' not in request.cookies:
return redirect(url_for('loginPage'))
return send_from_directory('static', 'playlog.html')
# 企鹅换装路由
@app.route('/avatar')
def avatarPage():
"""
提供企鹅换装路由
"""
if 'db_id' not in request.cookies:
return redirect(url_for('loginPage'))
return send_from_directory('static', 'avatar.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8081,debug=True)