-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
140 lines (112 loc) · 4.17 KB
/
examples.py
File metadata and controls
140 lines (112 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
"""
CloudMusic 使用示例
展示各个模块的基本用法
"""
import sys
from pathlib import Path
# 添加项目路径
sys.path.insert(0, str(Path(__file__).parent))
import requests
from cloudmusic.api import QUALITY_LEVELS, resolve_play_url, parse_lrc, get_current_lyric
from cloudmusic.config import Config
from cloudmusic.player_controller import PlayerController
def example_api():
"""API 模块使用示例"""
print("=== API 模块示例 ===\n")
# 1. 查看支持的音质
print("支持的音质:")
for quality, config in QUALITY_LEVELS.items():
print(f" - {quality}: {config['br']} bps")
# 2. 解析歌词
print("\n歌词解析示例:")
lrc_text = """
[00:00.00]音乐播放器示例
[00:05.20]欢迎使用 CloudMusic
[00:10.50]支持多种音质
[00:15.80]歌词同步显示
"""
lyrics = parse_lrc(lrc_text)
for i, (timestamp, text) in enumerate(lyrics[:3]):
print(f" {i+1}. [{timestamp:.2f}s] {text}")
# 3. 获取当前歌词
print(f"\n播放到 7.5 秒时的歌词:")
print(f" {get_current_lyric(lyrics, 7.5)}")
def example_config():
"""配置模块使用示例"""
print("\n=== 配置模块示例 ===\n")
# 加载配置(首次运行会创建默认配置)
config = Config.load()
print("当前配置:")
print(f" 音质: {config.quality}")
print(f" 下载目录: {config.download_dir}")
print(f" 最大并发下载数: {config.max_concurrent_downloads}")
print(f" 主题: {config.theme}")
print(f" 音量: {config.volume}")
print(f" 循环模式: {config.loop_mode}")
print(f" 随机播放: {config.shuffle}")
# 修改配置
print("\n修改配置...")
config.quality = "exhigh"
config.volume = 80
config.shuffle = True
config.save()
print("配置已保存")
# 重新加载验证
config2 = Config.load()
print(f"\n验证加载: quality={config2.quality}, volume={config2.volume}, shuffle={config2.shuffle}")
def example_player():
"""播放器控制器使用示例"""
print("\n=== 播放器控制器示例 ===\n")
# 创建播放器控制器
player = PlayerController()
print("播放器控制器已创建")
print(f" Socket 路径: {player.socket_path}")
print(f" 播放器状态: {'运行中' if player.is_alive() else '未运行'}")
# 注意:实际播放需要有效的音乐 URL
print("\n提示:使用 player.play(url, title) 播放音乐")
print("示例:")
print(" player.play('http://example.com/music.mp3', '歌曲标题')")
print(" player.pause() # 暂停")
print(" player.resume() # 恢复")
print(" player.seek(30.0) # 跳转到 30 秒")
print(" player.set_volume(80) # 设置音量为 80")
print(" player.stop() # 停止播放")
def example_download():
"""下载模块使用示例"""
print("\n=== 下载模块示例 ===\n")
from cloudmusic.downloader import download_songs_batch_sync
print("并发下载示例:")
print(" from cloudmusic.downloader import download_songs_batch_sync")
print(" ")
print(" # 创建客户端会话")
print(" client = requests.Session()")
print(" client.cookies.update({'MUSIC_U': 'your_cookie'})")
print(" ")
print(" # 下载歌曲")
print(" song_ids = [123456, 234567, 345678]")
print(" results = download_songs_batch_sync(")
print(" client=client,")
print(" song_ids=song_ids,")
print(" output_dir=Path('./downloads'),")
print(" level='standard',")
print(" max_concurrent=5,")
print(" progress_callback=lambda completed, total: print(f'{completed}/{total}')")
print(" )")
print(" ")
print(" for song_id, success, message in results:")
print(" print(f'{song_id}: {'✓' if success else '✗'} {message}')")
def main():
"""运行所有示例"""
print("CloudMusic 模块使用示例")
print("=" * 50)
print()
example_api()
example_config()
example_player()
example_download()
print("\n" + "=" * 50)
print("示例运行完成!")
print("\n更多信息请参考各模块的文档字符串。")
if __name__ == "__main__":
main()