mirror of
https://github.com/MeoProject/lx-music-api-server.git
synced 2025-05-23 19:17:41 +08:00
chore: 完善返回数据
This commit is contained in:
parent
92236c549a
commit
a8cfd47e15
@ -49,16 +49,21 @@ sourceExpirationTime = {
|
|||||||
|
|
||||||
|
|
||||||
async def handleApiRequest(command, source, songId, quality):
|
async def handleApiRequest(command, source, songId, quality):
|
||||||
if (source == "kg"):
|
|
||||||
songId = songId.lower()
|
|
||||||
try:
|
try:
|
||||||
c = config.getCache('urls', f'{source}_{songId}_{quality}')
|
cache = config.getCache('urls', f'{source}_{songId}_{quality}')
|
||||||
if c:
|
if cache:
|
||||||
logger.debug(f'使用缓存的{source}_{songId}_{quality}数据,URL:{c["url"]}')
|
logger.debug(f'使用缓存的{source}_{songId}_{quality}数据,URL:{cache["url"]}')
|
||||||
return {
|
return {
|
||||||
'code': 0,
|
'code': 0,
|
||||||
'msg': 'success',
|
'msg': 'success',
|
||||||
'data': c['url'],
|
'data': {
|
||||||
|
'url': cache['url'],
|
||||||
|
'cache': True,
|
||||||
|
'quality': {
|
||||||
|
'target': quality,
|
||||||
|
'result': quality,
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
except:
|
except:
|
||||||
logger.error(traceback.format_exc())
|
logger.error(traceback.format_exc())
|
||||||
@ -71,18 +76,33 @@ async def handleApiRequest(command, source, songId, quality):
|
|||||||
'data': None,
|
'data': None,
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
url = await func(songId, quality)
|
result = await func(songId, quality)
|
||||||
logger.debug(f'获取{source}_{songId}_{quality}成功,URL:{url}')
|
logger.debug(f'获取{source}_{songId}_{quality}成功,URL:{result['url']}')
|
||||||
|
|
||||||
|
canExpire = sourceExpirationTime[source]['expire']
|
||||||
|
expireTime = sourceExpirationTime[source]['time'] + int(time.time())
|
||||||
config.updateCache('urls', f'{source}_{songId}_{quality}', {
|
config.updateCache('urls', f'{source}_{songId}_{quality}', {
|
||||||
"expire": sourceExpirationTime[source]['expire'],
|
"expire": canExpire,
|
||||||
"time": sourceExpirationTime[source]['time'] + int(time.time()),
|
"time": expireTime,
|
||||||
"url": url,
|
"url": result['url'],
|
||||||
})
|
})
|
||||||
logger.debug(f'缓存已更新:{source}_{songId}_{quality}, URL:{url}, expire: {sourceExpirationTime[source]["time"] + int(time.time())}')
|
logger.debug(f'缓存已更新:{source}_{songId}_{quality}, URL:{result['url']}, expire: {expireTime}')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'code': 0,
|
'code': 0,
|
||||||
'msg': 'success',
|
'msg': 'success',
|
||||||
'data': url,
|
'data': {
|
||||||
|
'url': result['url'],
|
||||||
|
'cache': False,
|
||||||
|
'quality': {
|
||||||
|
'target': quality,
|
||||||
|
'result': result['quality'],
|
||||||
|
},
|
||||||
|
'expire': {
|
||||||
|
'time': expireTime,
|
||||||
|
'canExpire': canExpire,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
except FailedException as e:
|
except FailedException as e:
|
||||||
return {
|
return {
|
||||||
|
@ -63,7 +63,6 @@ def signRequest(url, params, options):
|
|||||||
return Httpx.request(url, options)
|
return Httpx.request(url, options)
|
||||||
|
|
||||||
def getKey(hash_):
|
def getKey(hash_):
|
||||||
# print(hash_ + tools.pidversec + tools.appid + tools.mid + tools.userid)
|
|
||||||
return utils.createMD5(hash_.lower() + tools.pidversec + tools.appid + tools.mid + tools.userid)
|
return utils.createMD5(hash_.lower() + tools.pidversec + tools.appid + tools.mid + tools.userid)
|
||||||
|
|
||||||
async def getMusicInfo(hash_):
|
async def getMusicInfo(hash_):
|
||||||
@ -120,7 +119,7 @@ async def url(songId, quality):
|
|||||||
'album_id': albumid,
|
'album_id': albumid,
|
||||||
'userid': tools.userid,
|
'userid': tools.userid,
|
||||||
'area_code': 1,
|
'area_code': 1,
|
||||||
'hash': thash.lower(),
|
'hash': thash,
|
||||||
'module': '',
|
'module': '',
|
||||||
'mid': tools.mid,
|
'mid': tools.mid,
|
||||||
'appid': tools.appid,
|
'appid': tools.appid,
|
||||||
@ -142,7 +141,6 @@ async def url(songId, quality):
|
|||||||
}
|
}
|
||||||
if (tools.version == 'v5'):
|
if (tools.version == 'v5'):
|
||||||
params['quality'] = tools.qualityMap[quality]
|
params['quality'] = tools.qualityMap[quality]
|
||||||
# print(params.quality)
|
|
||||||
if (tools.version == "v4"):
|
if (tools.version == "v4"):
|
||||||
params['version'] = tools.clientver
|
params['version'] = tools.clientver
|
||||||
headers = createObject({
|
headers = createObject({
|
||||||
@ -162,4 +160,8 @@ async def url(songId, quality):
|
|||||||
raise FailedException('链接获取失败,请检查账号是否有会员或数字专辑是否购买')
|
raise FailedException('链接获取失败,请检查账号是否有会员或数字专辑是否购买')
|
||||||
elif body.status != 1:
|
elif body.status != 1:
|
||||||
raise FailedException('链接获取失败,可能是数字专辑或者api失效')
|
raise FailedException('链接获取失败,可能是数字专辑或者api失效')
|
||||||
return body.url[0]
|
|
||||||
|
return {
|
||||||
|
'url': body.url[0],
|
||||||
|
'quality': quality
|
||||||
|
}
|
||||||
|
@ -16,6 +16,11 @@ tools = {
|
|||||||
'320k': '320kmp3',
|
'320k': '320kmp3',
|
||||||
'flac': '2000kflac',
|
'flac': '2000kflac',
|
||||||
},
|
},
|
||||||
|
'qualityMapReverse': {
|
||||||
|
'128': '128k',
|
||||||
|
'320': '320k',
|
||||||
|
'2000': 'flac',
|
||||||
|
},
|
||||||
'extMap': {
|
'extMap': {
|
||||||
'128k': 'mp3',
|
'128k': 'mp3',
|
||||||
'320k': 'mp3',
|
'320k': 'mp3',
|
||||||
@ -24,7 +29,7 @@ tools = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async def url(songId, quality):
|
async def url(songId, quality):
|
||||||
target_url = f'''https://bd-api.kuwo.cn/api/service/music/downloadInfo/{songId}?isMv=0&format={tools['extMap'][quality]}&br={tools['qualityMap'][quality]}&level='''
|
target_url = f'''https://bd-api.kuwo.cn/api/service/music/downloadInfo/{songId}?isMv=0&format={tools['extMap'][quality]}&br={tools['qualityMap'][quality]}'''
|
||||||
req = Httpx.request(target_url, {
|
req = Httpx.request(target_url, {
|
||||||
'method': 'GET',
|
'method': 'GET',
|
||||||
'headers': {
|
'headers': {
|
||||||
@ -39,8 +44,14 @@ async def url(songId, quality):
|
|||||||
})
|
})
|
||||||
try:
|
try:
|
||||||
body = req.json()
|
body = req.json()
|
||||||
if (body['code'] != 200) or (body['data']['audioInfo']['bitrate'] == 1):
|
data = body['data']
|
||||||
|
|
||||||
|
if (body['code'] != 200) or (data['audioInfo']['bitrate'] == 1):
|
||||||
raise FailedException('failed')
|
raise FailedException('failed')
|
||||||
return body['data']['url'].split('?')[0]
|
|
||||||
|
return {
|
||||||
|
'url': data['url'].split('?')[0],
|
||||||
|
'quality': tools['qualityMapReverse'][data['audioInfo']['bitrate']]
|
||||||
|
}
|
||||||
except:
|
except:
|
||||||
raise FailedException('failed')
|
raise FailedException('failed')
|
@ -20,6 +20,12 @@ tools = {
|
|||||||
'flac': 'SQ',
|
'flac': 'SQ',
|
||||||
'flac24bit': 'ZQ',
|
'flac24bit': 'ZQ',
|
||||||
},
|
},
|
||||||
|
'qualityMapReverse': {
|
||||||
|
'PQ': '128k',
|
||||||
|
'HQ': '320k',
|
||||||
|
'SQ': 'flac',
|
||||||
|
'ZQ': 'flac24bit',
|
||||||
|
},
|
||||||
'token': config.read_config('module.mg.user.token'),
|
'token': config.read_config('module.mg.user.token'),
|
||||||
'aversionid': config.read_config('module.mg.user.aversionid'),
|
'aversionid': config.read_config('module.mg.user.aversionid'),
|
||||||
'useragent': config.read_config('module.mg.user.useragent'),
|
'useragent': config.read_config('module.mg.user.useragent'),
|
||||||
@ -42,9 +48,14 @@ async def url(songId, quality):
|
|||||||
})
|
})
|
||||||
try:
|
try:
|
||||||
body = req.json()
|
body = req.json()
|
||||||
# print((body['data']['url']))
|
data = body['data']
|
||||||
if ((not int(body['code']) == 0) or ( not body['data']['url'])):
|
|
||||||
|
if ((not int(body['code']) == 0) or ( not data['url'])):
|
||||||
raise FailedException('failed')
|
raise FailedException('failed')
|
||||||
return body['data']['url'].split('?')[0]
|
|
||||||
|
return {
|
||||||
|
'url': data['url'].split('?')[0],
|
||||||
|
'quality': tools['qualityMapReverse'][data['audioFormatType']],
|
||||||
|
}
|
||||||
except:
|
except:
|
||||||
raise FailedException('failed')
|
raise FailedException('failed')
|
@ -43,6 +43,14 @@ tools = createObject({
|
|||||||
"h": "AI00",
|
"h": "AI00",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
'qualityMapReverse': {
|
||||||
|
'M500': '128k',
|
||||||
|
'M800': '320k',
|
||||||
|
'F000': 'flac',
|
||||||
|
'RS01': 'flac24bit',
|
||||||
|
'Q000': 'dolby',
|
||||||
|
'AI00': 'master'
|
||||||
|
},
|
||||||
"key": config.read_config("module.tx.user.qqmusic_key"),
|
"key": config.read_config("module.tx.user.qqmusic_key"),
|
||||||
"loginuin": config.read_config("module.tx.user.uin"),
|
"loginuin": config.read_config("module.tx.user.uin"),
|
||||||
"guid": config.read_config("module.tx.vkeyserver.guid"),
|
"guid": config.read_config("module.tx.vkeyserver.guid"),
|
||||||
@ -107,8 +115,20 @@ async def url(songId, quality):
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
req = signRequest(requestBody)
|
req = signRequest(requestBody)
|
||||||
body = CreateObject(req.json())
|
body = createObject(req.json())
|
||||||
# js const { purl } = data.req_0.data.midurlinfo[0]
|
data = body.req_0.data.midurlinfo[0]
|
||||||
if (not body.req_0.data.midurlinfo[0]['purl']):
|
url = data['purl']
|
||||||
|
|
||||||
|
if (not url):
|
||||||
raise FailedException('failed')
|
raise FailedException('failed')
|
||||||
return tools.cdnaddr + body.req_0.data.midurlinfo[0]['purl']
|
|
||||||
|
try:
|
||||||
|
resultQuality = data['filename'].split('.')[0].replace(data['songmid'], '')
|
||||||
|
except:
|
||||||
|
resultQuality = None
|
||||||
|
|
||||||
|
return {
|
||||||
|
'url': tools.cdnaddr + url,
|
||||||
|
'quality': tools.qualityMapReverse[resultQuality]
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,15 @@ tools = {
|
|||||||
"sky": "jysky",
|
"sky": "jysky",
|
||||||
"master": "jymaster",
|
"master": "jymaster",
|
||||||
},
|
},
|
||||||
|
'qualityMapReverse': {
|
||||||
|
'standard': '128k',
|
||||||
|
'exhigh': '320k',
|
||||||
|
'lossless': 'flac',
|
||||||
|
'hires': 'flac24bit',
|
||||||
|
"jyeffect": "dolby",
|
||||||
|
"jysky": "sky",
|
||||||
|
"jymaster": "master",
|
||||||
|
},
|
||||||
'cookie': config.read_config('module.wy.user.cookie'),
|
'cookie': config.read_config('module.wy.user.cookie'),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +52,13 @@ async def url(songId, quality):
|
|||||||
body = json.loads(req.text)
|
body = json.loads(req.text)
|
||||||
if (not body.get("data") or (not body.get("data")) or (not body.get("data")[0].get("url"))):
|
if (not body.get("data") or (not body.get("data")) or (not body.get("data")[0].get("url"))):
|
||||||
raise FailedException("failed")
|
raise FailedException("failed")
|
||||||
|
|
||||||
|
data = body["data"][0]
|
||||||
if (config.read_config('module.wy.reject_unmatched_quality')):
|
if (config.read_config('module.wy.reject_unmatched_quality')):
|
||||||
if (body['data'][0]['level'] != tools['qualityMap'][quality]):
|
if (data['level'] != tools['qualityMap'][quality]):
|
||||||
raise FailedException("reject unmatched quality")
|
raise FailedException("reject unmatched quality")
|
||||||
return body["data"][0]["url"].split("?")[0]
|
|
||||||
|
return {
|
||||||
|
'url': data["url"].split("?")[0],
|
||||||
|
'quality': tools['qualityMapReverse'][data['level']]
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user