247 lines
7.1 KiB
Python
247 lines
7.1 KiB
Python
"""
|
|
测试音乐库外部链接 API
|
|
"""
|
|
import requests
|
|
import json
|
|
|
|
# 配置
|
|
BASE_URL = "http://localhost:30101"
|
|
# 需要替换为实际的 token
|
|
TOKEN = "your_token_here"
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {TOKEN}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
|
|
def test_add_netease_music():
|
|
"""测试添加网易云音乐"""
|
|
print("\n=== 测试添加网易云音乐 ===")
|
|
|
|
data = {
|
|
"title": "七里香",
|
|
"artist": "周杰伦",
|
|
"platform": "netease",
|
|
"external_id": "186016",
|
|
"external_url": "https://music.163.com/#/song?id=186016",
|
|
"cover_url": "https://p1.music.126.net/P1ciTpERjRdYqk1v7MD05w==/109951163076136658.jpg",
|
|
"duration": 300
|
|
}
|
|
|
|
try:
|
|
response = requests.post(
|
|
f"{BASE_URL}/music/add-external",
|
|
headers=headers,
|
|
json=data
|
|
)
|
|
print(f"状态码: {response.status_code}")
|
|
print(f"响应: {json.dumps(response.json(), indent=2, ensure_ascii=False)}")
|
|
return response.json()
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
return None
|
|
|
|
|
|
def test_add_qq_music():
|
|
"""测试添加 QQ 音乐"""
|
|
print("\n=== 测试添加 QQ 音乐 ===")
|
|
|
|
data = {
|
|
"title": "稻香",
|
|
"artist": "周杰伦",
|
|
"platform": "qq",
|
|
"external_id": "003aAYrm3GE0Ac",
|
|
"external_url": "https://y.qq.com/n/ryqq/songDetail/003aAYrm3GE0Ac",
|
|
"cover_url": "https://y.gtimg.cn/music/photo_new/T002R300x300M000003Nz2So3XXYek.jpg",
|
|
"duration": 223
|
|
}
|
|
|
|
try:
|
|
response = requests.post(
|
|
f"{BASE_URL}/music/add-external",
|
|
headers=headers,
|
|
json=data
|
|
)
|
|
print(f"状态码: {response.status_code}")
|
|
print(f"响应: {json.dumps(response.json(), indent=2, ensure_ascii=False)}")
|
|
return response.json()
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
return None
|
|
|
|
|
|
def test_add_kugou_music():
|
|
"""测试添加酷狗音乐"""
|
|
print("\n=== 测试添加酷狗音乐 ===")
|
|
|
|
data = {
|
|
"title": "青花瓷",
|
|
"artist": "周杰伦",
|
|
"platform": "kugou",
|
|
"external_id": "sample_id",
|
|
"external_url": "https://www.kugou.com/song/#hash=sample",
|
|
"cover_url": "",
|
|
"duration": 230
|
|
}
|
|
|
|
try:
|
|
response = requests.post(
|
|
f"{BASE_URL}/music/add-external",
|
|
headers=headers,
|
|
json=data
|
|
)
|
|
print(f"状态码: {response.status_code}")
|
|
print(f"响应: {json.dumps(response.json(), indent=2, ensure_ascii=False)}")
|
|
return response.json()
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
return None
|
|
|
|
|
|
def test_get_music_library():
|
|
"""测试获取音乐库列表"""
|
|
print("\n=== 测试获取音乐库列表 ===")
|
|
|
|
try:
|
|
response = requests.get(
|
|
f"{BASE_URL}/music/library?page=1&page_size=10",
|
|
headers=headers
|
|
)
|
|
print(f"状态码: {response.status_code}")
|
|
result = response.json()
|
|
print(f"总数: {result.get('data', {}).get('total', 0)}")
|
|
|
|
# 显示前 5 条
|
|
music_list = result.get('data', {}).get('list', [])
|
|
print(f"\n前 {min(5, len(music_list))} 首音乐:")
|
|
for i, music in enumerate(music_list[:5], 1):
|
|
print(f"\n{i}. {music.get('title')} - {music.get('artist')}")
|
|
print(f" 类型: {music.get('upload_type')}")
|
|
if music.get('external_platform'):
|
|
print(f" 平台: {music.get('external_platform')}")
|
|
print(f" 链接: {music.get('external_url')}")
|
|
else:
|
|
print(f" 链接: {music.get('music_url')[:50]}...")
|
|
|
|
return result
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
return None
|
|
|
|
|
|
def test_get_my_music():
|
|
"""测试获取我的音乐"""
|
|
print("\n=== 测试获取我的音乐 ===")
|
|
|
|
try:
|
|
response = requests.get(
|
|
f"{BASE_URL}/music/my?page=1&page_size=10",
|
|
headers=headers
|
|
)
|
|
print(f"状态码: {response.status_code}")
|
|
result = response.json()
|
|
print(f"总数: {result.get('data', {}).get('total', 0)}")
|
|
|
|
# 按类型统计
|
|
music_list = result.get('data', {}).get('list', [])
|
|
type_count = {}
|
|
for music in music_list:
|
|
upload_type = music.get('upload_type')
|
|
type_count[upload_type] = type_count.get(upload_type, 0) + 1
|
|
|
|
print("\n按类型统计:")
|
|
for upload_type, count in type_count.items():
|
|
print(f" {upload_type}: {count} 首")
|
|
|
|
return result
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
return None
|
|
|
|
|
|
def test_invalid_platform():
|
|
"""测试无效的平台"""
|
|
print("\n=== 测试无效的平台 ===")
|
|
|
|
data = {
|
|
"title": "测试歌曲",
|
|
"artist": "测试歌手",
|
|
"platform": "invalid_platform",
|
|
"external_id": "123",
|
|
"external_url": "https://example.com/song/123",
|
|
"cover_url": "",
|
|
"duration": 200
|
|
}
|
|
|
|
try:
|
|
response = requests.post(
|
|
f"{BASE_URL}/music/add-external",
|
|
headers=headers,
|
|
json=data
|
|
)
|
|
print(f"状态码: {response.status_code}")
|
|
print(f"响应: {json.dumps(response.json(), indent=2, ensure_ascii=False)}")
|
|
return response.json()
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
return None
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
print("=" * 60)
|
|
print("音乐库外部链接 API 测试")
|
|
print("=" * 60)
|
|
|
|
# 检查 token
|
|
if TOKEN == "your_token_here":
|
|
print("\n⚠️ 警告: 请先设置有效的 TOKEN")
|
|
print(" 1. 登录应用获取 token")
|
|
print(" 2. 修改此脚本中的 TOKEN 变量")
|
|
print(" 3. 重新运行测试")
|
|
return
|
|
|
|
# 运行测试
|
|
tests = [
|
|
("添加网易云音乐", test_add_netease_music),
|
|
("添加 QQ 音乐", test_add_qq_music),
|
|
("添加酷狗音乐", test_add_kugou_music),
|
|
("获取音乐库列表", test_get_music_library),
|
|
("获取我的音乐", test_get_my_music),
|
|
("测试无效平台", test_invalid_platform),
|
|
]
|
|
|
|
results = []
|
|
for name, test_func in tests:
|
|
try:
|
|
result = test_func()
|
|
success = result and result.get('code') == 200
|
|
results.append((name, success))
|
|
except Exception as e:
|
|
print(f"\n测试 {name} 时出错: {e}")
|
|
results.append((name, False))
|
|
|
|
# 显示测试结果
|
|
print("\n" + "=" * 60)
|
|
print("测试结果汇总")
|
|
print("=" * 60)
|
|
|
|
for name, success in results:
|
|
status = "✅ 通过" if success else "❌ 失败"
|
|
print(f"{status} - {name}")
|
|
|
|
# 统计
|
|
passed = sum(1 for _, success in results if success)
|
|
total = len(results)
|
|
print(f"\n总计: {passed}/{total} 通过")
|
|
|
|
if passed == total:
|
|
print("\n🎉 所有测试通过!")
|
|
else:
|
|
print(f"\n⚠️ 有 {total - passed} 个测试失败")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|