chore: 优化代码

This commit is contained in:
helloplhm-qwq
2024-01-20 15:02:23 +08:00
parent d308ffb4ee
commit 3b5d810f2c
3 changed files with 35 additions and 41 deletions

11
main.py
View File

@ -127,15 +127,10 @@ async def handle(request):
try:
query = dict(request.query)
if (method in dir(modules) and query == {}):
return handleResult(await getattr(modules, method)(source, songId, quality))
elif ((method + '_with_query') in dir(modules) and query != {}):
return handleResult(await getattr(modules, method + '_with_query')(source, songId, quality, query))
if (method in dir(modules)):
return handleResult(await getattr(modules, method)(source, songId, quality, query))
else:
if (query == {}):
return handleResult(await modules.other(method, source, songId, quality))
else:
return handleResult(await modules.other_with_query(method, source, songId, quality, query))
return handleResult(await modules.other(source, songId, quality, query))
except:
logger.error(traceback.format_exc())
return handleResult({'code': 4, 'msg': '内部服务器错误', 'data': None}, 500)

View File

@ -47,14 +47,17 @@ sourceExpirationTime = {
}
async def url(source, songId, quality):
async def url(source, songId, quality, query):
if (not quality):
return {
'code': 2,
'msg': '需要参数"quality"',
'data': None,
}
if (source == "kg"):
songId = songId.lower()
try:
cache = config.getCache('urls', f'{source}_{songId}_{quality}')
if cache:
@ -124,7 +127,7 @@ async def url(source, songId, quality):
'data': None,
}
async def lyric(source, songId, _):
async def lyric(source, songId, _, query):
cache = config.getCache('lyric', f'{source}_{songId}')
if cache:
return {
@ -160,13 +163,30 @@ async def lyric(source, songId, _):
'data': None,
}
async def lyric_with_query(source, songId, thisvariableisnotuseful):
return await lyric(source, songId, None)
async def search(source, songid, _, query):
try:
func = require('modules.' + source + '.search')
except:
return {
'code': 1,
'msg': '未知的源或不支持的方法',
'data': None,
}
try:
result = await func(songid, query)
return {
'code': 0,
'msg': 'success',
'data': result
}
except FailedException as e:
return {
'code': 2,
'msg': e.args[0],
'data': None,
}
async def url_with_query(source, songId, quality):
return await url(source, songId, quality)
async def other(method, source, songid, _):
async def other(method, source, songid, _, query):
try:
func = require('modules.' + source + '.' + method)
except:
@ -189,28 +209,5 @@ async def other(method, source, songid, _):
'data': None,
}
async def other_with_query(method, source, t, _, query):
try:
func = require('modules.' + source + '.' + method)
except:
return {
'code': 1,
'msg': '未知的源或不支持的方法',
'data': None,
}
try:
result = await func(t, query)
return {
'code': 0,
'msg': 'success',
'data': result
}
except FailedException as e:
return {
'code': 2,
'msg': e.args[0],
'data': None,
}
async def info_with_query(source, songid, _, query):
return await other('info', source, songid, None)

View File

@ -59,7 +59,9 @@ def formatSubResult(l):
})
return res
async def getSongSearchResult(query, page, size):
async def getSongSearchResult(query, page = 1, size = 20):
page = int(page)
size = int(size)
req = await Httpx.AsyncRequest(utils.encodeURI(f'https://songsearch.kugou.com/song_search_v2?' + buildRequestParams({
"keyword": query,
"page": page,