forked from Diaoxiaozhang/Ximalaya-Downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
219 lines (216 loc) · 9.54 KB
/
cli.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import main
import asyncio
import re
import requests
import os
import time
import argparse
from fake_useragent import UserAgent
import tkinter as tk
from tkinter import filedialog
import json
ximalaya = main.Ximalaya()
loop = asyncio.get_event_loop()
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--sound', type=int, help='')
ua = UserAgent()
def select_directory():
root = tk.Tk()
root.withdraw()
directory_path = filedialog.askdirectory()
root.destroy()
return directory_path
if __name__ == "__main__":
print("欢迎使用喜马拉雅下载器")
cookie, path = ximalaya.analyze_config()
if not cookie:
username = False
else:
username = ximalaya.judge_cookie(cookie)
if os.path.isdir(path):
print(f"检测到已设置下载路径为{path}")
else:
print('在config文件中未检测到有效的下载路径,将使用默认下载路径./download')
path = './download'
response = requests.get(f"https://www.ximalaya.com/mobile-playpage/track/v3/baseInfo/{int(time.time() * 1000)}?device=web&trackId=188017958&trackQualityLevel=1",headers=ximalaya.default_headers)
if response.json()["ret"] == 927 and not username:
print("检测到当前ip不在中国大陆,由于喜马拉雅官方限制,必须登录才能继续使用,将自动跳转到登录流程")
ximalaya.login()
headers = {
"user-agent": ua.random,
"cookie": ximalaya.analyze_config()[0]
}
logined = True
elif not username:
while True:
print("未检测到有效喜马拉雅登录信息,请选择是否要登录:")
print("1. 登录")
print("2. 不登录")
choice = input()
if choice == "1":
ximalaya.login()
headers = {
"user-agent": ua.random,
"cookie": ximalaya.analyze_config()[0]
}
logined = True
break
elif choice == "2":
headers = ximalaya.default_headers
logined = False
break
else:
print("输入有误,请重新输入!")
else:
print(f"已检测到有效登录信息,当前登录用户为{username},如需切换账号请删除config.json文件然后重新启动本程序!")
headers = {
"user-agent": ua.random,
"cookie": ximalaya.analyze_config()[0]
}
logined = True
while True:
print("请选择要使用的功能:")
print("1. 下载单个声音")
print("2. 下载专辑声音")
print("3. 修改下载路径")
print("4. 退出程序")
choice = input()
if choice == "1":
print("请输入声音ID或链接:")
_ = input()
try:
sound_id = int(_)
except ValueError:
try:
sound_id = re.search(r"sound/(?P<sound_id>\d+)", _).group('sound_id')
except Exception:
print("输入有误,请重新输入!")
continue
sound_info = ximalaya.analyze_sound(sound_id, headers)
if sound_info is False:
continue
if sound_info == 0 and logined:
print(f"ID为{sound_id}的声音解析为vip声音或付费声音,但当前登录账号未购买!")
continue
elif sound_info == 0 and not logined:
print(f"ID为{sound_id}的声音解析为vip声音或付费声音,请登录后再试!")
continue
print(f"成功解析声音{sound_info['name']},请选择您要下载的音质:(直接回车默认为普通音质)")
print("0. 低音质")
print("1. 普通音质")
if sound_info[2] != "":
print("2. 高音质")
while True:
choice = input()
if choice == "":
choice = "1"
if choice == "0" or choice == "1":
ximalaya.get_sound(sound_info["name"], sound_info[int(choice)], path)
break
elif choice == "2" and sound_info[2] != "":
ximalaya.get_sound(sound_info["name"], sound_info[2], path)
break
else:
print("输入有误,请重新输入!")
elif choice == "2":
print("请输入专辑ID或链接:")
input_album = input()
try:
album_id = int(input_album)
except ValueError:
try:
album_id = re.search(r"album/(?P<album_id>\d+)", input_album).group('album_id')
except Exception:
print("输入有误,请重新输入!")
continue
album_name, sounds = ximalaya.analyze_album(album_id)
if not sounds:
continue
album_type = ximalaya.judge_album(album_id, headers)
if album_type == 0:
print(f"成功解析免费专辑{album_id},专辑名{album_name},共{len(sounds)}个声音")
elif album_type == 1:
print(f"成功解析已购付费专辑{album_id},专辑名{album_name},共{len(sounds)}个声音")
elif album_type == 2:
if logined is True:
print(f"成功解析付费专辑{album_id},专辑名{album_name},但是当前登陆账号未购买此专辑或未开通vip")
else:
print(f"成功解析付费专辑{album_id},专辑名{album_name},但是当前未登陆账号,请登录再尝试下载")
continue
else:
continue
while True:
print("请选择要使用的功能:")
print("1. 下载整个专辑")
print("2. 下载专辑的部分声音")
print("3. 显示专辑内声音列表")
choice = input()
if choice == "1" or choice == "2":
if choice == "1":
start = 1
end = len(sounds)
else:
while True:
print("请输入要下载的声音范围,中间用空格隔开,如输入“1 10”则表示下载第1到第10个声音:")
download_range = input()
try:
start, end = download_range.split(" ")
start = int(start)
end = int(end)
except Exception:
print("输入有误,请重新输入!")
continue
if start > end or start < 1 or end > len(sounds):
print("输入有误,请重新输入!")
else:
break
if album_type == 0 or album_type == 1:
while True:
print("请选择是否要在下载的音频文件名中加入序号:")
print("1. 加入序号")
print("2. 不加序号")
choice = input()
if choice == "1":
number = True
break
elif choice == "2":
number = False
break
else:
print("输入错误,请重新输入!")
print("请选择您想要下载的音质:(直接回车默认为普通音质)")
print("0. 低音质")
print("1. 普通音质")
print("2. 高音质(如果没有高音质则将下载普通音质)")
choice = input()
while True:
if choice == "":
choice = "1"
if choice == "0" or choice == "1" or choice == "2":
loop.run_until_complete(ximalaya.get_selected_sounds(sounds, album_name, start, end, headers, int(choice), number, path))
break
else:
print("输入有误,请重新输入!")
break
elif choice == "3":
for sound in sounds:
print(f"{sound['index']}. {sound['title']}")
else:
print("无效的选择,请重新输入。")
elif choice == "3":
print("请在弹出的窗口中选择下载路径。")
path_ = select_directory()
if path_ == "":
print("检测到目录选择窗口被关闭,将继续使用原有下载路径。")
else:
path = path_
with open("config.json", "r") as f:
config = json.load(f)
config["path"] = path
with open("config.json", "w") as f:
json.dump(config, f)
print(f"成功修改下载路径为{path}")
elif choice == "4":
break
else:
print("无效的选择,请重新输入。")