feat: kg源mv支持

This commit is contained in:
helloplhm-qwq 2023-12-17 13:49:45 +08:00
parent 84b1212c7e
commit 484adde934
No known key found for this signature in database
GPG Key ID: 6BE1B64B905567C7
3 changed files with 127 additions and 8 deletions

View File

@ -94,7 +94,8 @@ async def url(source, songId, quality):
expireTime = sourceExpirationTime[source]['time'] + int(time.time())
config.updateCache('urls', f'{source}_{songId}_{quality}', {
"expire": canExpire,
"time": int(expireTime - sourceExpirationTime[source]['time'] * 0.25), # 取有效期的75%作为链接可用时长
# 取有效期的75%作为链接可用时长
"time": int(expireTime - sourceExpirationTime[source]['time'] * 0.25),
"url": result['url'],
})
logger.debug(f'缓存已更新:{source}_{songId}_{quality}, URL{result["url"]}, expire: {expireTime}')
@ -143,4 +144,26 @@ async def info(source, songid, _):
'code': 2,
'msg': e.args[0],
'data': None,
}
async def mv(source, mvId, _):
try:
func = require('modules.' + source + '.mv')
except:
return {
'code': 1,
'msg': '未知的源或不支持的方法',
'data': None,
}
try:
result = await func(mvId)
return {
'code': 0,
'msg': 'success',
'data': result
}
except FailedException as e:
return {
'code': 2,
'msg': e.args[0],
}

View File

@ -7,10 +7,13 @@
# ----------------------------------------
# This file is part of the "lx-music-api-server" project.
from .musicInfo import getMusicMVHash as _getMVHash
from .musicInfo import getMusicSingerInfo as _getInfo2
from .musicInfo import getMusicInfo as _getInfo
from .utils import tools
from .player import url
from .mv import getMvInfo as _getMvInfo
from .mv import getMvPlayURL as _getMvUrl
from common.exceptions import FailedException
from common import Httpx
from common import utils
@ -20,13 +23,10 @@ async def info(hash_):
tasks = []
tasks.append(_getInfo(hash_))
tasks.append(_getInfo2(hash_))
tasks.append(Httpx.request('http://mobilecdnbj.kugou.com/api/v3/song/info?hash=' + hash_, {
'method': 'GET'
}))
tasks.append(_getMVHash(hash_))
res = await asyncio.gather(*tasks)
res1 = res[0]
res2 = res[1]
mvhash = res[2].json()['data']['mvhash'] if (res[2].json()['data']) else ''
file_info = {}
for k, v in tools['qualityHashMap'].items():
if (res1['audio_info'][v] and k != 'master'):
@ -51,11 +51,21 @@ async def info(hash_):
'songmid': res1['audio_id'],
'album_id': res1['album_info']['album_id'],
'album': res1['album_info']['album_name'],
'bpm': res1['bpm'],
'bpm': int(res1['bpm']),
'language': res1['language'],
'cover': res1['album_info']['sizable_cover'].format(size = 1080),
'sizable_cover': res1['album_info']['sizable_cover'],
'publish_date': res1['publish_date'],
'mvid': mvhash,
'mvid': res[2],
'genre': []
}
}
async def mv(hash_):
tasks = []
tasks.append(_getMvInfo(hash_))
tasks.append(_getMvUrl(hash_))
res = await asyncio.gather(*tasks)
res1 = res[0]
res2 = res[1]
res1['play_info'] = res2
return res1

86
modules/kg/mv.py Normal file
View File

@ -0,0 +1,86 @@
# ----------------------------------------
# - mode: python -
# - author: helloplhm-qwq -
# - name: mv.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
async def getMvInfo(mvhash, use_cache = True):
req = await Httpx.AsyncRequest(f'http://mobilecdnbj.kugou.com/api/v3/mv/detail?mvhash={mvhash}', {
'method': 'GET',
'cache': 86400 * 30 if use_cache else 'no-cache',
})
body = req.json()
if (body['status'] != 1):
raise FailedException('获取失败')
if (not body['data']['info']):
raise FailedException('mv不存在')
singers = []
for s in body['data']['info']['authors']:
singers.append({
'name': s['singername'],
'id': s['singerid'],
'avatar': s['singeravatar'].format(size=1080),
'sizable_avatar': s['singeravatar'],
})
tags = []
for t in body['data']['info']['tags']:
tags.append(t['tag_name'])
return {
'name': body['data']['info']['filename'].replace(body['data']['info']['singername'] + ' - ', ''),
'name_ori': body['data']['info']['videoname'],
'name_extra': body['data']['info']['remark'],
'filename': body['data']['info']['filename'],
'intro': body['data']['info']['description'],
'music_hash': body['data']['info']['audio_info']['hash'],
'music_id': body['data']['info']['audio_info']['audio_id'],
'format_length': utils.timeLengthFormat(body['data']['info']['mv_timelength'] / 1000),
'length': body['data']['info']['mv_timelength'] / 1000,
'hash': body['data']['info']['hash'],
'vid': body['data']['info']['video_id'],
'singer': body['data']['info']['singername'],
'singer_list': singers,
'tags': tags,
'cover': body['data']['info']['imgurl'].format(size=1080),
'sizable_cover': body['data']['info']['imgurl'],
}
async def getMvPlayURL(mvhash):
req = await Httpx.AsyncRequest(f'https://m.kugou.com/app/i/mv.php?cmd=100&hash={mvhash}&ismp3=1&ext=mp4', {
'method': 'GET'
})
body = req.json()
if (body['status'] != 1):
return {}
formatted = {}
if (body['mvdata']['le']):
formatted['270p'] = {
'url': body['mvdata']['le']['downurl'],
'hash': body['mvdata']['le']['hash'],
'bitrate': body['mvdata']['le']['bitrate'],
'format_size': utils.sizeFormat(body['mvdata']['le']['filesize']),
'size': body['mvdata']['le']['filesize'],
}
if (body['mvdata']['sq']):
formatted['720p'] = {
'url': body['mvdata']['sq']['downurl'],
'hash': body['mvdata']['sq']['hash'],
'bitrate': body['mvdata']['sq']['bitrate'],
'format_size': utils.sizeFormat(body['mvdata']['sq']['filesize']),
'size': body['mvdata']['sq']['filesize'],
}
if (body['mvdata']['rq']):
formatted['1080p'] = {
'url': body['mvdata']['rq']['downurl'],
'hash': body['mvdata']['rq']['hash'],
'bitrate': body['mvdata']['rq']['bitrate'],
'format_size': utils.sizeFormat(body['mvdata']['rq']['filesize']),
'size': body['mvdata']['rq']['filesize'],
}
return formatted