mirror of
https://github.com/Lost-MSth/Arcaea-server.git
synced 2026-02-10 18:07:28 +08:00
Code refactoring
- Code refactoring mainly for API - Delete a useless option in `setting.py` - Change some constants in Link Play mode
This commit is contained in:
@@ -1,57 +1,48 @@
|
||||
from core.sql import Connect
|
||||
from core.sql import Sql
|
||||
from core.error import ArcError, NoData
|
||||
from core.song import Song
|
||||
from core.sql import Connect, Query, Sql
|
||||
from flask import Blueprint, request
|
||||
|
||||
from .api_auth import request_json_handle, role_required
|
||||
from .api_code import error_return, success_return
|
||||
from .constant import Constant
|
||||
|
||||
bp = Blueprint('songs', __name__, url_prefix='/songs')
|
||||
|
||||
|
||||
def get_song_info(song_id):
|
||||
# 查询指定歌曲信息,返回字典
|
||||
r = {}
|
||||
|
||||
with Connect('') as c:
|
||||
c.execute('''select * from chart where song_id=:a''', {'a': song_id})
|
||||
x = c.fetchone()
|
||||
if x:
|
||||
r = {'song_id': x[0],
|
||||
'name': {'name_en': x[1],
|
||||
'name_jp': x[2]},
|
||||
'pakset': x[5],
|
||||
'artist': x[6],
|
||||
'date': x[9],
|
||||
'rating_pst': x[13]/10,
|
||||
'rating_prs': x[14]/10,
|
||||
'rating_ftr': x[15]/10,
|
||||
'rating_byn': x[16]/10,
|
||||
'difficultly_pst': x[17]/2,
|
||||
'difficultly_prs': x[18]/2,
|
||||
'difficultly_ftr': x[19]/2,
|
||||
'difficultly_byn': x[20]/2
|
||||
}
|
||||
|
||||
return r
|
||||
@bp.route('/<string:song_id>', methods=['GET'])
|
||||
@role_required(request, ['select', 'select_song_info'])
|
||||
def songs_song_get(user, song_id):
|
||||
'''查询歌曲信息'''
|
||||
with Connect() as c:
|
||||
try:
|
||||
s = Song(c, song_id).select()
|
||||
return success_return(s.to_dict())
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
|
||||
|
||||
def get_songs(query=None):
|
||||
# 查询全部歌曲信息,返回字典列表
|
||||
r = []
|
||||
|
||||
with Connect('') as c:
|
||||
x = Sql.select(c, 'chart', [], query)
|
||||
|
||||
if x:
|
||||
@bp.route('', methods=['GET'])
|
||||
@role_required(request, ['select', 'select_song_info'])
|
||||
@request_json_handle(request, optional_keys=Constant.QUERY_KEYS)
|
||||
def songs_get(data, user):
|
||||
'''查询全歌曲信息'''
|
||||
A = ['song_id', 'name']
|
||||
B = ['song_id', 'name', 'rating_pst',
|
||||
'rating_prs', 'rating_ftr', 'rating_byn']
|
||||
with Connect() as c:
|
||||
try:
|
||||
query = Query(A, A, B).from_data(data)
|
||||
x = Sql(c).select('chart', query=query)
|
||||
r = []
|
||||
for i in x:
|
||||
r.append({'sid': i[0],
|
||||
'name': {'name_en': i[1],
|
||||
'name_jp': i[2]},
|
||||
'pakset': i[5],
|
||||
'artist': i[6],
|
||||
'date': i[9],
|
||||
'rating_pst': i[13]/10,
|
||||
'rating_prs': i[14]/10,
|
||||
'rating_ftr': i[15]/10,
|
||||
'rating_byn': i[16]/10,
|
||||
'difficultly_pst': i[17]/2,
|
||||
'difficultly_prs': i[18]/2,
|
||||
'difficultly_ftr': i[19]/2,
|
||||
'difficultly_byn': i[20]/2
|
||||
})
|
||||
r.append(Song(c).from_list(i))
|
||||
|
||||
return r
|
||||
if not r:
|
||||
raise NoData(api_error_code=-2)
|
||||
|
||||
return success_return([x.to_dict() for x in r])
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
|
||||
Reference in New Issue
Block a user