From 1dd9056d88ab2d518ddcdb66ef66f274e6fbb1dd Mon Sep 17 00:00:00 2001 From: helloplhm-qwq Date: Sat, 6 Jan 2024 14:10:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81kg=E6=BA=90=E6=90=9C?= =?UTF-8?q?=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/utils.py | 5 +- main.py | 11 +++- modules/__init__.py | 30 +++++++++ modules/kg/__init__.py | 9 ++- modules/kg/search.py | 139 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 modules/kg/search.py diff --git a/common/utils.py b/common/utils.py index 178aea1..1e1e5b2 100644 --- a/common/utils.py +++ b/common/utils.py @@ -175,7 +175,10 @@ def sizeFormat(size): return f"{round(size / 1024**5, 2)}PB" def timeLengthFormat(t): - t = int(t) + try: + t = int(t) + except: + return '//' hour = t // 3600 minute = (t % 3600) // 60 second = t % 60 diff --git a/main.py b/main.py index fa1b033..0648b16 100644 --- a/main.py +++ b/main.py @@ -119,10 +119,17 @@ async def handle(request): return handleResult({"code": 1, "msg": "lxm请求头验证失败", "data": None}, 403) 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)) + elif ((method + '_with_query') in dir(modules) and query != {}): + return handleResult(await getattr(modules, method + '_with_query')(source, songId, quality, query)) 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: logger.error(traceback.format_exc()) return handleResult({'code': 4, 'msg': '内部服务器错误', 'data': None}, 500) diff --git a/modules/__init__.py b/modules/__init__.py index 3fb0d0b..15cd369 100644 --- a/modules/__init__.py +++ b/modules/__init__.py @@ -160,6 +160,12 @@ async def lyric(source, songId, _): '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, _): try: func = require('modules.' + source + '.' + method) @@ -182,3 +188,27 @@ async def other(method, source, songid, _): 'msg': e.args[0], '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, + } \ No newline at end of file diff --git a/modules/kg/__init__.py b/modules/kg/__init__.py index fb7c118..0c2ae0b 100644 --- a/modules/kg/__init__.py +++ b/modules/kg/__init__.py @@ -16,6 +16,7 @@ from .lyric import getLyric as _getLyric from .lyric import lyricSearchByHash as _lyricSearch from .mv import getMvInfo as _getMvInfo from .mv import getMvPlayURL as _getMvUrl +from .search import getSongSearchResult as _songsearch from common.exceptions import FailedException from common import Httpx from common import utils @@ -75,4 +76,10 @@ async def mv(hash_): async def lyric(hash_): lyric_search_result = await _lyricSearch(hash_) choosed_lyric = lyric_search_result[0] - return await _getLyric(choosed_lyric['id'], choosed_lyric['accesskey']) \ No newline at end of file + return await _getLyric(choosed_lyric['id'], choosed_lyric['accesskey']) + +async def search(type, params): + if (type == 'song'): + return await _songsearch(**params) + else: + raise FailedException('暂不支持该类型搜索') \ No newline at end of file diff --git a/modules/kg/search.py b/modules/kg/search.py new file mode 100644 index 0000000..2598323 --- /dev/null +++ b/modules/kg/search.py @@ -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, + } \ No newline at end of file