fix(cloud_music.py): 更换API优化逻辑

This commit is contained in:
Miyokiss 2025-06-06 19:23:09 +08:00
parent e0791882db
commit 9effec5e6f

View file

@ -258,47 +258,29 @@ async def netease_music_download(song_id, song_name, singer, session):
except requests.RequestException as e:
return None
async def music_search(keyword):
"""
第三方歌曲搜索
Args:
keyword:
Returns:
"""
url = "https://api.kxzjoker.cn/api/163_search"
params = {
"name": keyword,
"limit": 10,
}
response = await async_client.get(url,params=params)
result = response.json()
song_id = result["data"][0]["id"]
song_name = result["data"][0]["name"]
singer = result["data"][0]["artists"][0]["name"]
return song_id,song_name,singer
async def music_download(song_id, song_name, singer):
async def music_download(song_id):
if not os.path.exists(save_path):
os.makedirs(save_path)
try:
headers = {
"Referer": "https://kxzjoker.cn/",
"Referer": "https://www.byfuns.top/api/1/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
"Origin": "https://kxzjoker.cn"
"Origin": "https://www.byfuns.top"
}
# 构造请求URL
url = f'https://api.kxzjoker.cn/api/163_music?ids={song_id}&level=lossless&type=down'
url = f'https://www.byfuns.top/api/1/?id={song_id}&level=lossless'
# 获取Url内容
song_url = requests.request("GET", url, headers=headers)
if song_url.status_code == 200:
# 异步流式下载
async with async_client.stream("GET",url,headers=headers,follow_redirects=True) as response:
async with async_client.stream("GET",song_url.text,headers=headers,follow_redirects=True) as response:
response.raise_for_status()
file_path = os.path.join(save_path, f"{song_name}-{singer}.wav")
file_name = os.path.basename(f"{song_name}-{singer}.wav")
if response.status_code == 200:
logger.debug(f"下载歌曲中...")
file_path = os.path.join(save_path, f"{datetime.now().date()}-{uuid.uuid4().hex}-{song_id}.wav")
file_name = os.path.basename(f"{datetime.now().date()}-{uuid.uuid4().hex}-{song_id}.wav")
with open(file_path, "wb") as f:
async for chunk in response.aiter_bytes(chunk_size=8192):
@ -310,9 +292,16 @@ async def music_download(song_id, song_name, singer):
# 删除临时文件
await delete_file(file_path)
return output_silk_path
except httpx.HTTPStatusError as e:
print(f"❌ HTTP错误 {e.response.status_code}")
except Exception as e:
print(f"❌ 下载失败:{str(e)}")
else:
logger.error(f"获取歌曲链接失败,状态码:{song_url.status_code}")
return None
else:
logger.error(f"获取歌曲链接失败,状态码:{song_url.status_code}")
return None
except httpx.Timeout as e:
logger.error(f"TimeoutError: {e}")
except httpx.HTTPStatusError as e:
logger.error(f"HTTPStatusError: {e}")
except Exception as e:
logger.error(e)
return None