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:
Lost-MSth
2022-07-06 22:07:00 +08:00
parent 9d746eab2d
commit af3e91b3e8
43 changed files with 780 additions and 1867 deletions

View File

@@ -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()