mirror of
https://github.com/MeoProject/lx-music-api-server.git
synced 2025-05-23 19:17:41 +08:00
feat: 支持kg源搜索
This commit is contained in:
parent
03161c504a
commit
1dd9056d88
@ -175,7 +175,10 @@ def sizeFormat(size):
|
|||||||
return f"{round(size / 1024**5, 2)}PB"
|
return f"{round(size / 1024**5, 2)}PB"
|
||||||
|
|
||||||
def timeLengthFormat(t):
|
def timeLengthFormat(t):
|
||||||
t = int(t)
|
try:
|
||||||
|
t = int(t)
|
||||||
|
except:
|
||||||
|
return '//'
|
||||||
hour = t // 3600
|
hour = t // 3600
|
||||||
minute = (t % 3600) // 60
|
minute = (t % 3600) // 60
|
||||||
second = t % 60
|
second = t % 60
|
||||||
|
11
main.py
11
main.py
@ -119,10 +119,17 @@ async def handle(request):
|
|||||||
return handleResult({"code": 1, "msg": "lxm请求头验证失败", "data": None}, 403)
|
return handleResult({"code": 1, "msg": "lxm请求头验证失败", "data": None}, 403)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if (method in dir(modules)):
|
query = dict(request.query)
|
||||||
|
print(query)
|
||||||
|
if (method in dir(modules) and query == {}):
|
||||||
return handleResult(await getattr(modules, method)(source, songId, quality))
|
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))
|
||||||
else:
|
else:
|
||||||
return handleResult(await modules.other(method, source, songId, quality))
|
if (query == {}):
|
||||||
|
return handleResult(await modules.other(method, source, songId, quality))
|
||||||
|
else:
|
||||||
|
return handleResult(await modules.other_with_query(method, source, songId, quality, query))
|
||||||
except:
|
except:
|
||||||
logger.error(traceback.format_exc())
|
logger.error(traceback.format_exc())
|
||||||
return handleResult({'code': 4, 'msg': '内部服务器错误', 'data': None}, 500)
|
return handleResult({'code': 4, 'msg': '内部服务器错误', 'data': None}, 500)
|
||||||
|
@ -160,6 +160,12 @@ async def lyric(source, songId, _):
|
|||||||
'data': None,
|
'data': None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async def lyric_with_query(source, songId, thisvariableisnotuseful):
|
||||||
|
return await lyric(source, songId, 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, _):
|
||||||
try:
|
try:
|
||||||
func = require('modules.' + source + '.' + method)
|
func = require('modules.' + source + '.' + method)
|
||||||
@ -182,3 +188,27 @@ async def other(method, source, songid, _):
|
|||||||
'msg': e.args[0],
|
'msg': e.args[0],
|
||||||
'data': None,
|
'data': None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async def other_with_query(method, source, t, _, query):
|
||||||
|
print(method)
|
||||||
|
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,
|
||||||
|
}
|
@ -16,6 +16,7 @@ from .lyric import getLyric as _getLyric
|
|||||||
from .lyric import lyricSearchByHash as _lyricSearch
|
from .lyric import lyricSearchByHash as _lyricSearch
|
||||||
from .mv import getMvInfo as _getMvInfo
|
from .mv import getMvInfo as _getMvInfo
|
||||||
from .mv import getMvPlayURL as _getMvUrl
|
from .mv import getMvPlayURL as _getMvUrl
|
||||||
|
from .search import getSongSearchResult as _songsearch
|
||||||
from common.exceptions import FailedException
|
from common.exceptions import FailedException
|
||||||
from common import Httpx
|
from common import Httpx
|
||||||
from common import utils
|
from common import utils
|
||||||
@ -76,3 +77,9 @@ async def lyric(hash_):
|
|||||||
lyric_search_result = await _lyricSearch(hash_)
|
lyric_search_result = await _lyricSearch(hash_)
|
||||||
choosed_lyric = lyric_search_result[0]
|
choosed_lyric = lyric_search_result[0]
|
||||||
return await _getLyric(choosed_lyric['id'], choosed_lyric['accesskey'])
|
return await _getLyric(choosed_lyric['id'], choosed_lyric['accesskey'])
|
||||||
|
|
||||||
|
async def search(type, params):
|
||||||
|
if (type == 'song'):
|
||||||
|
return await _songsearch(**params)
|
||||||
|
else:
|
||||||
|
raise FailedException('暂不支持该类型搜索')
|
139
modules/kg/search.py
Normal file
139
modules/kg/search.py
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
# ----------------------------------------
|
||||||
|
# - mode: python -
|
||||||
|
# - author: helloplhm-qwq -
|
||||||
|
# - name: search.py -
|
||||||
|
# - project: lx-music-api-server -
|
||||||
|
# - license: MIT -
|
||||||
|
# ----------------------------------------
|
||||||
|
# This file is part of the "lx-music-api-server" project.
|
||||||
|
|
||||||
|
from common import Httpx
|
||||||
|
from common import utils
|
||||||
|
from common.exceptions import FailedException
|
||||||
|
from .utils import buildRequestParams
|
||||||
|
|
||||||
|
def formatSubResult(l):
|
||||||
|
res = []
|
||||||
|
for songinfo in l:
|
||||||
|
fileinfo = {}
|
||||||
|
if (songinfo['FileSize'] != 0):
|
||||||
|
fileinfo['128k'] = {
|
||||||
|
'hash': songinfo['FileHash'],
|
||||||
|
'size': utils.sizeFormat(songinfo['FileSize']),
|
||||||
|
}
|
||||||
|
if (songinfo['HQFileSize'] != 0):
|
||||||
|
fileinfo['320k'] = {
|
||||||
|
'hash': songinfo['HQFileHash'],
|
||||||
|
'size': utils.sizeFormat(songinfo['HQFileSize']),
|
||||||
|
}
|
||||||
|
if (songinfo['SQFileSize'] != 0):
|
||||||
|
fileinfo['flac'] = {
|
||||||
|
'hash': songinfo['SQFileHash'],
|
||||||
|
'size': utils.sizeFormat(songinfo['SQFileSize']),
|
||||||
|
}
|
||||||
|
if (songinfo['ResFileSize'] != 0):
|
||||||
|
fileinfo['flac24bit'] = {
|
||||||
|
'hash': songinfo['ResFileHash'],
|
||||||
|
'size': utils.sizeFormat(songinfo['ResFileSize']),
|
||||||
|
}
|
||||||
|
|
||||||
|
res.append({
|
||||||
|
'name': songinfo['SongName'],
|
||||||
|
'name_ori': songinfo['OriSongName'],
|
||||||
|
'name_extra': songinfo['SongName'].replace(songinfo['OriSongName'], ''),
|
||||||
|
'singer': songinfo['SingerName'],
|
||||||
|
'singer_list': [{'name': i['name'], 'id': i['id']} for i in songinfo['Singers']],
|
||||||
|
'isoriginal': True if (songinfo['IsOriginal'] == 1) else False,
|
||||||
|
'tag': songinfo.get('TagContent') if songinfo.get('TagContent') else '',
|
||||||
|
'format_length': utils.timeLengthFormat(songinfo['Duration']),
|
||||||
|
'length': songinfo['Duration'],
|
||||||
|
'hash': songinfo['FileHash'],
|
||||||
|
'file_info': fileinfo,
|
||||||
|
'songmid': songinfo['Audioid'],
|
||||||
|
'album_id': songinfo['AlbumID'],
|
||||||
|
'album': songinfo['AlbumName'],
|
||||||
|
'language': songinfo['trans_param'].get('language') if songinfo['trans_param'] else '',
|
||||||
|
'cover': songinfo['Image'].format(size = 1080),
|
||||||
|
'sizable_cover': songinfo['Image'],
|
||||||
|
'mvid': songinfo['MvHash'],
|
||||||
|
})
|
||||||
|
return res
|
||||||
|
|
||||||
|
async def getSongSearchResult(query, page, size):
|
||||||
|
req = await Httpx.AsyncRequest(utils.encodeURI(f'https://songsearch.kugou.com/song_search_v2?' + buildRequestParams({
|
||||||
|
"keyword": query,
|
||||||
|
"page": page,
|
||||||
|
"pagesize": size,
|
||||||
|
"userid": 0,
|
||||||
|
"clientver": "",
|
||||||
|
"platform": "WebFilter",
|
||||||
|
"filter": 2,
|
||||||
|
"iscorrection": 1,
|
||||||
|
"privilege_filter": 0
|
||||||
|
})), {
|
||||||
|
"headers": {
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.142.86 Safari/537.36",
|
||||||
|
"Referer": "https://www.kugou.com",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
body = req.json()
|
||||||
|
if (body['status'] != 1):
|
||||||
|
raise FailedException('歌曲搜索失败')
|
||||||
|
if (body['data']['total'] == 0 or body['data']['lists'] == []):
|
||||||
|
return {
|
||||||
|
'total': 0,
|
||||||
|
'page': page,
|
||||||
|
'size': size,
|
||||||
|
'list': [],
|
||||||
|
}
|
||||||
|
res = []
|
||||||
|
for songinfo in body['data']['lists']:
|
||||||
|
fileinfo = {}
|
||||||
|
if (songinfo['FileSize'] != 0):
|
||||||
|
fileinfo['128k'] = {
|
||||||
|
'hash': songinfo['FileHash'],
|
||||||
|
'size': utils.sizeFormat(songinfo['FileSize']),
|
||||||
|
}
|
||||||
|
if (songinfo['HQFileSize'] != 0):
|
||||||
|
fileinfo['320k'] = {
|
||||||
|
'hash': songinfo['HQFileHash'],
|
||||||
|
'size': utils.sizeFormat(songinfo['HQFileSize']),
|
||||||
|
}
|
||||||
|
if (songinfo['SQFileSize'] != 0):
|
||||||
|
fileinfo['flac'] = {
|
||||||
|
'hash': songinfo['SQFileHash'],
|
||||||
|
'size': utils.sizeFormat(songinfo['SQFileSize']),
|
||||||
|
}
|
||||||
|
if (songinfo['ResFileSize'] != 0):
|
||||||
|
fileinfo['flac24bit'] = {
|
||||||
|
'hash': songinfo['ResFileHash'],
|
||||||
|
'size': utils.sizeFormat(songinfo['ResFileSize']),
|
||||||
|
}
|
||||||
|
|
||||||
|
res.append({
|
||||||
|
'name': songinfo['SongName'],
|
||||||
|
'name_ori': songinfo['OriSongName'],
|
||||||
|
'name_extra': songinfo['SongName'].replace(songinfo['OriSongName'], ''),
|
||||||
|
'singer': songinfo['SingerName'],
|
||||||
|
'singer_list': [{'name': i['name'], 'id': i['id']} for i in songinfo['Singers']],
|
||||||
|
'isoriginal': True if (songinfo['IsOriginal'] == 1) else False,
|
||||||
|
'tag': songinfo.get('TagContent') if songinfo.get('TagContent') else '',
|
||||||
|
'format_length': utils.timeLengthFormat(songinfo['Duration']),
|
||||||
|
'length': songinfo['Duration'],
|
||||||
|
'hash': songinfo['FileHash'],
|
||||||
|
'file_info': fileinfo,
|
||||||
|
'songmid': songinfo['Audioid'],
|
||||||
|
'album_id': songinfo['AlbumID'],
|
||||||
|
'album': songinfo['AlbumName'],
|
||||||
|
'language': songinfo['trans_param'].get('language') if songinfo['trans_param'] else '',
|
||||||
|
'cover': songinfo['Image'].format(size = 1080),
|
||||||
|
'sizable_cover': songinfo['Image'],
|
||||||
|
'mvid': songinfo['MvHash'],
|
||||||
|
'subresult': [] if (songinfo['Grp'] == []) else formatSubResult(songinfo['Grp']),
|
||||||
|
})
|
||||||
|
return {
|
||||||
|
'total': body['data']['total'],
|
||||||
|
'page': page,
|
||||||
|
'size': size,
|
||||||
|
'list': res,
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user