feat(cloud_music): 修复登录状态校验问题

This commit is contained in:
SlyAimer 2025-03-03 21:36:06 +08:00
parent 3a7151bb6d
commit 6e97aa3b4b
3 changed files with 76 additions and 56 deletions

View file

@ -3,6 +3,8 @@ nonebot_adapter_qq
nonebot2[fastapi] nonebot2[fastapi]
nonebot2[httpx] nonebot2[httpx]
nonebot2[websockets] nonebot2[websockets]
apscheduler
ffmpeg-python ffmpeg-python
sqlalchemy sqlalchemy
@ -26,6 +28,7 @@ nonebot-plugin-htmlrender
tenacity tenacity
paramiko paramiko
requests requests
pillow pillow
pyyaml pyyaml

View file

@ -67,6 +67,14 @@ qrcode_path = os.getcwd()+'/src/clover_music'
# 判断cookie是否有效 # 判断cookie是否有效
async def netease_cloud_music_is_login(session): async def netease_cloud_music_is_login(session):
"""
验证登录状态
Args:
session:
Returns:
"""
try: try:
session.cookies.load(ignore_discard=True) session.cookies.load(ignore_discard=True)
except Exception: except Exception:
@ -77,18 +85,23 @@ async def netease_cloud_music_is_login(session):
else: else:
try: try:
loginurl = session.post(f'https://music.163.com/weapi/w/nuser/account/get?csrf_token={csrf_token}',data={'params': login_params(None), 'encSecKey': login_encSecKey()}, headers=headers).json() loginurl = session.post(f'https://music.163.com/weapi/w/nuser/account/get?csrf_token={csrf_token}',data={'params': login_params(None), 'encSecKey': login_encSecKey()}, headers=headers).json()
if '200' in str(loginurl['code']): if loginurl['account'] is not None:
print(loginurl) return session, True,loginurl['account']['id']
print('登录成功')
return session, True
else: else:
print('登录失败')
return session, False return session, False
except BaseException: except BaseException:
return session, False return session, False,None
# 获取二维码的key # 获取二维码的key
async def get_qr_key(session): async def get_qr_key(session):
"""
获取unikey
Args:
session:
Returns:
"""
url = f"https://music.163.com/weapi/login/qrcode/unikey" url = f"https://music.163.com/weapi/login/qrcode/unikey"
data = {"params": login_params(None),"encSecKey": login_encSecKey()} data = {"params": login_params(None),"encSecKey": login_encSecKey()}
response = session.post(url, headers=headers,params=data) response = session.post(url, headers=headers,params=data)
@ -103,6 +116,14 @@ async def get_qr_key(session):
qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, )
# 生成二维码 # 生成二维码
async def create_qr_code(unikey): async def create_qr_code(unikey):
"""
二维码生成
Args:
unikey:
Returns:
"""
# 添加数据 # 添加数据
png_url = f"http://music.163.com/login?codekey={unikey}" png_url = f"http://music.163.com/login?codekey={unikey}"
qr.add_data(png_url) qr.add_data(png_url)
@ -120,6 +141,15 @@ async def check_qr_code(unikey,session):
return qrcode_data.get('code') return qrcode_data.get('code')
async def netease_music_search(keyword,session): async def netease_music_search(keyword,session):
"""
歌曲搜索
Args:
keyword:
session:
Returns:
"""
url = "http://music.163.com/api/search/get" url = "http://music.163.com/api/search/get"
params = { params = {
"s": keyword, "s": keyword,
@ -164,28 +194,39 @@ async def netease_music_search(keyword,session):
#所有歌曲都可以下载 #所有歌曲都可以下载
async def netease_music_download(song_id, song_name, singer, session): async def netease_music_download(song_id, song_name, singer, session):
"""
歌曲下载
Args:
song_id:
song_name:
singer:
session:
Returns:
"""
if not os.path.exists(save_path): if not os.path.exists(save_path):
os.makedirs(save_path) os.makedirs(save_path)
# 获取加密后的歌曲id信息 # 获取加密后的歌曲id信息
result = get_music(song_id) result = get_music(song_id)
data = {'params': result['encText'], 'encSecKey': result['encSecKey'],} data = {'params': result['encText'], 'encSecKey': result['encSecKey']}
download_url = 'https://music.163.com/weapi/song/enhance/player/url/v1?br=999000' download_url = 'https://music.163.com/weapi/song/enhance/player/url/v1?br=999000'
url = session.post(download_url, headers=headers,data=data).json()['data'][0]['url'] response_data = session.post(download_url, headers=headers, data=data).json()
url = response_data['data'][0]['url']
if url is None: if url is None:
return -1 return -1
# 下载歌曲 # 下载歌曲
data = requests.get(url, stream=True) try:
response = requests.get(url, stream=True)
if response.status_code == 200:
file_path = os.path.join(save_path, f"{song_name}-{singer}.wav") file_path = os.path.join(save_path, f"{song_name}-{singer}.wav")
file_name = os.path.basename(f"{song_name}-{singer}.wav") file_name = os.path.basename(f"{song_name}-{singer}.wav")
with open(file_path, "wb") as f:
f.write(data.content) with open(file_path, "wb") as file:
if data.status_code == 200: for chunk in response.iter_content(chunk_size=8192):
with open(file_path, 'wb') as file:
for chunk in data.iter_content(chunk_size=8192):
file.write(chunk) file.write(chunk)
output_silk_path = os.path.join(save_path, os.path.splitext(file_name)[0] + ".silk") output_silk_path = os.path.join(save_path, os.path.splitext(file_name)[0] + ".silk")
# 使用 graiax-silkcoder 进行转换 # 使用 graiax-silkcoder 进行转换
silkcoder.encode(file_path, output_silk_path, rate=32000, tencent=True, ios_adaptive=True) silkcoder.encode(file_path, output_silk_path, rate=32000, tencent=True, ios_adaptive=True)
@ -194,29 +235,5 @@ async def netease_music_download(song_id,song_name,singer,session):
return output_silk_path return output_silk_path
else: else:
return None return None
except requests.RequestException as e:
return None

View file

@ -22,7 +22,7 @@ async def handle_function(msg: MessageEvent):
pickle.dump(session.cookies, f) pickle.dump(session.cookies, f)
# 读取 cookie # 读取 cookie
session.cookies = pickle.load(open('cloud_music_cookies.cookie', 'rb')) session.cookies = pickle.load(open('cloud_music_cookies.cookie', 'rb'))
session, status = await netease_cloud_music_is_login(session) session, status,user_id = await netease_cloud_music_is_login(session)
if not status: if not status:
await music.send("登录失效,请联系管理员进行登录") await music.send("登录失效,请联系管理员进行登录")
unikey = await get_qr_key(session) unikey = await get_qr_key(session)